ORIGIN

Spring Framework 6.0 Log framework

Spring6 3 mins581 words

Notes of Log4j2 base on the previous article.

log framework (Log4j2)

the framework is mad of some important components:

  • log priority: TRACE < DEBUG < INFO < WARN < ERROR < FATAL

    The level lower than your set level will not show.

  • where the log output, to console or to file.

  • format of the log.

Add Dependency

add this in the pom.xml file under your project.

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<version>2.20.0</version>
</dependency>

Add config file

create a new log4j2.xml file in source folder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<!-- Logging Properties -->
<Properties>
<Property name="LOG_PATTERN">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n</Property>
<Property name="APP_LOG_ROOT">c:/temp/logs</Property>
</Properties>
<appenders>
<!-- Console Appender -->
<console name="spring6log" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss SSS} [%t] %-3level %logger{1024} - %msg%n"/>
</console>

<!-- File Appenders on need basis -->
<File name="log" fileName="${APP_LOG_ROOT}/app-framework.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
</File>

<RollingFile name="RollingFile" fileName="${APP_LOG_ROOT}/app-info.log"
filePattern="${APP_LOG_ROOT}/app-info-%d{yyyy-MM-dd}-%i.log" >
<LevelRangeFilter minLevel="INFO" maxLevel="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="$d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</appenders>
<loggers>
<root level="DEBUG">
<appender-ref ref="spring6log"/>
<appender-ref ref="RollingFile"/>
<appender-ref ref="log"/>
</root>
</loggers>
</Configuration>

And then run the previous code, we got

1
2
3
4
5
6
2023-02-21 10:50:37 907 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1c481ff2
2023-02-21 10:50:38 366 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 1 bean definitions from class path resource [beam.xml]
2023-02-21 10:50:38 466 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'user'
Constructor function ran.
com.yao.spring6.User@3f28bd56
add ...

And then, we found log files in C:\temp\logs

Out put log manually

import packages of log4j2

1
2
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

create object and use it

1
2
private Logger logger = LoggerFactory.getLogger(TestUser.class);
logger.info("# SUCCESS");
TOP
COMMENT
  • ABOUT
  • |
o_oyao
  The Jigsaw puzzle is incomplete with even one missing piece. And I want to be the last piece to make the puzzle complete.
Like my post?
Default QR Code
made with ❤️ by o_oyao
©o_oyao 2019-2024

|