日志的配置方式我一般接触到的有三种,一种是xml,一种是properties,还有一种是yml;
方式一 xml
引入依赖
<dependencies>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
配置log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/' >
<appender name="myConsole" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd HH:mm:ss,SSS\} %-5p] [%t] %c{2\} - %m%n" />
</layout>
<!--过滤器设置输出的级别(INFO-WARN),将levelMin与levelMax设置为相同的值,则可实现只输出某个级别的日志-->
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="INFO" />
<param name="levelMax" value="WARN" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<appender name="myFile" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="D:/output.log" /><!-- 设置日志输出文件路径(包含文件名) -->
<!-- 设置是否在重新启动服务时,在原有日志的基础上追加新日志 -->
<param name="Append" value="true" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%p (%c:%L)- %m%n" />
</layout>
</appender>
<appender name="activexAppender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="E:/activex.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{MMdd HH:mm:ss SSS\} %-5p] [%t] %c{3\} - %m%n" />
</layout>
</appender>
<!-- 一个logger子元素定义一个日志写出器,指定logger的设置,定义只打印某个包或其下某个类的某种级别的日志,additivity指示是否遵循缺省的继承机制-->
<logger name="com.runway.bssp.activeXdemo" additivity="false">
<priority value ="info"/>
<appender-ref ref="activexAppender" />
</logger>
<!-- 根logger的设置-->
<root>
<priority value ="debug"/>
<appender-ref ref="myConsole"/>
<appender-ref ref="myFile"/>
</root>
</log4j:configuration>
方式 2 properties
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
配置文件:
log4j.rootLogger=DEBUG,Console,File
# Console - 将日志打印到控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
# File - 将日志打印至指定的文件中
log4j.appender.File=org.apache.log4j.FileAppender
log4j.appender.File.File=本地路径\\appender-log.log
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
方式三:
Configuration:
status: warn #日志级别 有高到低 ERROR、WARN、INFO、DEBUG、TRACE
monitorInterval: 30 #30秒检查一次
properties:
property:
- name: LOG_HOME
value: logs/
- name: FILE_NAME
value: test.log
- name: FILE_ERROR_NAME
value: test_error.log
Appenders:
Console: #控制台输出
name: Console
target: SYSTEM_OUT
PatternLayout: #输出格式
pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
# File: #文件会打印出所有信息,这个log每次运行程序会自动清空,由append属性决定,这个也挺有用的,适合临时测试用
# name: log
# fileName: log/test.log
# append: true
# PatternLayout: #输出格式
# pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
RollingFile:
- name: SYSTEMLOG #系统日志
fileName: ${LOG_HOME}${FILE_NAME}
filePattern: "${LOG_HOME}$${date:yyyy-MM}/${FILE_NAME}-%d{yyyy-MM-dd}-%i.log.gz"
PatternLayout:
pattern: "%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
Filters:
ThresholdFilter: #过滤debug后的
level: DEBUG
onMatch: ACCEPT
onMismatch: DENY
Policies:
TimeBasedTriggeringPolicy: #按天分类
modulate: true
interval: 1
SizeBasedTriggeringPolicy: #文件大小
size: 300MB
DefaultRolloverStrategy: #目录下文件最多个数
max: 20
- name: ERRORLOG #错误日志
fileName: ${LOG_HOME}${FILE_ERROR_NAME}
filePattern: "${LOG_HOME}$${date:yyyy-MM}/${FILE_ERROR_NAME}-%d{yyyy-MM-dd}-%i.log.gz"
PatternLayout:
pattern: "%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
Filters:
ThresholdFilter: #过滤error后的
level: error
onMatch: ACCEPT
onMismatch: DENY
Policies:
TimeBasedTriggeringPolicy: #按天分类
modulate: true
interval: 1
SizeBasedTriggeringPolicy: #文件大小
size: 300MB
DefaultRolloverStrategy: #目录下文件最多个数
max: 20
Loggers:
Root:
level: info
AppenderRef:
ref: Console
Logger:
- name: com.weirdo.web
level: debug
AppenderRef:
- ref: CONSOLE
- ref: SYSTEMLOG
- ref: ERRORLOG