[英]log4j sample configuration file (properties file)
开始使用log4j配置的最简单方法是什么?
Put a file named log4j.properties
in the root of your classpath: 将名为log4j.properties
的文件放在类路径的根目录中:
log4j.rootLogger = ALL, Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.conversionPattern=%m%n
Nothing else is needed. 不需要任何其他东西。 Log4j will discover it and configure itself. Log4j将发现它并进行自我配置。
The absolute easiest way is to visit the log4j pages at apache and read the short introduction . 绝对最简单的方法是访问apache的log4j页面并阅读简短的介绍 。 They have a sample log4j.configuration ready to be copied and pasted. 他们有一个示例log4j.configuration可以复制和粘贴。
In case you stumble into this and are looking for a sample file for log4j2. 万一你偶然发现并正在寻找log4j2的示例文件。 The way I got it to work was to create a file names log4j2.xml in the base 'resources' directory (I'm using maven so that was 'src/main/resources') 我让它工作的方式是在基础'resources'目录中创建一个文件名log4j2.xml(我正在使用maven,因此它是'src / main / resources')
Then copy the sample configuration from the manual: http://logging.apache.org/log4j/2.x/manual/configuration.html 然后从手册中复制示例配置: http : //logging.apache.org/log4j/2.x/manual/configuration.html
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
This will give you a nice simple console logger. 这将为您提供一个简单的控制台记录器。 I recommend you modify the pattern to how you want it to look and the 'Root level=' to something more inclusive. 我建议您将模式修改为您希望它的外观,将“Root level =”修改为更具包容性的内容。 And of course, read the manual for more powerful settings... 当然,阅读手册以获得更强大的设置......
In addition to some other answers, I would add a persistence appender since that's the greatest advantage of using logs over consoles and debuggers; 除了其他一些答案,我还会添加一个持久性appender,因为这是在控制台和调试器上使用日志的最大优势; when one can't run through the application code in real-time or the event already occurred. 当一个人无法实时运行应用程序代码或事件已经发生时。
!/"path"/"filename" will write to root of filesystem. !/“path”/“filename”将写入文件系统的根目录。 "path"/"filename" will write to path relative to classpath root. “path”/“filename”将写入相对于类路径根的路径。
log4j.rootLogger = ALL, Console, default.file
log4j.appender.default.file=org.apache.log4j.FileAppender
log4j.appender.default.file.file={path}/{filename}
log4j.appender.default.file.layout=org.apache.log4j.PatternLayout
log4j.appender.default.file.layout.conversionPattern=%m%n
log4j.appender.Console=org.apache.log4j.ConsoleAppender
...
# Root logger option
log4j.rootLogger=DEBUG, stdout, file
# Redirect log messages to console
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{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Redirect log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
#outputs to Tomcat home
log4j.appender.file.File=${catalina.home}/logs/myapp.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.