对log4j进行多环境不同日志级别的控制
This commit is contained in:
62
Chapter4-2-3/pom.xml
Normal file
62
Chapter4-2-3/pom.xml
Normal file
@@ -0,0 +1,62 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>com.didispace</groupId>
|
||||
<artifactId>Chapter4-2-3</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Chapter4-2-3</name>
|
||||
<description>Spring Boot project</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.3.2.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-logging</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-log4j</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
16
Chapter4-2-3/src/main/java/com/didispace/Application.java
Normal file
16
Chapter4-2-3/src/main/java/com/didispace/Application.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
logging.level.com.didispace=INFO
|
||||
@@ -0,0 +1 @@
|
||||
logging.level.com.didispace=INFO
|
||||
@@ -0,0 +1 @@
|
||||
logging.level.com.didispace=DEBUG
|
||||
1
Chapter4-2-3/src/main/resources/application.properties
Normal file
1
Chapter4-2-3/src/main/resources/application.properties
Normal file
@@ -0,0 +1 @@
|
||||
spring.profiles.active=dev
|
||||
36
Chapter4-2-3/src/main/resources/log4j.properties
Executable file
36
Chapter4-2-3/src/main/resources/log4j.properties
Executable file
@@ -0,0 +1,36 @@
|
||||
# LOG4J配置
|
||||
log4j.rootCategory=INFO, stdout, file, errorfile
|
||||
log4j.category.com.didispace=${logging.level.com.didispace}, didifile
|
||||
log4j.logger.error=errorfile
|
||||
|
||||
# 控制台输出
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n
|
||||
|
||||
# root日志输出
|
||||
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
|
||||
log4j.appender.file.file=logs/all.log
|
||||
log4j.appender.file.DatePattern='.'yyyy-MM-dd
|
||||
log4j.appender.file.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n
|
||||
|
||||
# error日志输出
|
||||
log4j.appender.errorfile=org.apache.log4j.DailyRollingFileAppender
|
||||
log4j.appender.errorfile.file=logs/error.log
|
||||
log4j.appender.errorfile.DatePattern='.'yyyy-MM-dd
|
||||
log4j.appender.errorfile.Threshold = ERROR
|
||||
log4j.appender.errorfile.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.errorfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n
|
||||
|
||||
# com.didispace下的日志输出
|
||||
log4j.appender.didifile=org.apache.log4j.DailyRollingFileAppender
|
||||
log4j.appender.didifile.file=logs/my.log
|
||||
log4j.appender.didifile.DatePattern='.'yyyy-MM-dd
|
||||
log4j.appender.didifile.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.didifile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L ---- %m%n
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = Application.class)
|
||||
public class ApplicationTests {
|
||||
|
||||
private Logger logger = Logger.getLogger(getClass());
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
logger.info("输出info");
|
||||
logger.debug("输出debug");
|
||||
logger.error("输出error");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user