pom依赖
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.11</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.logback-extensions</groupId>
<artifactId>logback-ext-spring</artifactId>
<version>0.1.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.25</version>
</dependency>
aop实现的日志记录 logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- 尽量别用绝对路径,如果带参数不同容器路径解释可能不同,以下配置参数在pom.xml里 -->
<property name="log.root.level" value="INFO" /> <!-- 日志级别 -->
<property name="log.other.level" value="DEBUG" /> <!-- 其他日志级别 -->
<property name="log.base" value="logs" /> <!-- 日志路径,这里是相对路径,web项目eclipse下会输出到eclipse的安装目录下,如果部署到linux上的tomcat下,会输出到tomcat/bin目录 下 -->
<property name="log.moduleName" value="log_center" /> <!-- 模块名称, 影响日志配置名,日志文件名 -->
<property name="log.max.size" value="100MB" /> <!-- 日志文件大小,超过这个大小将被压缩 -->
<!--控制台输出 -->
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread]:%msg%n</pattern>
</encoder>
</appender>
<!-- 用来保存输出所有级别的日志 -->
<appender name="file.all" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${log.base}/${log.moduleName}.log</File><!-- 设置日志不超过${log.max.size}时的保存路径,注意如果
是web项目会保存到Tomcat的bin目录 下 -->
<!-- 滚动记录文件,先将日志记录到指定文件,当符合某个条件时,将日志记录到其他文件。 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${log.base}/archive/${log.moduleName}_all_%d{yyyy-MM-dd}.%i.log.zip
</FileNamePattern>
<!-- 文件输出日志 (文件大小策略进行文件输出,超过指定大小对文件备份) -->
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${log.max.size}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<!-- 日志输出的文件的格式 -->
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method\(\):%L -%msg%n</pattern>
</layout>
</appender>
<!-- 这也是用来保存输出所有级别的日志 -->
<appender name="file.all.other" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${log.base}/${log.moduleName}_other.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${log.base}/archive/${log.moduleName}_other_%d{yyyy-MM-dd}.%i.log.zip
</FileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${log.max.size}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<!--<pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{56}.%method\(\):%L -%msg%n</pattern>-->
<pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread]:%msg%n</pattern>
</layout>
</appender>
<!-- 只用保存输出error级别的日志 -->
<appender name="file.error"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${log.base}/${log.moduleName}_err.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${log.base}/archive/${log.moduleName}_err_%d{yyyy-MM-dd}.%i.log.zip
</FileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${log.max.size}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{56}.%method\(\):%L - %msg%n</pattern>
</layout>
<!-- 下面为配置只输出error级别的日志 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
<!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
<!-- 添加附加的appender,最多只能添加一个 -->
<appender name="file.async" class="ch.qos.logback.classic.AsyncAppender">
<discardingThreshold>0</discardingThreshold>
<queueSize>256</queueSize>
<includeCallerData>true</includeCallerData>
<appender-ref ref="file.all" />
</appender>
<appender name="file.async.other" class="ch.qos.logback.classic.AsyncAppender">
<discardingThreshold>0</discardingThreshold>
<queueSize>256</queueSize>
<includeCallerData>true</includeCallerData>
<appender-ref ref="file.all.other" />
</appender>
<!--浏览记录类日志-->
<appender name="addViewLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.base}/addView_log/exception_view_log-%d{yyyy-MM-dd}-%i.info.log</fileNamePattern>
<maxHistory>30</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>30MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [ %thread ] - [ %-5level ] [ %logger{50} : %line ] - %msg%n</pattern>
</layout>
</appender>
<!-- 为某个包下的所有类的指定Appender 这里也可以指定类名称例如:com.aa.bb.ClassName -->
<!--<logger name="com.yyt" additivity="false">-->
<!--<level value="${log.root.level}" />-->
<!--<appender-ref ref="stdout" />-->
<!--<appender-ref ref="file.async" /><!– 即com.li包下级别为 ${log.root.level}的才会使用file.async来打印 –>-->
<!--<appender-ref ref="file.error" />-->
<!--</logger>-->
<logger name="org.apache.ibatis" level="info">
<appender-ref ref="STDOUT"/>
</logger>
<logger name="java.sql" level="info">
<appender-ref ref="STDOUT"/>
</logger>
<!-- root将级别为${log.root.level}及大于${log.root.level}的日志信息交给已经配置好的名为“Console”的appender处理,“Console”appender将信息打印到Console,其它同理 -->
<root level="info">
<appender-ref ref="stdout" /> <!-- 标识这个appender将会添加到这个logger -->
<appender-ref ref="file.async.other" />
<appender-ref ref="file.error" />
</root>
<logger level="INFO" name="addViewLog">
<appender-ref ref="addViewLog"/>
</logger>
</configuration>
自定义注解下的aop实现
@interface AddViewsCount.java
import java.lang.annotation.*;
/**
* 记录浏览记录views的数量
*/
@Documented
@Inherited
@Target({ ElementType.FIELD, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface AddViewsCount {
}
aop的实现体AddViewCountAopImpl.java
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import xyz.zjhwork.dao.ExceptionDao;
import org.apache.catalina.connector.RequestFacade;
import java.util.HashMap;
import java.util.Map;
@Component
@Aspect
@Slf4j(topic = "addViewLog")
public class AddViewCountAopImpl {
@Autowired
private ExceptionDao exceptionDao;
// private final Logger log = LoggerFactory.getLogger(getClass());
@Pointcut("@annotation(xyz.zjhwork.aop.interfaces.AddViewsCount)")
private void do_annotation(){
log.info("do_annotation about add count for exception views ....");
}
/**
* 前置处理浏览文章
* request -> org.apache.catalina.connector.RequestFacade
* integer -> java.lang.Integer
* @param joinPoint 切入点
*/
@Before(value = "do_annotation()")
public void addViewCount(JoinPoint joinPoint){
log.info("do addViewCount.....");
Map<String,Object> map = new HashMap<>();
Object[] args = joinPoint.getArgs();
for (Object arg : args) {
if(null!=arg)
map.put(arg.getClass().toString(),arg);
}
RequestFacade request = (RequestFacade)map.get("class org.apache.catalina.connector.RequestFacade");
Integer exceptionId = (Integer) map.get("class java.lang.Integer");
log.info("api about /getException has visited form ip :[{}],visited exception id is [{}]",request.getRemoteHost(),exceptionId);
log.info("starting execute add Views....");
int res=0;
//判断exceptionId是否为null
if(exceptionId!=null)
res = exceptionDao.addViews(exceptionId);
log.info("execute add result:[{}]",res==1);
}
}