blogApp编写之slf4j日志记录数据库读写耗时
来源:互联网 发布:agar.io 源码 编辑:程序博客网 时间:2024/06/07 02:29
我们继续我们的博客创建。
在一个项目日常的运行中,常常会为服务过慢而导致用户体验不好,在这一点上,常常需要进行性能优化,而如果没有针对性的对某一个
模块进行优化,那么效果往往是不理想的,因此需要对日志进行记载,得知是数据库读写性能过差,还是因为程序逻辑写的过于繁琐而导致项目运行
过慢,因此在此就需要对每次的读写进行日志记录。
这里采用了slf4j以及spring的aop特性来做到日志记录。
具体需要的maven依赖如下:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> </dependency> <!--logback--> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </dependency> <!--slf4j--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency>这里我采用了logback,logback的速度非常快,相比于log4j的记录日志,有着明显的优势。
logback的配置文件为:
<?xml version="1.0" encoding="UTF-8"?><configuration> <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> <encoder charset="UTF-8"> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <!--每天生成一个日志文件,保存30天的日志文件。--> <appender name="DayFile" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>logs/logFile.%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>30</maxHistory> </rollingPolicy> <encoder> <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> </encoder> </appender> <!--指定logger name为包名或类全名 指定级别 additivity设置是否传递到root logger --> <logger name="slf4j" level="INFO" additivity="true"> <appender-ref ref="STDOUT"/> <appender-ref ref="DayFile"/> </logger> <!--slf4j2包下的类在ERROR级别时候传递到root logger中--> <logger name="slf4j2" level="ERROR" /> <!--根logger控制--> <root level="INFO"> <appender-ref ref="stdout" /> <appender-ref ref="DayFile"/> </root></configuration>在这里,在项目启动时,会自动在相应的模块中创建一个文件夹为log,用来存放日志。
在这些配置完成之后,我们可以开始我们的日志记录。
在这里,首先编写一个日志记录的注解:
可以采用直接在对应的方法中添加log来记录耗时,但是这种方法太过繁琐,以及不够优雅,因此不采用这种方法进行日志记录。
注解LogPerformance:
package cn.com.log;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * Created by Administrator on 2016/1/20. */@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public @interface LogPerformance {}在这里我们还另外需要一个拦截器来拦截有这个注解的相应方法。
拦截器LogPerformanceInterceptor:
package cn.com.log;import lombok.extern.slf4j.Slf4j;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;/** * Created by Administrator on 2016/1/20. */@Slf4jpublic class LogPerformanceInteceptor implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { long methodegin = System.currentTimeMillis(); Object result = invocation.proceed(); long during = System.currentTimeMillis() - methodegin; //只有加上注解的才进行解析 if (invocation.getMethod().isAnnotationPresent(LogPerformance.class)) { log.info("blog-db : Class {} , Method {} execute time {} ms ", invocation.getMethod().getDeclaringClass().getName(), invocation.getMethod().getName(), during); } return result; }}上述代码中,检查到相应的方法上有Logperformance注解之后,对这个方法进行日志记录其耗时。
具体的代码就是如上,但是还有spring的配置文件中,要对拦截器进行配置才能起作用。
相应的配置为:
<bean id="logPerformanceInteceptor" class="cn.com.log.LogPerformanceInteceptor"></bean><aop:config><!--切入点--><aop:pointcut id="loginPoint" expression="execution(public * cn.com.*.*.*(..)) "/><!--在该切入点使用自定义拦截器--><aop:advisor pointcut-ref="loginPoint" advice-ref="logPerformanceInteceptor"/></aop:config>在这里,因为我采用的数据库并不只是mysql,还有mongodb日后可能还会采用cassandra,因此不能只监控一个package下的类,这个其切入点的配置,基本对所有的
package下的类都进行了监控。
如此,就可以进行日志记录了,这个操作比较简单,在这里我就不演示项目运行时如何记录日志。
2 0
- blogApp编写之slf4j日志记录数据库读写耗时
- SpringBoot之日志记录 SLF4J(H)
- SpringBoot之日志记录 SLF4J(H)
- 使用slf4j编写日志
- slf4j记录日志
- slf4j框架日志记录
- JAVA日志之SLF4J
- Spring Boot 日志记录 SLF4J
- Spring Boot 日志记录 SLF4J
- Spring Boot 日志记录 SLF4J
- Spring Boot 日志记录 SLF4J
- logback+slf4j实现日志记录
- Spring Boot 日志记录 SLF4J
- Spring Boot 日志记录 SLF4J
- 调试日志之slf4j+logback
- Java日志工具之SLF4J
- log4j,slf4j (日志记录工具)
- spring中使用slf4j记录日志
- Leetcode 50 - Pow(x, n)
- Leetcode 48 - Rotate Image
- 白书练习 WERTYU
- Eclipse 如何查看jdk源码
- Hadoop生态系统窥探
- blogApp编写之slf4j日志记录数据库读写耗时
- 明日传奇.Legends.of.Tomorrow
- 使用宏来作为C++单元测试开关
- poj-3254 Corn Fields 状态压缩dp
- rhel7的xfs与Oracle database
- servlet运行原理和生命周期
- SelectionKey,read,
- Linux曲径通幽:用户组管理
- Spark高阶排序算法