Log4j configuration and use details, introduction to Java development tutorial

Log level and priority in Log4j: all < debug < info < warn < error < fatal < off. By defining the level of each log information, we can more carefully control the log generation process.

log4j.properties configuration file:

# Global logging configuration

# Set the log output level and output destination. You can set multiple output destinations. In the development environment, the log level should be set to DEBUG or ERROR

# Write the log level in front, and write the output destination after the comma: corresponding to the destination set below, separated by commas

# log4j.rootLogger = [level],appenderName1,appenderName2,...

log4j.rootLogger=DEBUG,CONSOLE,LOGFILE



#### console output  ####

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

# Output to console

log4j.appender.CONSOLE.Target = System.out

# Specify console output log level

log4j.appender.CONSOLE.Threshold = DEBUG

# The default value is true, indicating whether to output immediately

log4j.appender.CONSOLE.ImmediateFlush = true

# Set encoding method

log4j.appender.CONSOLE.Encoding = UTF-8

# Log output layout

log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout

# If the log output layout is at the PatternLayout custom level, you need to use ConversionPattern to specify the output format

log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p (%c:%L) - %m%n



#### Output error message to file ####

log4j.appender.LOGFILE=org.apache.log4j.FileAppender

# Specify the output file path

#log4j.appender.LOGFILE.File =F://Intellij idea/logs/error.log 

log4j.appender.LOGFILE.File =./logs/error.log 



#Log output to file. The default value is true

log4j.appender.LOGFILE.Append = true

# Specify the output log level

log4j.appender.LOGFILE.Threshold = ERROR

# Whether to output immediately. The default value is true,

log4j.appender.LOGFILE.ImmediateFlush = true

# Set encoding method

log4j.appender.LOGFILE.Encoding = UTF-8

# Log output layout

log4j.appender.LOGFILE.layout = org.apache.log4j.PatternLayout

# If the log output layout is at the PatternLayout custom level, you need to use ConversionPattern to specify the output format

log4j.appender.LOGFILE.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n 

Test query operation:

Log4j three components

  • Logger: logger, the core class of logging, which is used to output messages at different log levels.

  • Appender: log output destination, used to specify the destination of log output, such as console, file, etc.

  • Layout: log formatter, used to specify the format of log output. It is the formatter of log output.

Appender: the destination used to control log output in Log4j. Each appender represents an output target. There are the following types:

  • ConsoleAppender: output to the console;

  • FileAppender: output to the specified file;

  • Daily rolling file appender: generate a separate log file every day;

  • RollingFileAppender: limit the size of the log file and generate a new log file whenever the size limit is reached;

  • WriterAppender: send log information to any specified place in stream format;

Layout: log output format. Log4j provides the following layouts:

  • org.apache.log4j.HTMLLayout (layout in HTML table form),

  • org.apache.log4j.PatternLayout (layout mode can be flexibly specified),

  • org.apache.log4j.SimpleLayout (contains the level and information string of log information),

  • org.apache.log4j.TTCCLayout (including log generation time, thread, category, etc.)

Logger: the logger is the core class used by Log4j. Through the logger class, you can set the output level, output destination and output format of log messages;

Basic use

1. Introduce related dependencies: log4j

<dependency>

    <groupId>log4j</groupId>

    <artifactId>log4j</artifactId>

    <version>1.2.12</version>

</dependency> 

2. Import package: org apache. log4j. Logger

3. Create log object: static final logger logger = logger getLogger(XXX.class); // The parameter is the class file of the current class

summary

The interview inevitably makes people anxious. Everyone who has experienced it knows. But it's much easier if you anticipate the questions the interviewer will ask you in advance and come up with appropriate answers.

In addition, it is said that "the interview makes a rocket, and the work screws". For friends preparing for the interview, you only need to understand one word: brush!

Brush me, brush hard! Since I'm here to talk about the interview today, I have to come to the real interview questions. It didn't take me 28 days to do a "collection of analysis of interview questions for high posts in large Java front-line factories: Java foundation - intermediate - Advanced interview + SSM framework + distributed + performance tuning + microservice + concurrent programming + Network + design pattern + data structure and algorithm, etc."

Data collection method: click here to download for free

And in addition to simply brushing questions, You also need to prepare a copy [JAVA advanced core knowledge manual]: JVM, JAVA collection, JAVA multithreading concurrency, JAVA foundation, Spring principle, microservice, Netty and RPC, network, log, Zookeeper, Kafka, RabbitMQ, Hbase, MongoDB, Cassandra, design pattern, load balancing, database, consistency algorithm, JAVA algorithm, data structure, encryption algorithm, distributed cache, Hadoop, Spark , Storm, YARN, machine learning and cloud computing are best used to check leaks and fill vacancies.

JVM, JAVA collection, JAVA multithreading concurrency, JAVA foundation, Spring principle, microservice, Netty and RPC, network, log, Zookeeper, Kafka, RabbitMQ, Hbase, MongoDB, Cassandra, design pattern, load balancing, database, consistency algorithm, JAVA algorithm, data structure, encryption algorithm, distributed cache, Hadoop, Spark, Storm, YARN, machine learning Cloud computing is best used to check leaks and fill vacancies.

Keywords: Java Interview Programmer

Added by coolispaul on Tue, 21 Dec 2021 12:26:04 +0200