log4j sample configuration file (properties fi

2019-02-06 22:01发布

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

6条回答
Summer. ? 凉城
2楼-- · 2019-02-06 22:36

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

查看更多
迷人小祖宗
3楼-- · 2019-02-06 22:37
# 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
查看更多
相关推荐>>
4楼-- · 2019-02-06 22:41

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(大扎)
5楼-- · 2019-02-06 22:46

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.

查看更多
等我变得足够好
6楼-- · 2019-02-06 22:46

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.

查看更多
戒情不戒烟
7楼-- · 2019-02-06 22:49

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.

查看更多
登录 后发表回答