Spring MVC Log4j – Ghi log trong Spring MVC với Spring AOP, AspectJ, Log4j

Spring MVC Log4j – Ghi log trong Spring MVC với Spring AOP, AspectJ, Log4j

 

1. Spring MVC + Log4j

Spring MVC Log4j – Ghi log trong Spring MVC với Spring AOP, AspectJ, Log4j. Code ví dụ Log4j với Spring MVC, kết hợp với AOP + AspectJ

Xem lại  về Log4j: https://stackjava.com/library/apache-log4j.html

Spring MVC Log4j - Ghi log trong Spring MVC với Spring AOP, AspectJ, Log4j

Thư viện sử dung:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>stackjava.com</groupId>
  <artifactId>SpringMVC-Log4j</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.0.2.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
  </dependencies>
  
</project>

File Controller

package stackjava.com.springmvchello.controller;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

  private static final Logger logger = Logger.getLogger(HelloController.class);

  @RequestMapping("/")
  public String index() {
    logger.debug("debug log");
    logger.error("error log");
    logger.info("info log");
    return "index";
  }

}

File config log4j

## Show error log + info log + debug log to console
log4j.rootLogger= DEBUG, stdout
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p %c{1}: %C %M - %m%n
<html>
<head>
<title>index</title>
</head>
<body>
  <h1>Spring MVC Log4j!</h1>
</body>
</html>

 

Kết quả:

spring mvc log4j spring mvc log4j

 

2. Spring MVC + AOP + AspectJ + Log4j

Xem lại về Spring AOP: https://stackjava.com/spring/spring-aop-1.html

Xem lại về Spring AOP + AspectJ: https://stackjava.com/spring/spring-core-aop-aspectj.html

spring mvc aspectj + log4j

Thư viện sử dụng:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>stackjava.com</groupId>
  <artifactId>SpringMVCHello</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <dependencies>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.0.2.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.0.2.RELEASE</version>
    </dependency>
    <!-- AspectJ -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.6.11</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.6.11</version>
    </dependency>

    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
  </dependencies>
</project>

File controller

package stackjava.com.springmvchello.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

  @RequestMapping("/")
  public String index() {
    return "index";
  }

  @RequestMapping("/error")
  public String error() throws Exception {
    throw new Exception("testing");
  }
}

File thực hiện ghi log:

package stackjava.com.springmvchello.logger;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class LoggerAspectJ {

  private static final Logger logger = Logger.getLogger(LoggerAspectJ.class);

  @Before("execution(* stackjava.com.springmvchello.controller.HelloController.*(..))")
  public void logBefore(JoinPoint joinPoint) {
    logger.info("before method: " + joinPoint.getSignature().getName());
  }

  @After("execution(* stackjava.com.springmvchello.controller.HelloController.*(..))")
  public void logAfter(JoinPoint joinPoint) {
    logger.info("after method: " + joinPoint.getSignature().getName());
  }

  @AfterReturning(pointcut = "execution(* stackjava.com.springmvchello.controller.HelloController.*(..))",
      returning = "result")
  public void logAfterReturning(JoinPoint joinPoint, Object result) {
    logger.info("after return method : " + joinPoint.getSignature().getName());
    logger.info("Method returned value is : " + result);
  }

  @AfterThrowing(pointcut = "execution(* stackjava.com.springmvchello.controller.HelloController.*(..))",
      throwing = "error")
  public void logThrow(JoinPoint joinPoint, Throwable error) {
    logger.info("exception in method: " + joinPoint.getSignature().getName());
    logger.error("Exception is: " + error);
  }

}

File config log4j:

## Show error log + info log + debug log to console
log4j.rootLogger= DEBUG, stdout
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p %c{1}: %C %M - %m%n

File view:

<html>
<head>
<title>index</title>
</head>
<body>
  <h1>Spring MVC Log4j + AspectJ!</h1>
  <a href="error">/error</a>
</body>
</html>

Demo:

Kết quả log cho method index()

Kết quả log cho method error() (/error)

 

Okay, done!

Download code ví dụ trên tại đây: SpringMVC-Log4j  | SpringMVC-Log4j-AspectJ

 

References:

https://docs.spring.io/spring/docs/current/spring-framework-reference

Spring MVC Log4j – Ghi log với Log4j, Spring AOP + AspectJ + Log4j

 

stackjava.com