** 定义子logger其目的就是能够在某一范围内(某一个class或者某一个package)下面,日志的输出方式与其他地方的日志输出方式不同。**
log4j.properties
[java] view plain copy
<embed id="ZeroClipboardMovie_1" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" name="ZeroClipboardMovie_1" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&width=16&height=16" wmode="transparent" width="16" height="16" align="middle">
-
<strong><span style="font-size:14px;">log4j.rootLogger = DEBUG,stdout,D
-
log4j.debug=ture //是否打开log4j的内部日志打印
-
(ConsoleAppender:控制台打印)
-
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
-
log4j.appender.stdout.Target = System.out
-
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
-
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
-
(DailyRollingFileAppender)
-
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
-
log4j.appender.D.File = G://logs/log.log
-
log4j.appender.D.Append = true
-
log4j.appender.D.Threshold = DEBUG
-
log4j.appender.D.layout = org.apache.log4j.PatternLayout
-
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
-
(mail)
-
log4j.appender.mail=org.apache.log4j.net.SMTPAppender
-
log4j.appender.mail.Threshold=DEBUG
-
log4j.appender.mail.BufferSize=10
-
log4j.appender.mail.From = 123@qq.com
-
log4j.appender.mail.SMTPHost=smtp.qq.com
-
log4j.appender.mail.Subject=Log4J Message
-
log4j.appender.mail.SMTPUsername=123
-
log4j.appender.mail.SMTPPassword=xxx
-
log4j.appender.mail.layout=org.apache.log4j.PatternLayout
-
log4j.appender.mail.layout.ConversionPattern=[%-5p] %d(%r) --> [%t] %l: %m %x %n
-
配置子logger,目的就是控制一定范围内的日志输出,比如这里的子logger是控制cn.server.test包下面的所有类的日志输出,其父logger就是rootLogger
-
log4j.logger.cn.server.test=error,E
-
log4j.additivity.cn.server.test=false //
-
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
-
log4j.appender.E.File =G://logs/error.log
-
log4j.appender.E.Append = true
-
log4j.appender.E.Threshold = ERROR
-
log4j.appender.E.layout = org.apache.log4j.PatternLayout
-
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
**配置子logger,目的就是控制一定范围内的日志输出,比如这里的子logger是控制cn.server.test包下面的所有类的日志输出,其父logger就是rootLogger。**
[java] view plain copy
<embed id="ZeroClipboardMovie_2" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" name="ZeroClipboardMovie_2" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&width=16&height=16" wmode="transparent" width="16" height="16" align="middle">
- log4j.logger.cn.server.test=error,E
** 在cn.server.test包里面中使用**
[java] view plain copy
<embed id="ZeroClipboardMovie_3" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" name="ZeroClipboardMovie_3" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&width=16&height=16" wmode="transparent" width="16" height="16" align="middle">
- Logger logger = Logger.getLogger(xxx.class)
得到logger对象,然后使用logger对象进行日志输入的时候,logger会根据xxx.class的全类名找到这里声明的子Logger
[plain] view plain copy
<embed id="ZeroClipboardMovie_4" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" name="ZeroClipboardMovie_4" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=4&width=16&height=16" wmode="transparent" width="16" height="16" align="middle">
- log4j.logger.cn.server.test=error,E
在cn.server.test包外的类,也可以使用
[java] view plain copy
<embed id="ZeroClipboardMovie_5" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" name="ZeroClipboardMovie_5" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=5&width=16&height=16" wmode="transparent" width="16" height="16" align="middle">
- Logger logger = Logger.getLogger("cn.server.test");
特别指定使用该子logger进行日志输出。
log4j的additivity属性: 表示子logger是否要在父logger的appender中输出日志,若是additivity设为false,则子Logger只会在自己的appender里输出,而不会在父Logger的appender里输出。
网友评论