日期:2014-05-20  浏览次数:20779 次

log4j不能按天生成文件
大家帮我看下,这个配置刚开始还能按天输出log 后来就不会按天生成了 
帮帮小弟看看什么问题~~~


Java code
# A default log4j configuration for log4j users.
#
# To use this configuration, deploy it into your application's WEB-INF/classes
# directory.  You are also encouraged to edit it as you like.

# Configure the console as our one appender
#log4j.rootLogger=DEBUG,stdout
#log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern=%d %5p (%F:%L) - %m%n

log4j.rootLogger = debug,stdout,D,E
### 输出到控制台 ###
log4j.appender.Console.Encoding=UTF-8 
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{ABSOLUTE} [%5p] %c{1}:%L - %m%n

### 输出到日志文件 ###

log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.D.File = e:/shgj04logs/log.log
log4j.appender.D.Append = true
## 输出DEBUG级别以上的日志

log4j.appender.D.Threshold = INFO 
log4j.appender.D.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} %l - [%p] %m%n

### 保存异常信息到单独文件 ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
## 异常日志文件名

log4j.appender.E.File = e:/shgj04logs/errorLog.log 
log4j.appender.E.Append = true
## 只输出ERROR级别以上的日志!!!
log4j.appender.E.Threshold = ERROR 
log4j.appender.E.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} %l - [%p] %m%n 





------解决方案--------------------
建议采用 XML 方式进行配置,下面这个久用不衰,可以用在各种工程中!

XML code
<?xml version="1.0" encoding="GBK" ?>

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
  threshold="debug">

  <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%t] [%c{1}] %m%n" />
    </layout>
  </appender>
  
  <appender name="ASYNC_CONSOLE" class="org.apache.log4j.AsyncAppender">
    <param name="BufferSize" value="8192"/>
    <appender-ref ref="CONSOLE" />
  </appender>

  <appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="File" value="/home/x/log/server.log" />
    <param name="Append" value="true" />
    <param name="DatePattern" value="'.'yyyy-MM-dd" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%t] [%c{1}] %m%n" />
    </layout>
  </appender>

  <logger name="com.test">
    <level value="DEBUG" />
  </logger>

  <root>
    <level value="INFO" />
    <appender-ref ref="FILE" />
  </root>
</log4j:configuration>