项目集成 log4j logback log4j2

1 项目集成log4j

1.1 加入pom依赖

<dependency>
    <groupId>log4jgroupId>
    <artifactId>log4jartifactId>
    <version>1.2.17version>
dependency>
<dependency>
    <groupId>org.slf4jgroupId>
    <artifactId>slf4j-apiartifactId>
    <version>1.7.25version>
dependency>
<dependency>
    <groupId>org.slf4jgroupId>
    <artifactId>slf4j-log4j12artifactId>
    <version>1.7.25version>
dependency>

1.2 加入log4j.properties

log4j.rootLogger=debug,stdout,debug,info,warn,error
### 输出信息到控制抬
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}  [ %t:%r ] - [ %p ]  %m%n
### 输出DEBUG 级别以上的日志到 log/debug.log
log4j.appender.debug=org.apache.log4j.DailyRollingFileAppender
log4j.appender.debug.File=log/integrate-log4j-debug.log
log4j.appender.debug.Append=true
log4j.appender.debug.Threshold=DEBUG
log4j.appender.debug.layout=org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
### 输出DEBUG 级别以上的日志到 log/debug.log
log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
log4j.appender.info.File=log/integrate-log4j-info.log
log4j.appender.info.Append=true
log4j.appender.info.Threshold=INFO
log4j.appender.info.layout=org.apache.log4j.PatternLayout
log4j.appender.info.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
### 输出DEBUG 级别以上的日志到 log/debug.log
log4j.appender.warn=org.apache.log4j.DailyRollingFileAppender
log4j.appender.warn.File=log/integrate-log4j-warn.log
log4j.appender.warn.Append=true
log4j.appender.warn.Threshold=WARN
log4j.appender.warn.layout=org.apache.log4j.PatternLayout
log4j.appender.warn.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
### 输出ERROR 级别以上的日志到 log/error.log
log4j.appender.error=org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.File=log/integrate-log4j-error.log
log4j.appender.error.Append=true
log4j.appender.error.Threshold=ERROR
log4j.appender.error.layout=org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

1.3 在代码中使用

package com.zjw;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Log4jTest {
  private static Logger logger = LoggerFactory.getLogger(Log4jTest.class);
  
  public static void main(String[] args) {
    logger.info("Log4j test.");
  }
}

2 项目集成logback

2.1 加入pom依赖

<dependency>
    <groupId>org.slf4jgroupId>
    <artifactId>slf4j-apiartifactId>
    <version>1.7.25version>
dependency>
<dependency>
    <groupId>ch.qos.logbackgroupId>
    <artifactId>logback-coreartifactId>
    <version>1.2.3version>
dependency>
<dependency>
    <groupId>ch.qos.logbackgroupId>
    <artifactId>logback-accessartifactId>
    <version>1.2.3version>
dependency>
<dependency>
    <groupId>ch.qos.logbackgroupId>
    <artifactId>logback-classicartifactId>
    <version>1.2.3version>
dependency>

2.2 加入logback.xml


<configuration>
  
  <property name="LOG_HOME" value="./log"/>
  
  <property name="LOG_NAME" value="eureka-server"/>
  
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
      
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
    encoder>
  appender>
  
  <appender name="ROLLING_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      
      <FileNamePattern>${LOG_HOME}/${LOG_NAME}-%d{yyyy-MM-dd}.log
      FileNamePattern>
      
      <MaxHistory>30MaxHistory>
    rollingPolicy>
    <encoder
      class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
      
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
    encoder>
    
    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
      <MaxFileSize>10MBMaxFileSize>
    triggeringPolicy>
  appender>
  
  <root level="INFO">
    <appender-ref ref="STDOUT"/>
    <appender-ref ref="ROLLING_FILE"/>
  root>
configuration>

2.3 在代码中使用

package com.zjw;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LogBackTest {
  private static Logger logger = LoggerFactory.getLogger(LogBackTest.class);

  public static void main(String[] args) {
    logger.info("Logback Test.");
  }
}

3 项目集成log4j2

3.1 加入pom依赖


<dependency>
    <groupId>org.slf4jgroupId>
    <artifactId>slf4j-apiartifactId>
    <version>1.7.25version>
dependency>
<dependency>
    <groupId>org.slf4jgroupId>
    <artifactId>jcl-over-slf4jartifactId>
    <version>1.7.25version>
    <scope>runtimescope>
dependency>

<dependency>
    <groupId>org.apache.logging.log4jgroupId>
    <artifactId>log4j-apiartifactId>
    <version>2.4.1version>
dependency>
<dependency>
    <groupId>org.apache.logging.log4jgroupId>
    <artifactId>log4j-coreartifactId>
    <version>2.4.1version>
dependency>

<dependency>
    <groupId>org.apache.logging.log4jgroupId>
    <artifactId>log4j-slf4j-implartifactId>
    <version>2.4.1version>
dependency>

<dependency>
    <groupId>org.apache.logging.log4jgroupId>
    <artifactId>log4j-webartifactId>
    <version>2.4.1version>
    <scope>runtimescope>
dependency>

<dependency>
    <groupId>com.lmaxgroupId>
    <artifactId>disruptorartifactId>
    <version>3.2.0version>
dependency>

3.2 加入log4j2.xml


<Configuration status="off" monitorInterval="1800">
  <properties>
    <property name="LOG_HOME">./logsproperty>
    <property name="ERROR_LOG_FILE_NAME">errorproperty>
  properties>
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d %-5p (%F:%L) - %m%n"/>
    Console>
    <RollingRandomAccessFile name="ErrorLog" fileName="${LOG_HOME}/${ERROR_LOG_FILE_NAME}.log"
      filePattern="${LOG_HOME}/${ERROR_LOG_FILE_NAME}.log.%d{yyyy-MM-dd}.gz">
      <PatternLayout pattern="%d %-5p (%F:%L) - %m%n"/>
      <Policies>
        <TimeBasedTriggeringPolicy/>
        <SizeBasedTriggeringPolicy size="100 MB"/>
      Policies>
      <DefaultRolloverStrategy max="20"/>
    RollingRandomAccessFile>
  Appenders>
  <Loggers>
    
    <logger name="org.springframework.core" level="info">logger>
    <logger name="org.springframework.beans" level="info">logger>
    <logger name="org.springframework.context" level="info">logger>
    <logger name="org.springframework.web" level="info">logger>
    <logger name="com.hafiz.www.controller" level="error" includeLocation="true" additivity="false">
      <appender-ref ref="ErrorLog"/>
      <appender-ref ref="Console"/>
    logger>
    <root level="info" includeLocation="true">
      <appender-ref ref="Console"/>
    root>
  Loggers>
Configuration>

3.3 在代码中使用

package com.zjw;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Log4j2Test {
  private static Logger logger = LoggerFactory.getLogger(Log4j2Test.class);
  public static void main(String[] args) {
    logger.info("Log4j2 Test.");
  }
}

你可能感兴趣的:(log4j,log4j2,logback)