springmvc + logback project log configuration

  Logback recently used in doing the project as a project log, but abandoned the log4j. Specific Why logback, I summarized the following points:

  Creators 1.log4j and logback same person, logback is an upgraded version of log4j, log4j is a package of core.

  2. The performance, in certain scenarios, the implementation of faster than 10 times, while the smaller memory initialization

  3. Expand the document, rich and detailed documentation and constantly updated, can be viewed on the official website

  4.Filters (filter), use of filters is a major feature of logback, when using log4j when we encounter problems, we need to reduce the log level, but there will be plenty of time and log output. But logback according to different user set the log level, such as when a user log level debug, other users can when error, here to understand MDCFilter

  The automatic log removal and log auto-compression, or automatically cleared by setting TimeBasedRollingPolicy SizeAndTimeBasedFNATP of maxHistory properties, can automatically rollback RollingFileAppender compression, the log file is automatically compressed archive, asynchronous rollback

  6. Automatic loading configuration file, Logback-classic can be modified after the configuration file, automatically reload

  Do above it is to introduce the theory, and now we have to use logback framework through specific examples

  Configuration dependent maven

  ch.qos.logback

  logback-core

  1.1.5

  ch.qos.logback

  logback-classic

  1.1.5

  org.logback-extensions

  logback-ext-spring

  0.1.3

  ch.qos.logback

  logback-classic

  org.slf4j

  slf4j-fire

  1.7.5

  org.slf4j

  slf4j-simple

  1.7.5

  Note: After configuring logback jar package, be sure not to configure log4j jar package, because the jar package this conflict, it is possible to cause the log file output has no content, if the other jar included in the package have the log4j we want to exclusion, such as the introduction of our dependence shiro

  org.apache.shiro

  shiro-core

  org.slf4j

  slf4j-fire

  1.2.2

  2. Profiles

  First, in web.xml configured for loading and filtered logback.xml

  log4jConfigLocation

  classpath:logback.xml

  ch.qos.logback.ext.spring.web.LogbackConfigListener

  Note: there must be a listener, or else logback not effective, there must be placed rescore logback folder, if placed under loads of less than webapp

  

Here Insert Picture Description

 

  3. Configure logback.xml

  logback

  %red(%d{yyyy-MM-dd HH:mm:ss}) %green([%thread]) %highlight(%-5level) %boldMagenta(%logger:%-3L) - %cyan(%msg%n)

  ${CONSOLE_LOG_PATH}/neoinfo.log

  INFO

  DENY

  ACCEPT

  ${CONSOLE_LOG_PATTERN}

  UTF-8

  true

  false

  ${CONSOLE_LOG_PATH}/neoinfo.%d.log

  15

  10GB

  ${CONSOLE_LOG_PATH}/neoinfo_error.log

  ERROR

  ${CONSOLE_LOG_PATTERN}

  UTF-8

  false

  false

  ${CONSOLE_LOG_PATH}/neoinfo_error.%d.log

  15

  10GB

  4. Configuration class aop

  package com.aop;

  import com.fasterxml.jackson.databind.ObjectMapper;

  import org.apache.shiro.SecurityUtils;

  import org.apache.shiro.session.Session;

  import org.aspectj.lang.JoinPoint;

  import org.aspectj.lang.annotation.AfterReturning;

  import org.aspectj.lang.annotation.Aspect;

  import org.aspectj.lang.annotation.Before;

  import org.aspectj.lang.annotation.Pointcut;

  import org.slf4j.Logger;

  import org.slf4j.LoggerFactory;

  import org.slf4j.MDC;

  import org.springframework.stereotype.Component;

  import org.springframework.web.context.request.RequestContextHolder;

  import org.springframework.web.context.request.ServletRequestAttributes;

  import javax.servlet.http.HttpServletRequest;

  import java.text.SimpleDateFormat;

  import java.util.Date;

  import java.util.Enumeration;

  import java.util.UUID;

  / ** Zhengzhou gynecological hospital Which is good http://fk.zyfuke.com/

  * Log output

  * */

  @Aspect

  @Component

  public class LogAspect {

  private static Logger log = LoggerFactory.getLogger(LogAspect.class);

  @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping) || @annotation(org.springframework.web.bind.annotation.GetMapping) || @annotation(org.springframework.web.bind.annotation.PostMapping)")

  public void webLogs() {

  }

  @Before("webLogs()")

  public void doBefore(JoinPoint joinPoint) {

  // receipt of the request, the content recording request

  ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

  HttpServletRequest request = attributes.getRequest();

  // request contents recorded

  log.info("DATE:"+new Date());

  log.info("URL : " + request.getRequestURL().toString());

  log.info("HTTP_METHOD : " + request.getMethod());

  log.info("IP : " + request.getRemoteAddr());

  log.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());

  Enumeration enu = request.getParameterNames();

  while (enu.hasMoreElements()) {

  String name = (String) enu.nextElement();

  log.info("name:{" + name + "},value:{" + request.getParameter(name) + "}");

  }

  }

  @AfterReturning(returning = "ret", pointcut = "webLogs()")

  public void doAfterReturning(JoinPoint joinPoint, Object ret) throws Throwable {

  ObjectMapper mapper = new ObjectMapper();

  mapper.setDateFormat(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"));

  try {

  if (log.isDebugEnabled()) {

  log.debug("returnValue={}", mapper.writeValueAsString(ret));

  }

  } catch (Exception ex) {

  log.error ( "interceptors abnormal", ex);

  }

  }

  }

Guess you like

Origin www.cnblogs.com/gnz49/p/12084403.html