-
Notifications
You must be signed in to change notification settings - Fork 5
/
SqlLogging.java
69 lines (64 loc) · 2.12 KB
/
SqlLogging.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Ver Creation_Time Created_By Update_Time Updated_By Description
--- ------------- -------------- ----------- ---------- -------------------------------------
1.1 2019-09-04 xlzhu@ips.com java logging
*/
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.Level;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
import java.util.logging.Handler;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.io.IOException;
public class SqlLogging{
private Logger sloger;
public SqlLogging(String pathFile){
try{
this.sloger = Logger.getLogger("IPSSqlCheckLogging");
Handler[] handlers = this.sloger.getHandlers();
for (Handler hd:handlers){
this.sloger.removeHandler(hd);
}
this.sloger.setUseParentHandlers(false);
SqlLogFormatter sLF=new SqlLogFormatter();
ConsoleHandler consoleHandler =new ConsoleHandler();
//consoleHandler.setLevel(Level.ALL);
consoleHandler.setFormatter(sLF);
this.sloger.addHandler(consoleHandler);
String dateString = new SimpleDateFormat("yyyyMMdd").format(new Date())+".log";
FileHandler fileHandler = new FileHandler(pathFile+dateString,10000000,10,true);
//fileHandler.setLevel(Level.ALL);
fileHandler.setFormatter(sLF);
this.sloger.addHandler(fileHandler);
//java.util.logging.SimpleFormatter.format="%1$tF %1$tH:%1$tM:%1$tS %2$s%n%4$s: %5$s%6$s%n";
}catch(IOException e) {
e.printStackTrace();
}
}
public void log(String msg){
sloger.info(msg);
}
public void log(String msg,char type){
switch (type){
case 'w':
sloger.warning(msg);
break;
case 's':
sloger.severe(msg);
break;
default:
sloger.info(msg);
break;
}
}
class SqlLogFormatter extends Formatter{
@Override
public String format(LogRecord record) {
return new SimpleDateFormat("yyyyMMdd HH:mm:ss").format(new Date()) + String.format("%-9s", "["+record.getLevel()+"]")+":"
+record.getMessage()+"\r\n";
}
}
}