Java 使用 log4j 创建多个不同内容的日志文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/728295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating multiple log files of different content with log4j
提问by
Is there a way to configure log4j so that it outputs different levels of logging to different appenders?
有没有办法配置 log4j 以便它向不同的 appender 输出不同级别的日志记录?
I'm trying to set up multiple log files. The main log file would catch all INFO and above messages for all classes. (In development, it would catch all DEBUG and above messages, and TRACE for specific classes.)
我正在尝试设置多个日志文件。主日志文件将捕获所有类的所有 INFO 及以上消息。(在开发中,它将捕获所有 DEBUG 及以上消息,并针对特定类进行 TRACE。)
Then, I would like to have a separate log file. That log file would catch all DEBUG messages for a specific subset of classes, and ignore all messages for any other class.
然后,我想要一个单独的日志文件。该日志文件将捕获特定类子集的所有调试消息,并忽略任何其他类的所有消息。
Is there a way to get what I'm after?
有没有办法得到我所追求的?
Thanks, Dan
谢谢,丹
回答by matt b
For the main logfile/appender, set up a .Threshold = INFO
to limit what is actually logged in the appender to INFO and above, regardless of whether or not the loggers have DEBUG, TRACE, etc, enabled.
对于主日志文件/附加程序,设置 a.Threshold = INFO
以将附加程序中实际记录的内容限制为 INFO 及以上,无论记录器是否启用了调试、跟踪等。
As for catching DEBUG and nothing above that... you'd probably have to write a custom appender.
至于捕获 DEBUG 以及除此之外的任何内容……您可能必须编写自定义附加程序。
However I'd recommend not doing this, as it sounds like it would make troubleshooting and analysis pretty hard:
但是我建议不要这样做,因为这听起来会使故障排除和分析变得非常困难:
- If your goal is to have a single file where you can look to troubleshoot something, then spanning your log data across different files will be annoying - unless you have a very regimented logging policy, you'll likely need content from both DEBUG and INFO to be able to trace execution of the problematic code effectively.
- By still logging all of your debug messages, you are losing any performance gains you usually get in a production system by turning the logging (way) down.
- 如果您的目标是拥有一个文件,您可以在其中查找故障排除,那么跨越不同文件的日志数据将很烦人 - 除非您有非常严格的日志记录策略,否则您可能需要来自 DEBUG 和 INFO 的内容能够有效地跟踪有问题的代码的执行。
- 通过仍然记录所有调试消息,您将失去通常通过关闭日志记录(方式)在生产系统中获得的任何性能提升。
回答by jasonnerothin
This should get you started:
这应该让你开始:
log4j.rootLogger=QuietAppender, LoudAppender, TRACE
# setup A1
log4j.appender.QuietAppender=org.apache.log4j.RollingFileAppender
log4j.appender.QuietAppender.Threshold=INFO
log4j.appender.QuietAppender.File=quiet.log
...
# setup A2
log4j.appender.LoudAppender=org.apache.log4j.RollingFileAppender
log4j.appender.LoudAppender.Threshold=DEBUG
log4j.appender.LoudAppender.File=loud.log
...
log4j.logger.com.yourpackage.yourclazz=TRACE
回答by araqnid
Perhaps something like this?
也许像这样?
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- general application log -->
<appender name="MainLogFile" class="org.apache.log4j.FileAppender">
<param name="File" value="server.log" />
<param name="Threshold" value="INFO" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %t [%-40.40c] %x - %m%n"/>
</layout>
</appender>
<!-- additional fooSystem logging -->
<appender name="FooLogFile" class="org.apache.log4j.FileAppender">
<param name="File" value="foo.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %t [%-40.40c] %x - %m%n"/>
</layout>
</appender>
<!-- foo logging -->
<logger name="com.example.foo">
<level value="DEBUG"/>
<appender-ref ref="FooLogFile"/>
</logger>
<!-- default logging -->
<root>
<level value="INFO"/>
<appender-ref ref="MainLogFile"/>
</root>
</log4j:configuration>
Thus, all info messages are written to server.log; by contrast, foo.log contains only com.example.foo messages, including debug-level messages.
因此,所有信息消息都写入 server.log;相比之下, foo.log 只包含 com.example.foo 消息,包括调试级别的消息。
回答by Kieveli
I had this question, but with a twist - I was trying to log different content to different files. I had information for a LowLevel debug log, and a HighLevel user log. I wanted the LowLevel to go to only one file, and the HighLevel to go to both a file, and a syslogd.
我有这个问题,但有一个转折 - 我试图将不同的内容记录到不同的文件中。我有关于 LowLevel 调试日志和 HighLevel 用户日志的信息。我希望 LowLevel 只访问一个文件,HighLevel 访问一个文件和一个 syslogd。
My solution was to configure the 3 appenders, and then setup the logging like this:
我的解决方案是配置 3 个 appender,然后像这样设置日志记录:
log4j.threshold=ALL
log4j.rootLogger=,LowLogger
log4j.logger.HighLevel=ALL,Syslog,HighLogger
log4j.additivity.HighLevel=false
The part that was difficult for me to figure out was that the 'log4j.logger' could have multiple appenders listed. I was trying to do it one line at a time.
我很难弄清楚的部分是“log4j.logger”可以列出多个附加程序。我试图一次做一行。
Hope this helps someone at some point!
希望这在某些时候可以帮助某人!
回答by Rzv Razvan
Demo link: https://github.com/RazvanSebastian/spring_multiple_log_files_demo.git
演示链接:https: //github.com/RazvanSebastian/spring_multiple_log_files_demo.git
My solution is based on XML configuration using spring-boot-starter-log4j
. The example is a basic example using spring-boot-starter
and the two Loggers writes into different log files.
我的解决方案基于 XML 配置,使用spring-boot-starter-log4j
. 该示例是使用spring-boot-starter
两个 Logger 写入不同日志文件的基本示例。