commons-logging日志接口API
Log4j日志实现API
优先级
(高)Fatal -> Error -> Warn -> Info -> Debug -> Trace(低)
优先级 越低、输出的日志信息会越多
日志输出目的地
Appender日志输出目的地
- ConsoleAppender
- FileAppender
- RollingFileAppender
log4j2.xml 配置
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<!-- 定义日志格式 -->
<Property name="log.pattern">%d{MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36}%n%msg%n%n</Property>
<!-- 定义文件名变量 -->
<Property name="file.err.filename">log/err.log</Property>
<Property name="file.err.pattern">log/err.%i.log.gz</Property>
</Properties>
<!-- 定义Appender,即目的地 -->
<Appenders>
<!-- 定义输出到屏幕 -->
<Console name="console" target="SYSTEM_OUT">
<!-- 日志格式引用上面定义的log.pattern -->
<PatternLayout pattern="${log.pattern}" />
</Console>
<!-- 定义输出到文件,文件名引用上面定义的file.err.filename -->
<RollingFile name="err" bufferedIO="true" fileName="${file.err.filename}" filePattern="${file.err.pattern}">
<PatternLayout pattern="${log.pattern}" />
<Policies>
<!-- 根据文件大小自动切割日志 -->
<SizeBasedTriggeringPolicy size="1 MB" />
</Policies>
<!-- 保留最近10份 -->
<DefaultRolloverStrategy max="10" />
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<!-- 对info级别的日志,输出到console -->
<AppenderRef ref="console" level="info" />
<!-- 对error级别的日志,输出到err,即上面定义的RollingFile -->
<AppenderRef ref="err" level="error" />
</Root>
</Loggers>
</Configuration>
slf4j + log4j
log4j.properties
log4j.rootLogger=debug, console , email, fileS, jdbc
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.out
log4j.appender.console.encoding=UTF-8
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%d{ISO8601}] %c %replace{%m}{'}{"}%n
log4j.appender.jdbc=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.jdbc.driver=com.mysql.jdbc.Driver
log4j.appender.jdbc.URL=jdbc:mysql://127.0.0.1:3306/test
log4j.appender.jdbc.user=root
log4j.appender.jdbc.password=root
log4j.appender.jdbc.sql=insert into t_logger(msg) values(%m)
log4j.appender.email=org.apache.log4j.net.SMTPAppender
log4j.appender.email.SMTPHost=smtp.qq.com
log4j.appender.email.SMTPUsername=472759903@qq.com
log4j.appender.email.SMTPPassword=psvykbpvciowbgic
log4j.appender.email.subject=系统异常日志
log4j.appender.email.from=472759903@qq.com
log4j.appender.email.to=472759903@qq.com
log4j.appender.email.layout=org.apache.log4j.HTMLLayout
log4j.appender.fileS=org.apache.log4j.RollingFileAppender
log4j.appender.fileS.file=log/err1.log
log4j.appender.fileS.maxFileSize=10485760
log4j.appender.fileS.layout=org.apache.log4j.PatternLayout
log4j.appender.fileS.layout.ConversionPattern=[%d{ISO8601}] %c %replace{%m}{'}{"}%n
log4j.logger.com.soft.qikux = info