Use of log4j

1. Simple use

Only one jar package of log4j needs to be imported, and the configuration file under the src directory will be loaded by default

1. Print log to console configuration file content

#Set the log level to info and output to the console
log4j.appender.console = org.apache.log4j.ConsoleAppender
#Log output style

Test code:

package util;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Test {
    public static Logger logger = Logger.getLogger(Test.class);
    public static void logTest(){
        logger.trace("trace Level log");"info Level log");
        logger.debug("debug Level log");
        logger.warn("warn Level log");
        logger.error("error Level log");
        logger.fatal("fatal Level log");

    public static void main(String[] args) {

Output results:

Custom profile:

2. Print the log to the specified file configuration file content

log4j.appender.file = org.apache.log4j.FileAppender

The test code does not change, and the result is (the default logging method is to append records in the file):

2. Profile details

1. Log level

Log4j only four levels are recommended, with the priority from high to low being ERROR, WARN, INFO and DEBUG.

Level details, log from the specified level down the arrow to the OFF level (ALL → TRACE → DEBUG → INFO → WARN → ERROR → fat → OFF):

Level describe
ALL Levels include custom levels
DEBUG Specifying fine-grained information events is the most useful application debugging
ERROR Error events may still allow the application to continue
FATAL Specifies a very serious error event that may cause the application to abort
INFO Messages that specify information that highlights the operation of an application at a coarse-grained level
OFF This is the highest level, in order to turn off logging
TRACE Specify information events with lower granularity than DEBUG
WARN Specify potentially hazardous situations

Overall introduction

#log4j.rootLogger = [level] appenderName
#Level is the log level, appenderName is the place where the specified log output can be written, so the specified location can be recorded to the log at the same time
#appenderName can be customized as long as the following configuration corresponds to it
#For example, output to console is defined as A, output to file e:/log1/log.log is defined as B, and output to file e:/log2/log.log is defined as C
#log4j.appender.A = org.apache.log4j.ConsoleAppender
#Specify layout
#log4j.appender.B.File = e:/log1/log.log
#log4j.appender.C.File = e:/log2/log.log

#Among them, the Appenders provided by Log4j are as follows:
#org.apache.log4j.ConsoleAppender (console),
#org.apache.log4j.FileAppender (file),
#org.apache.log4j.DailyRollingFileAppender (generate a log file every day),
#org.apache.log4j.RollingFileAppender (a new file is generated when the file size reaches the specified size),
#org.apache.log4j.WriterAppender (send log information to any specified place in stream format)

#Log4j provides the following layout s:
#org.apache.log4j.SimpleLayout (contains the level and information string of log information),
#org.apache.log4j.TTCCLayout (including the time, thread, category, etc. of log generation)

#Log4j formats the log information in a print format similar to the printf function in C language. The print parameters are as follows:% m the message specified in the output code
#%p output priority, that is, DEBUG, INFO, WARN, ERROR, fat
#%r input the number of milliseconds from the start of the application to the output of the log information
#%c output the class to which it belongs, usually the full name of the class
#%Tput the name of the thread that generated the log event
#%N output a carriage return line feed character, Windows platform is "rn", Unix platform is "n"
#%D output the date or time of the log time point. The default format is ISO8601. You can also specify the format after it, for example:% d{yyy MMM dd HH:mm:ss,SSS}. The output is similar to: 22:10:28921, October 18, 2002
#%l the location where the output log event occurs, including the category name, the thread that occurred, and the number of lines in the code.
#%M method name
#%Lines of L
#Example: [% d {yyyy m m DD HH: mm: SS}] - [% T -% P] - [% C -% m (% L)]:% m% n

Keywords: log4j Apache Windows Unix

Added by jay_bo on Mon, 20 Apr 2020 18:53:38 +0300