日志系统
1 log4j
对于log4j需要一个配置信息
log4j.rootLogger=DEBUG, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n
2 Logback
引入文件
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency>
这个是log4j的改良版本,和log4j是同一个人创作的。其配置要比log4j更复杂一点,但功能也更强大,以下便是配置了debug info和error三个级别的日志系统,并在控制台上输出info的日志
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true" scanPeriod="60 seconds" debug="false"> <!--定义参数常量 日志级别:TRACE<DEBUG<INFO<WARN<ERROR常用的一般是debug info error --> <!--配置日志级别--> <property name="log.level" value="debug"/> <!--设定日志保存时间--> <property name="log.maxHistory" value="30"/> <!--设定日志保存目录--> <property name="log.filePath" value="${catalina.base}/logs/webapps"/> <!--日志输出格式设定--> <property name="log.pattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS}[%thread] %-5level %logger{50} - %msg%n"/> <!--控制台设置--> <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>${log.pattern}</pattern> </encoder> </appender> <!--DEBUG--> <appender name="debugAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!--文件路径--> <file>${log.filePath}/debug.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--文件名称--> <fileNamePattern>${log.filePath}/debug/debug.%d{yyyy-MM-dd}.log.gz</fileNamePattern> <!--文件最大保存历史数量--> <maxHistory>${log.maxHistory}</maxHistory> </rollingPolicy> <encoder> <pattern>${log.pattern}</pattern> </encoder> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>DEBUG</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <!--INFO--> <appender name="infoAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!--文件路径--> <file>${log.filePath}/info.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--文件名称--> <fileNamePattern>${log.filePath}/info/info.%d{yyyy-MM-dd}.log.gz</fileNamePattern> <!--文件最大保存历史数量--> <maxHistory>${log.maxHistory}</maxHistory> </rollingPolicy> <encoder> <pattern>${log.pattern}</pattern> </encoder> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <!--INFO--> <appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!--文件路径--> <file>${log.filePath}/error.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--文件名称--> <fileNamePattern>${log.filePath}/error/error.%d{yyyy-MM-dd}.log.gz</fileNamePattern> <!--文件最大保存历史数量--> <maxHistory>${log.maxHistory}</maxHistory> </rollingPolicy> <encoder> <pattern>${log.pattern}</pattern> </encoder> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <!--additivity="true"info也可以在上面的日志中输出--> <logger name="cn.uestc.o2o" level="${log.level}" additivity="true"> <appender-ref ref="debugAppender"/> <appender-ref ref="infoAppender"/> <appender-ref ref="errorAppender"/> </logger> <root level="info"> <appender-ref ref="consoleAppender"/> </root> </configuration>
要注意的是对于同一配置文件,在Ecplise和IDEA中控制台的输出可能是不同的这和运行环境有关。