Java log4j 示例配置文件(属性文件)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2128185/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:52:14  来源:igfitidea点击:

log4j sample configuration file (properties file)

javalogginglog4j

提问by flybywire

What is the easiest way to get started with log4j configuration?

开始使用 log4j 配置的最简单方法是什么?

采纳答案by flybywire

Put a file named log4j.propertiesin 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 会发现它并自行配置。

回答by Brian Agnew

It's worth reading the manual(at the risk of stating the obvious). There are a ton of configuration options, and once you learn and understand what's possible, then you can implement some very powerful logging systems.

值得阅读手册(冒着说明明显的风险)。有大量的配置选项,一旦您了解并理解了哪些可能,那么您就可以实现一些非常强大的日志系统。

回答by Andreas Dolk

The absolute easiest way is to visit the log4j pages at apache and read the short introduction. They have a sample log4j.configuration ready to be copied and pasted.

最简单的方法是访问 apache 的 log4j 页面并阅读简短的介绍。他们有一个示例 log4j.configuration 可以复制和粘贴。

回答by ThinkBonobo

In case you stumble into this and are looking for a sample file for 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')

如果您偶然发现并正在寻找 log4j2 的示例文件。我让它工作的方式是在基本'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. And of course, read the manual for more powerful settings...

这将为您提供一个不错的简单控制台记录器。我建议您将模式修改为您想要的样子,并将“Root level=”修改为更具包容性的内容。当然,阅读手册以获得更强大的设置......

回答by ilupper

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; 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" 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
...

回答by Rohan C Bandara

# 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