forked from lenve/javaboy-code-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
Java 里边的日志两大类: | ||
|
||
- 门面(Apache Commons Logging,Slf4j) | ||
- 实现(Java Util Logging、Log4j) | ||
|
||
日志级别: | ||
|
||
- ERROR | ||
- WARN | ||
- INFO | ||
- DEBUG | ||
- TRACE | ||
|
||
Spring Boot 使用 Apache Commons Logging 作为内部日志框架,默认的日志实现是 Java Util Logging。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.3.4.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<groupId>org.javaboy</groupId> | ||
<artifactId>logging</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>logging</name> | ||
<description>Demo project for Spring Boot</description> | ||
|
||
<properties> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</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-log4j2</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>org.junit.vintage</groupId> | ||
<artifactId>junit-vintage-engine</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
28 changes: 28 additions & 0 deletions
28
logging/src/main/java/org/javaboy/logging/HelloController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.javaboy.logging; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* @author 江南一点雨 | ||
* @微信公众号 江南一点雨 springboot-logging | ||
* @网站 http://www.itboyhub.com | ||
* @国际站 http://www.javaboy.org | ||
* @微信 a_java_boy | ||
* @GitHub https://github.com/lenve | ||
* @Gitee https://gitee.com/lenve | ||
*/ | ||
@RestController | ||
public class HelloController { | ||
private static final Logger logger = LoggerFactory.getLogger(HelloController.class); | ||
@GetMapping("/hello") | ||
public void hello() { | ||
for (int i = 0; i < 99999; i++) { | ||
logger.info("hello-info"); | ||
logger.warn("hello-warn"); | ||
logger.error("hello-error"); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
logging/src/main/java/org/javaboy/logging/LoggingApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.javaboy.logging; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class LoggingApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(LoggingApplication.class, args); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
logging: | ||
level: | ||
root: error | ||
org.javaboy.logging: info | ||
file: | ||
name: ${user.home}/logs/hello.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<configuration> | ||
<appenders> | ||
<File name="file" | ||
fileName="${sys:user.home}/logs/log4j2.log"> | ||
<PatternLayout pattern="%d{HH:mm:ss,SSS} %p %c (%L) - %m%n"/> | ||
</File> | ||
|
||
</appenders> | ||
<loggers> | ||
<root level="ERROR"> | ||
<appender-ref ref="file"/> | ||
</root> | ||
<logger name="org.javaboy.logging" | ||
level="INFO"/> | ||
</loggers> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<configuration status="warn"> | ||
<properties> | ||
<Property name="app_name">logging</Property> | ||
<Property name="log_path">logs/${app_name}</Property> | ||
</properties> | ||
<appenders> | ||
<console name="Console" target="SYSTEM_OUT"> | ||
<PatternLayout pattern="[%d][%t][%p][%l] %m%n" /> | ||
</console> | ||
<RollingFile name="RollingFileInfo" fileName="${log_path}/info.log" | ||
filePattern="${log_path}/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log.gz"> | ||
<Filters> | ||
<ThresholdFilter level="INFO" /> | ||
<ThresholdFilter level="WARN" onMatch="DENY" | ||
onMismatch="NEUTRAL" /> | ||
</Filters> | ||
<PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" /> | ||
<Policies> | ||
<TimeBasedTriggeringPolicy interval="1" modulate="true" /> | ||
<SizeBasedTriggeringPolicy size="2 MB" /> | ||
</Policies> | ||
<DefaultRolloverStrategy compressionLevel="0" max="10"/> | ||
</RollingFile> | ||
<RollingFile name="RollingFileWarn" fileName="${log_path}/warn.log" | ||
filePattern="${log_path}/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log.gz"> | ||
<Filters> | ||
<ThresholdFilter level="WARN" /> | ||
<ThresholdFilter level="ERROR" onMatch="DENY" | ||
onMismatch="NEUTRAL" /> | ||
</Filters> | ||
<PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" /> | ||
<Policies> | ||
<TimeBasedTriggeringPolicy interval="1" modulate="true" /> | ||
<SizeBasedTriggeringPolicy size="2 MB" /> | ||
</Policies> | ||
<DefaultRolloverStrategy compressionLevel="0" max="10"/> | ||
</RollingFile> | ||
|
||
<RollingFile name="RollingFileError" fileName="${log_path}/error.log" | ||
filePattern="${log_path}/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log.gz"> | ||
<ThresholdFilter level="ERROR" /> | ||
<PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" /> | ||
<Policies> | ||
<TimeBasedTriggeringPolicy interval="1" modulate="true" /> | ||
<SizeBasedTriggeringPolicy size="2 MB" /> | ||
</Policies> | ||
<DefaultRolloverStrategy compressionLevel="0" max="10"/> | ||
</RollingFile> | ||
</appenders> | ||
<loggers> | ||
<root level="info"> | ||
<appender-ref ref="Console" /> | ||
<appender-ref ref="RollingFileInfo" /> | ||
<appender-ref ref="RollingFileWarn" /> | ||
<appender-ref ref="RollingFileError" /> | ||
</root> | ||
</loggers> | ||
</configuration> |
13 changes: 13 additions & 0 deletions
13
logging/src/test/java/org/javaboy/logging/LoggingApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.javaboy.logging; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
@SpringBootTest | ||
class LoggingApplicationTests { | ||
|
||
@Test | ||
void contextLoads() { | ||
} | ||
|
||
} |