这是我的log4j.xml文件:
<log4j:configuration> <appender name="async" class="org.apache.log4j.AsyncAppender"> <!-- this parameter need to be set to false to avoid application from hanging. --> <param name="Blocking" value="false" /> <appender-ref ref="myAppender" /> </appender> <appender name="myAppender" class="org.apache.log4j.ConsoleAppender"> <param name="Threshold" value="INFO" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-5p %d{ISO8601} [%t] %c %x - %m%n" /> </layout> </appender> <logger name="com.server"> <level value="INFO" /> </logger> <logger name="org.springframework"> <level value="INFO" /> </logger> <logger name="org.hibernate.LazyInitializationException" additivity="false"> <level value="off" /> <appender-ref ref="async" /> </logger> <logger name="net.sf.ehcache"> <level value="INFO" /> </logger> <logger name="com.mchange"> <level value="INFO" /> </logger> <root> <priority value="INFO" /> <appender-ref ref="async" /> </root> </log4j:configuration>
这里有一些示例输出:
INFO 2016-08-26 11:01:38,353 [main] com.server.Server - Server started successfully... 11:01:38,353 INFO : Server started successfully...
编辑:当我将“myAppender”appender阈值更改为“ERROR”时,显示的第二个日志消息(从时间开始,而不是“INFO”)仍在生成.有没有一些我需要禁用的默认记录器?似乎有些东西仍然记录这些“INFO”级别的消息,即使指定的appender是用于“ERROR”级消息.另外,如果我注释掉整个log4j.xml文件,第二个日志消息(和所有喜欢的)仍然被记录.我该如何防止这种情况?谢谢!
解决方法
首先尝试使用下面的根记录器配置而不是你的< appender-ref ref =“async”/> –
<logger name="org.hibernate.LazyInitializationException" additivity="false"> <level value="off" /> <appender-ref ref="myAppender" /> </logger> .............. ........... <root> <priority value="INFO" /> <appender-ref ref="myAppender" /> </root>
如果以上方法不起作用,则尝试将< param name =“Threshold”value =“ERROR”/>无论是在async和myAppender,我猜没有服务器启动成功日志将被打印(我想你已经尝试与myAppender)
如果这有帮助,那么这意味着您尝试引用/继承记录器的方式不正常或正确使用,所以您从该方向思考并纠正问题,您需要阅读有关appender-ref元素和AsyncAppender记录器的更多信息.
无论以上是否有效,我猜你试图实现以下目标 –
>打印您的标准输入/输出,即您的终端窗口
>打印文件以及prod环境
我将在log4j.properties中执行以下操作:
log4j.logger.com.sks.cs50=DEBUG,CS50GlobalFileAppender,stdout #####CS50 Web log########## log4j.appender.CS50GlobalFileAppender=org.apache.log4j.RollingFileAppender log4j.appender.CS50GlobalFileAppender.File=${logDir}cs50.log log4j.appender.CS50GlobalFileAppender.MaxBackupIndex=50 log4j.appender.CS50GlobalFileAppender.Threshold=DEBUG log4j.appender.CS50GlobalFileAppender.layout=org.apache.log4j.PatternLayout log4j.appender.CS50GlobalFileAppender.layout.ConversionPattern=%C %d [%t] [%X{randomNumber}] %-5p - %m%n log4j.appender.CS50GlobalFileAppender.MaxFileSize=500000KB # Log format to standard output 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=%d [%X{randomNumber}] %-5p - %m%n log4j.logger.org.apache = INFO log4j.logger.com.sun = INFO log4j.logger.sun = INFO log4j.logger.javax.xml.bind = INFO log4j.logger.org.springframework = INFO log4j.logger.com.cgi = DEBUG
所以,我想你可以做一些如下的事情.
<log4j:configuration> <appender name="async" class="org.apache.log4j.AsyncAppender"> <!-- this parameter need to be set to false to avoid application from hanging. --> <param name="Blocking" value="false" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-5p %d{ISO8601} [%t] %c %x - %m%n" /> </layout> </appender> <appender name="myAppender" class="org.apache.log4j.ConsoleAppender"> <param name="Threshold" value="INFO" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-5p %d{ISO8601} [%t] %c %x - %m%n" /> </layout> </appender> <logger name="com.server"> <level value="INFO" /> </logger> <logger name="org.springframework"> <level value="INFO" /> </logger> <logger name="org.hibernate.LazyInitializationException" additivity="false"> <level value="off" /> <appender-ref ref="async" /> </logger> <logger name="net.sf.ehcache"> <level value="INFO" /> </logger> <logger name="com.mchange"> <level value="INFO" /> </logger> <root> <priority value="INFO" /> <appender-ref ref="async" /> <appender-ref ref="myAppender" /> </root> </log4j:configuration>
请注意,以上XML配置是假定org.apache.log4j.AsyncAppender打印在某些日志文件中,但如果没有(我猜测它不是),那么上面的方法将不起作用(您仍然可以使用以上XML配置来实现文件控制台日志记录,如果您使用org.apache.log4j.RollingFileAppender替换AsyncAppender),并且我想知道为什么只能不能直接使用org.apache.log4j.AsyncAppender,因为它已经打印到控制台,所以可能只是使用:
<appender name="async" class="org.apache.log4j.AsyncAppender"> <!-- this parameter need to be set to false to avoid application from hanging. --> <param name="Blocking" value="false" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-5p %d{ISO8601} [%t] %c %x - %m%n" /> </layout> </appender>