使log4j将相同的日志写入多个文件

时间:2015-09-18 15:45:52

标签: java logging jboss log4j

这个配置的漂亮菜鸟;但是我在lo4j.xml中尝试了下面的配置它不起作用。我想将相同的日志写入两个不同的文件。

 <appender name="FILE" class="com.compname.apt.appender.CustomAppender">
    <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
    <param name="File" value="/home/c/logs/jboss/server.log"/>
    <param name="Append" value="true"/>
    <param name="Threshold" value="INFO"/>
    <param name="BufferedIO" value="false"/>
    <param name="BufferSize" value="8096"/>
    <!-- Rollover at midnight each day by default -->
    <param name="DatePattern" value="'.'yyyy-MM-dd"/>
    <layout class="org.apache.log4j.PatternLayout">
      <!-- The default pattern: Date Priority [Category] Message -->
      <param name="ConversionPattern" value="'[%d] - %x %-5p [%c] %m%n'"/>
    </layout>
  </appender>
  <appender name="flumeappender" class="com.compname.apt.appender.CustomAppender">
    <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
    <param name="File" value="/home/y/logs/jboss/flume_ews.log"/>
    <param name="Append" value="true"/>
    <param name="Threshold" value="INFO"/>
    <param name="BufferedIO" value="false"/>
    <param name="BufferSize" value="8096"/>
    <!-- Rollover at midnight each day by default -->
    <param name="DatePattern" value="'.'yyyy-MM-dd"/>
    <layout class="org.apache.log4j.PatternLayout">
      <!-- The default pattern: Date Priority [Category] Message -->
      <param name="ConversionPattern" value="'[%d] - %x %-5p [%c] %m%n'"/>
    </layout>
  </appender>

1 个答案:

答案 0 :(得分:3)

这是log4j的简单演示。希望这是你需要的东西。

#Destination
log4j.rootLogger=DEBUG, toConsole, toFile, DB

#Console
log4j.appender.toConsole=org.apache.log4j.ConsoleAppender
log4j.appender.toConsole.layout=org.apache.log4j.PatternLayout
log4j.appender.toConsole.layout.ConversionPattern=%d(HH:mm:ss) %5p [%t] - %c.%M - %m%n
#%x: Id, %d: Date, %C: Content,%p: Level, %m: Message
#log4j.appender.toConsole.layout.ConversionPattern=%x %d %C %p %m%n 
#https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html

#DailyRollingFile
log4j.appender.toFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.toFile.File=./log/logfile.txt
log4j.appender.toFile.DatePattern='.' yyyy-MM-dd
log4j.appender.toFile.layout=org.apache.log4j.PatternLayout
log4j.appender.toFile.layout.ConversionPattern=%d(HH:mm:ss) %5p [%t] - %c.%M - %m%n
#log4j.appender.toFile.layout.ConversionPattern=%x %d %C %p %m%n 

#JDBCAppender
# Define the file appender
log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.DB.URL=jdbc:sqlserver://localhost:1433;databaseName=Trainees
# Set Database Driver
log4j.appender.DB.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
# Set database user name and password
log4j.appender.DB.user=sa
log4j.appender.DB.password=123456
# Set the SQL statement to be executed.
log4j.appender.DB.sql=INSERT INTO Logs(A,B,C,D) VALUES('%d(HH:mm:ss)', '%5p [%t]', '%c.%M', '%m%n')
# Define the xml layout for file appender
log4j.appender.DB.layout=org.apache.log4j.PatternLayout