Log4j使用

最近版本的Log4j,使用maven,导入两个jar包,依赖如下:

  1. org.apache.logging.log4j
  2. log4j-api
  3. 2.6.2
  4. org.apache.logging.log4j
  5. log4j-core
  6. 2.6.2

然后在自己的resource文件下,新建

log4j.properties,运行不了。


修改依赖文件:

       
            log4j
            log4j
            1.2.17
       

   


然后就可以了,不同的的版本不一样,新版本的用法要继续学习。

然后配置文件详解:

log4j.rootLogger = INFO, File, stdout   rootLogger是设置日志级别和输出端的地方。 比如这句,
日志级别是INFO, 输出地方WieFile  和 stdout两个地方。

然后在下面要配置输出地方的位置,格式,比如下面:

Here, we use two appenders, one appender to log to a file and another one to log to the console. The first appender uses the org.apache.log4j.FileAppender and the second one the org.apache.log4j.ConsoleAppender. The first appender has a fileattribute, where the logging file is set, whereas the second one has a target attribute which is set to System.out. Both appenders use a layout for their log files. The org.apache.log4j.PatternLayout is used and the ConversionPattern is set to%d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n:

%d{dd/MMM/yyyy HH:mm:ss,SSS} is used to set the date pattern, %c{1} is used to print the class name, %m to print the message, and %n to leave an empty line.



log4j.appender.File=org.apache.log4j.FileAppender
log4j.appender.File.File=C:\\logs\\logs.log
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.conversionPattern=%d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n

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{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n




你可能感兴趣的:(Log4j使用)