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

发布于 2024-08-19 04:54:11 字数 30 浏览 3 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

咋地 2024-08-26 04:54:11

将名为 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

不需要其他任何东西。 Log4j 会发现它并自行配置。

Put a file named log4j.properties in the root of your classpath:

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.

一向肩并 2024-08-26 04:54:11

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

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.

む无字情书 2024-08-26 04:54:11

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

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.

夏天碎花小短裙 2024-08-26 04:54:11

如果您偶然发现此问题并正在寻找 log4j2 的示例文件。我让它工作的方法是在基本“资源”目录中创建一个文件名 log4j2.xml (我使用的是 Maven,所以它是“src/main/resources”)

然后从手册中复制示例配置:
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>

这将为您提供一个漂亮的简单控制台记录器。我建议您将模式修改为您想要的外观,并将“根级别=”修改为更具包容性的内容。当然,请阅读手册以获取更强大的设置......

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')

Then copy the sample configuration from the manual:
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...

不知在何时 2024-08-26 04:54:11

除了其他一些答案之外,我还会添加一个持久性附加程序,因为这是使用日志而不是控制台和调试器的最大优势;当无法实时运行应用程序代码或事件已经发生时。

!/"path"/"filename" 将写入文件系统的根目录。 “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
...

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.

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
...
深府石板幽径 2024-08-26 04:54:11
# 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
# 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文