日志配置
-
运行日志
- 采用Spring Boot默认的Logback日志框架
- 配置项目输出INFO和ERROR级别的日志到日志文件,具体配置如下:
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true" scanPeriod="60 seconds" debug="false"> <contextName>febs</contextName> <property name="log.path" value="log" /> <property name="log.maxHistory" value="15" /> <property name="log.colorPattern" value="%d{yyyy-MM-dd HH:mm:ss} | %highlight(%-5level) | %boldYellow(%thread) | %boldGreen(%logger) | %msg%n"/> <property name="log.pattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n" /> <!--输出到控制台--> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>${log.colorPattern}</pattern> </encoder> </appender> <!--输出到文件--> <appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.path}/info/info.%d{yyyy-MM-dd}.log</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> <appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.path}/error/error.%d{yyyy-MM-dd}.log</fileNamePattern> </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> <root level="debug"> <appender-ref ref="console" /> </root> <root level="info"> <appender-ref ref="file_info" /> <appender-ref ref="file_error" /> </root> </configuration>
- 可配置将指定目录下的增删改查操作SQL打印到控制台中(日志级别设置为debug)
logging: level: cn: makelocks: management: dao: debug
-
操作日志
- 项目中用户的操作日志使用AOP的方式实现,切点为@Log注解
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Log { String value() default ""; }
- 在需要生成用户操作日志的Service层或Controller层接口上加上@Log注解即可产生日志信息(包括操作用户、访问时间、方法总耗时、方法参数、操作用户的IP和具体地址等信息)