java打印log6_springboot(6)打印⽇志log和前台抛错
l (注意,这⾥要把spring boot⾃⼰依赖的log.jar包去掉不然报错)
at org.slf4j.impl.Log4jLoggerFactory.
去掉⽅法
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter
spring-boot-starter-logging
org.springframework.boot
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-log4j
1.3.
2.RELEASE
org.springframework.boot
spring-boot-starter-aop
2. application.properties
#Servlet端⼝号
server.port=8088
# AOP
# Add @EnableAspectJAutoProxy.
spring.aop.auto=true
# Whether subclass-based (CGLIB) proxies are to be created (true) as opposed to standard Java interface-based proxies (false).
# 即当我们需要使⽤CGLIB来实现AOP的时候,需要配置spring.aop.proxy-target-class=true,不然默认使⽤的是标准Java的实现。
spring.aop.proxy-target-class=false
3.log4j.properties
# LOG4J配置
#OFF,systemOut,logFile,logDailyFile,logRollingFile,logMail,logDB,ALL
# level是⽇志记录的优先级,分为OFF,TRACE,DEBUG,INFO,WARN,ERROR,FATAL,ALL
# Log4j建议只使⽤四个级别,优先级从低到⾼分别是DEBUG,INFO,WARN,ERROR ,注:Logger 给定的级别会继承,且重定义级别不能优先于⽗定义的级别
# systemOut,logFile,logDailyFile,logRollingFile 指定⽇志信息输出到哪个地⽅;
#输出到控制台(控制台)
log4j.appender.systemOut= org.apache.log4j.ConsoleAppender
log4j.appender.systemOut.layout= org.apache.log4j.PatternLayout
log4j.appender.systemOut.layout.ConversionPattern= [%-5p][%-22d{yyyy-MM-dd HH:mm:ssS}][%l]%n%m%n
#指定⽇志消息的输出最低层次
log4j.appender.systemOut.Threshold= DEBUG
log4j.appender.systemOut.ImmediateFlush= TRUE
log4j.appender.systemOut.Target= System.out
#输出到⽂件(⽂件)
log4j.appender.logFile= org.apache.log4j.FileAppender
log4j.appender.logFile.layout= org.apache.log4j.PatternLayout
log4j.appender.logFile.layout.ConversionPattern= [%-5p][%-22d{yyyy-MM-dd HH:mm:ssS}][%l]%n%m%n
log4j.appender.logFile.Threshold= DEBUG
log4j.appender.logFile.ImmediateFlush= TRUE
log4j.appender.logFile.Append= TRUE
log4j.appender.logFile.File=logs/log4j.log
log4j.appender.logFile.Encoding= UTF-8
#按DatePattern输出到⽂件(每天产⽣⼀个⽇志⽂件)
log4j.appender.logDailyFile= org.apache.log4j.DailyRollingFileAppender
log4j.appender.logDailyFile.layout= org.apache.log4j.PatternLayout
log4j.appender.logDailyFile.layout.ConversionPattern= [%-5p][%-22d{yyyy-MM-dd HH:mm:ssS}][%l]%n%m%n
log4j.appender.logDailyFile.Threshold= DEBUG
log4j.appender.logDailyFile.ImmediateFlush= TRUE
log4j.appender.logDailyFile.Append= TRUE
log4j.appender.logDailyFile.File= logs/log4j
log4j.appender.logDailyFile.DatePattern= '_'yyyy-MM-dd'.log'
log4j.appender.logDailyFile.Encoding= UTF-8
#设定⽂件⼤⼩输出到⽂件(⽂件⼤⼩到达指定尺⼨的时候产⽣⼀个新的⽂件)
log4j.appender.logRollingFile= org.apache.log4j.RollingFileAppender
log4j.appender.logRollingFile.layout= org.apache.log4j.PatternLayout
log4j.appender.logRollingFile.layout.ConversionPattern= [%-5p][%-22d{yyyy-MM-dd HH:mm:ssS}][%l]%n%m%n log4j.appender.logRollingFile.Threshold= DEBUG
log4j.appender.logRollingFile.ImmediateFlush= TRUE
log4j.appender.logRollingFile.Append= TRUE
log4j.appender.logRollingFile.File= logs/log4j.log
log4j.appender.logRollingFile.MaxFileSize= 1MB
log4j.appender.logRollingFile.MaxBackupIndex= 10
log4j.appender.logRollingFile.Encoding= UTF-8
4.Application.java
package com.guilf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 注:启动类不要放在main/java 根⽬录下,启动会报错
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
5.WebLogAspect.java
package com.guilf.aspect;
import at.util.http.fileupload.RequestContext;
import org.aspectj.lang.JoinPoint;
springboot aop
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestHeader;
import org.t.request.RequestAttributes;
import org.t.request.RequestContextHolder;
import org.t.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
/**
* ⽇志切⾯类.
* Created by hong on 2017/5/19.
*/
/**
* 可以使⽤@Order注解指定切⾯的优先级,值越⼩优先级越⾼
**/
@Order(2)
/**
* 注解将⼀个java类定义为切⾯类
**/
@Aspect
@Component
public class WebLogAspect {
private final Logger logger = Class());
/**
* 根据需要在切⼊点不同位置的切⼊内容:
* 使⽤ @Before 在切⼊点开始处切⼊内容 ( 前置通知 )
* 使⽤ @After 在切⼊点结尾处切⼊内容 ( 后置通知 )
* 使⽤ @AfterReturning 在切⼊点return内容之后切⼊内容(可以⽤来对处理返回值做⼀些加⼯处理) (
配置后置返回通知 ) * 使⽤ @Around 在切⼊点前后切⼊内容,并⾃⼰控制何时执⾏切⼊点⾃⾝的内容 ( 环绕通知 )
* 使⽤ @AfterThrowing ⽤来处理当切⼊内容部分抛出异常之后的处理逻辑 ( 异常通知 )
*/
/**
* 定义个⽅法,注解上@Pointcut 及其规则表达式,定义成⼀个切⼊点(如下例中某个package下的所有函数,也可以是⼀个注解等。); * Controller层切点
*/
@Pointcut("execution(public * com.guilf.mvc..*.*(..))")
public void webLog() {
}
/**
* 前置通知 ⽤于拦截Controller层记录的参数
*
* @param joinPoint
* @Before 接收的参数是前⾯定义的切⼊点⽅法
*/
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) {
logger.info("==========执⾏ weblog() 切⼊点 前置通知===============");
// 接收到请求,记录请求内容
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestAttributes(); HttpServletRequest request = Request();
// 记录下请求内容
logger.info("URL : " + RequestURL().toString());
logger.info("HTTP_METHOD : " + Method());
logger.info("IP : " + LocalAddr());
logger.info("CLASS_METHOD : " + Signature().getDeclaringTypeName() + "." +
}
/**
* 后置通知
*/
@After("webLog()")
public void after(JoinPoint joinPoint) {
logger.info("==========执⾏ weblog() 切⼊点 后置通知===============");

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。