①使用到的jar包:
大概是这几个,具体我也不知道是哪一个,下次使用的时候自己测试下。
commons-logging-1.2.jar
commons-discovery-0.5.jar
log4j-1.2.17.jar
②log4j.properties
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
log4j.rootLogger=DEBUG,CONSOLE,A
log4j.addivity.org.apache=
false
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=DEBUG
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} -%-4r [%t] %-5p %x - %m%n
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.Encoding=gbk
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.A=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A.File=${catalina.home}/logs/kentra_log/PurePro_
log4j.appender.A.DatePattern=yyyy-MM-dd
'.log'
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=[FH_sys] %d{yyyy-MM-dd HH\:mm\:ss} %5p %c{
1
}\:%L \: %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
|
③log4j.xml
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
|
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<!DOCTYPE log4j:configuration PUBLIC
"-//APACHE//DTD LOG4J 1.2//EN"
"log4j.dtd"
>
<log4j:configuration xmlns:log4j=
"http://jakarta.apache.org/log4j/"
>
<!-- Appenders -->
<appender name=
"console"
class
=
"org.apache.log4j.ConsoleAppender"
>
<param name=
"Target"
value=
"System.out"
/>
<layout
class
=
"org.apache.log4j.PatternLayout"
>
<param name=
"ConversionPattern"
value=
"%d{yyyy HH:mm:ss} %-5p %c - %m%n"
/>
</layout>
</appender>
<!-- Application Loggers -->
<logger name=
"com.kentra"
>
<level value=
"info"
/>
</logger>
<!-- 3rdparty Loggers -->
<logger name=
"org.springframework.core"
>
<level value=
"info"
/>
</logger>
<logger name=
"org.springframework.beans"
>
<level value=
"info"
/>
</logger>
<logger name=
"org.springframework.context"
>
<level value=
"info"
/>
</logger>
<logger name=
"org.springframework.web"
>
<level value=
"info"
/>
</logger>
<logger name=
"org.springframework.jdbc"
>
<level value=
"info"
/>
</logger>
<logger name=
"org.mybatis.spring"
>
<level value=
"info"
/>
</logger>
<logger name=
"java.sql"
>
<level value=
"info"
/>
</logger>
<!-- Root Logger -->
<root>
<priority value=
"info"
/>
<appender-ref ref=
"console"
/>
</root>
</log4j:configuration>
|
④Logger.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
package
com.kentra.util;
/**
* 说明:日志处理
* 创建人:KENTRASOFT
* 修改时间:2014年9月20日
* @version
*/
public
class
Logger {
private
org.apache.log4j.Logger logger;
/**
* 构造方法,初始化Log4j的日志对象
*/
private
Logger(org.apache.log4j.Logger log4jLogger) {
logger = log4jLogger;
}
/**
* 获取构造器,根据类初始化Logger对象
*
* @param Class
* Class对象
* @return Logger对象
*/
public
static
Logger getLogger(Class classObject) {
return
new
Logger(org.apache.log4j.Logger.getLogger(classObject));
}
/**
* 获取构造器,根据类名初始化Logger对象
*
* @param String
* 类名字符串
* @return Logger对象
*/
public
static
Logger getLogger(String loggerName) {
return
new
Logger(org.apache.log4j.Logger.getLogger(loggerName));
}
public
void
debug(Object object) {
logger.debug(object);
}
public
void
debug(Object object, Throwable e) {
logger.debug(object, e);
}
public
void
info(Object object) {
logger.info(object);
}
public
void
info(Object object, Throwable e) {
logger.info(object, e);
}
public
void
warn(Object object) {
logger.warn(object);
}
public
void
warn(Object object, Throwable e) {
logger.warn(object, e);
}
public
void
error(Object object) {
logger.error(object);
}
public
void
error(Object object, Throwable e) {
logger.error(object, e);
}
public
void
fatal(Object object) {
logger.fatal(object);
}
public
String getName() {
return
logger.getName();
}
public
org.apache.log4j.Logger getLog4jLogger() {
return
logger;
}
public
boolean
equals(Logger newLogger) {
return
logger.equals(newLogger.getLog4jLogger());
}
}
|
⑤使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/**保存
* @param
* @throws Exception
*/
@RequestMapping
(value=
"/save"
)
@ResponseBody
public
ResultUtil save(Supervision supervision,String fileGrids)
throws
Exception{
String busId =
this
.get32UUID();
if
(!Jurisdiction.buttonJurisdiction(
this
.getPageData().getString(
"key"
))){
return
null
;}
//校验权限
supervision.setId(busId);
supervision.setCreatTime(DateUtil.getTime());
supervisionService.save(supervision);
logger.info(
"-----------日志打印------------"
);
logBefore(logger, busId, Jurisdiction.getUsername(),
"督办单-新增"
);
return
ResultUtil.success(
"添加成功!"
,
"supervision"
,
""
,
"closeCurrent"
);
}
|
本文转自建波李 51CTO博客,原文链接:http://blog.51cto.com/jianboli/1960178,如需转载请自行联系原作者