开发者

log4j sample configuration file (properties file)

开发者 https://www.devze.com 2022-12-18 03:58 出处:网络
What is the easiest way to get started with log4j configura开发者_StackOverflow中文版tion?Put a file named log4j.properties in the root of your classpath:

What is the easiest way to get started with log4j configura开发者_StackOverflow中文版tion?


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.


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.


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.


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


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


# 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
0

精彩评论

暂无评论...
验证码 换一张
取 消