First knowledge of log4j2 and log4j
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Log4j2Test {
public static void main(String[] args) {
System.setProperty("log4j.skipJansi", "true");//2020-02-13 13:41:25,889 main WARN Unable to instantiate org.fusesource.jansi.WindowsAnsiOutputStream
// Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
Logger logger = LogManager.getLogger("mylog");
logger.trace("trace level");
logger.debug("debug level");
logger.info("info level");
logger.warn("warn level");
logger.error("error level");
logger.fatal("fatal level");
}
}
If there is no log4j2.xml file in this project (recommended resources directory), the error will be reported and the running result will be
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
14:12:28.891 [main] ERROR mylog - error level
14:12:28.894 [main] FATAL mylog - fatal level
When log4j2.xml is added to the resources directory, the content is
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<properties>
<property name="LOG_HOME">E:/logs</property>
<property name="FILE_NAME">mylog</property>
<property name="log.sql.level">info</property>
</properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %l - %msg%n" />
</Console>
<RollingRandomAccessFile name="RollingRandomAccessFile" fileName="${LOG_HOME}/${FILE_NAME}.log" filePattern="${LOG_HOME}/$${date:yyyy-MM}/${FILE_NAME}-%d{yyyy-MM-dd HH-mm}-%i.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %l - %msg%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
<SizeBasedTriggeringPolicy size="10 MB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<Root level="warn">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingRandomAccessFile" />
</Root>
<Logger name="com.dy.hbase2hbase" level="${log.sql.level}" additivity="false">
<AppenderRef ref="Console" />
</Logger>
<Logger name="mylog" level="${log.sql.level}" additivity="false">
<AppenderRef ref="Console" />
</Logger>
</Loggers>
</Configuration>
The location structure is shown in the figure
For the above error, just add the log4j.properties file in the resources directory, whose content is
log4j.rootLogger=info, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
#1.Applications for consoles
#log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender
#log4j.appender.Threshold = DEBUG
#log4j.appender.CONSOLE.Target = System.out
#log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
#log4j.appender.CONSOLE.layout.ConversionPattern = [framework] % d - % c -%- 4r [ % t] %- 5p % c % x - % m % n
##log4j.appender.CONSOLE.layout.ConversionPattern = [start] % d {DATE} [DATE] % n % p[PRIORITY] % n % x[NDC] % n % t[THREAD] n % c[CATEGORY] % n % m[MESSAGE] % n % n
# 2.Applications for files
#log4j.appender.FILE = org.apache.log4j.FileAppender
#log4j.appender.FILE.File = file.log
#log4j.appender.FILE.Append = false
#log4j.appender.FILE.layout = org.apache.log4j.PatternLayout
#log4j.appender.FILE.layout.ConversionPattern = [framework] % d - % c -%- 4r [ % t] %- 5p % c % x - % m % n
# Use this layout for LogFactor 5 analysis
# 3.Applications for files
#log4j.appender.FILE = org.apache.log4j.FileAppender
#log4j.appender.FILE.File = file.log
#log4j.appender.FILE.Append = false
#log4j.appender.FILE.layout = org.apache.log4j.PatternLayout
#log4j.appender.FILE.layout.ConversionPattern = [framework] % d - % c -%- 4r [ % t] %- 5p % c % x - % m % n
# Use this layout for LogFactor 5 analysis
And the project needs pom.xml by
<!-- slf4j + log4j begin -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.10</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- log4j end-->