【日志框架】以时间为单位描述应用项目运行状态:用户下线、接口超时、数据库崩溃等等一系列事件
【日志框架能力】
1.定制输出格式
2.定制输出目标
3.携带 Context 比如 HelloWorld.class 这个携带包名
【选择日志框架】SLF4J LOGBACK 原作者推荐,Log4j2性能最好但其他框架支持比较少。 (淘汰JCL JUL JBOSS_LOG Log4j)
…………………………………………………………………………………………………………………………………………………………
【省时神器 Lombok】Automatic Resource Management, automatic generation of getters, setters, equals, hashCode and toString, and more!
@Slf4j
class
method
log.warn("warning content name {} password {}",name,password);
【Logback 配置】 locback-spring.yml (相对于内聚性 application.xml 更自由开放)
1.区分 info 和 error 日志
2.周期性地打日志
【配置 Level】这里我一直误解和 Android的logcat日志级别一样,实际上是这样
public static final int OFF_INT = Integer.MAX_VALUE;
public static final int ERROR_INT = 40000;
public static final int WARN_INT = 30000;
public static final int INFO_INT = 20000;
public static final int DEBUG_INT = 10000; //当我们选到这一级 可见 DEBUG 和 Trace 级别日志
public static final int TRACE_INT = 5000;
public static final int ALL_INT = Integer.MIN_VALUE;
【将 info error 分开】 tail -f file_name.log 获得
<?xml version="1.0" encoding="UTF-8" ?> <configuration> <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern> %d - %msg%n </pattern> </layout> </appender> <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <!--是 ERROR 级别的扔掉--> <onMatch>DENY</onMatch> <!--不是 ERROR 级别的接纳--> <onMismatch>ACCEPT</onMismatch> </filter> <encoder> <pattern> %msg%n </pattern> </encoder> <!--滚动策略 每天一个日志文件--> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>/var/log/tomcat/sell/info.%d.log</fileNamePattern> </rollingPolicy> </appender> <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>ERROR</level> </filter> <encoder> <pattern> %msg%n </pattern> </encoder> <!--滚动策略--> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>/var/log/tomcat/sell/error.%d.log</fileNamePattern> </rollingPolicy> </appender> <root level="info"> <appender-ref ref="consoleLog"/> <appender-ref ref="fileInfoLog"/> <appender-ref ref="fileErrorLog"/> </root> </configuration>
………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………