概述
本文基于logback实现web项目(采用Struts2框架)的日志管理,设置彩色日志,根据日结级别还有日期划分日志文件
一般是springboot项目会集成,因为springboot中有已经定义好的色彩转换类,此处没有则自定义。
依赖
jar
web项目, 需要在 web/INF-WEB/lib
下添加以下依赖包
1 2 3
| logback-classic-1.2.3.jar logback-core-1.2.3.jar slf4j-api-1.7.32.jar
|
maven
如果是maven
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <dependencies> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency>
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.32</version> </dependency> </dependencies>
|
配置
拦截器
创建拦截器,在请求到时,先经过此处获取请求信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
public class GlobalLoggingInterceptor extends MethodFilterInterceptor{
@Override protected String doIntercept(ActionInvocation invocation) throws Exception { String actionName = invocation.getAction().getClass().getSimpleName(); String methodName = invocation.getProxy().getMethod();
HttpServletRequest request = ServletActionContext.getRequest();
String requestURI = request.getRequestURI(); String requestMethod = request.getMethod();
String queryParams = request.getQueryString();
String ipAddress = request.getRemoteAddr();
String userAgent = request.getHeader("User-Agent");
MDC.put("requestURI", requestURI); MDC.put("requestMethod", requestMethod); MDC.put("action", actionName); MDC.put("method", methodName); MDC.put("queryParams", queryParams != null ? queryParams : "N/A"); MDC.put("ipAddress", ipAddress); MDC.put("userAgent", userAgent);
String result = invocation.invoke();
MDC.clear();
return result; } }
|
struts.xml
添加日志拦截器
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| .... <package name="login" extends="struts-default"> <interceptors> <interceptor name="globalLoggingInterceptor" class="com.yisurvey.filter.GlobalLoggingInterceptor"/> <interceptor-stack name="authority"> <interceptor-ref name="globalLoggingInterceptor"/> <interceptor-ref name="otherInterceptor"/> <interceptor-ref name="defaultStack"/> </interceptor-stack> </interceptors> </package> ...
|
自定义彩色转换器
控制台日志中级别设置颜色
LogLevelColorfulConverter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
public class LogLevelColorfulConverter<E> extends CompositeConverter<E> {
@Override protected String transform(E event, String in) { if (in.contains("INFO")) { return "\u001B[32m" + in + "\u001B[0m"; } else if (in.contains("ERROR")) { return "\u001B[31m" + in + "\u001B[0m"; } else if (in.contains("WARN")) { return "\u001B[33m" + in + "\u001B[0m"; } return in; } }
|
MDC 彩色转换器
控制拦截器中获取请求方式,请求头的ip地址,请求的设备号....
统一以紫色打印
LogMDCColorfulConverter
1 2 3 4 5 6 7 8 9 10 11 12
|
public class LogMDCColorfulConverter extends CompositeConverter<ILoggingEvent> {
@Override protected String transform(ILoggingEvent event, String in) { return "\u001B[1;35m" + in + "\u001B[0m"; }
}
|
其他信息转换器
时间等统一以蓝色打印
LogOtherColorfulConverter
1 2 3 4 5 6 7 8 9
|
public class LogOtherColorfulConverter extends CompositeConverter<ILoggingEvent> { @Override protected String transform(ILoggingEvent event, String in) { return "\u001B[1;34m" + in + "\u001B[0m"; } }
|
logback.xml
如果在web项目一般 放在 web/WEB-INF/classes
目录下
如果是springboot项目放在 resource
目录下
下面的配置:控制台会输出INFO 以上的日志(等级高到低 ERROR > WARN
> INFO > DEBUG), 日志写出只会写出INFO跟ERROR
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| <configuration> <property name="log.path" value="D:/logs"/> <conversionRule conversionWord="clr" converterClass="com.yisurvey.config.LogLevelColorfulConverter"/> <conversionRule conversionWord="mdcClr" converterClass="com.yisurvey.config.LogMDCColorfulConverter"/> <conversionRule conversionWord="otClr" converterClass="com.yisurvey.config.LogOtherColorfulConverter"/> <property name="CONSOLE_LOG_PATTERN" value="%otClr(%d{yyyy-MM-dd HH:mm:ss}) %clr(%-5level) %otClr([%thread]) %logger{15} - %msg %mdcClr(%X{requestURI}) %mdcClr(%X{requestMethod}) %mdcClr(%X{action}) %mdcClr(%X{method}) %mdcClr(%X{queryParams}) %mdcClr(%X{ipAddress}) %mdcClr(%X{userAgent}) %n"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>debug</level> </filter> <encoder> <pattern>${CONSOLE_LOG_PATTERN}</pattern> <charset>UTF-8</charset> </encoder> </appender>
<appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] %logger{15} - %msg %X{requestURI} %X{requestMethod} %X{action} %X{method} %X{queryParams} %X{ipAddress} %X{userAgent}%n</pattern> <charset>UTF-8</charset> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.path}/debug/log-debug-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>5</maxHistory> </rollingPolicy> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>debug</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender>
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] %logger{15} - %msg %X{requestURI} %X{requestMethod} %X{action} %X{method} %X{queryParams} %X{ipAddress} %X{userAgent}%n</pattern> <charset>UTF-8</charset> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.path}/info/log-info-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>5</maxHistory> </rollingPolicy> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>info</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender>
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] %logger{15} - %msg %X{requestURI} %X{requestMethod} %X{action} %X{method} %X{queryParams} %X{ipAddress} %X{userAgent}%n</pattern> <charset>UTF-8</charset> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.path}/error/log-error-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>5</maxHistory> </rollingPolicy> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>error</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender>
<appender name="WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] %logger{15} - %msg %X{requestURI} %X{requestMethod} %X{action} %X{method} %X{queryParams} %X{ipAddress} %X{userAgent}%n</pattern> <charset>UTF-8</charset> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.path}/warn/log-warn-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>5</maxHistory> </rollingPolicy> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>warn</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender>
<root level="info"> <appender-ref ref="CONSOLE"/> <appender-ref ref="DEBUG_FILE"/> <appender-ref ref="INFO_FILE"/> <appender-ref ref="ERROR_FILE"/> </root> </configuration>
|
注释的日志归档配置为:按照文件大小再进行下一步划分
注意:如果是springboot项目,上面彩色日志则使用springboot的渲染类
,如下:
1 2 3 4 5 6 7 8 9 10
|
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/> <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/> <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
|
测试
日志写出格式
1 2 3 4 5 6 7
| D:/logs/ ├── info │ └── log-info-2025-01-01.log ├── error │ └── log-error-2025-01-01.log ├── warn └── deubug
|