Use of loger4j

brief introduction

Log4j consists of three important components:
1. Priority of log information (Loggers),
2. The output destination of log information (Appenders),
3. Output format of log information (Layouts).

  • The priority of log information from high to low includes ERROR, WARN, INFO and DEBUG, which are used to specify the importance of this log information respectively;

  • The output destination of log information specifies whether the log will be printed to the console or file; The output format controls the display content of log information.

1. log4j priority

  • Log4j is divided into OFF, fat, ERROR, WARN, INFO, DEBUG, ALL or the level you define.
  • Log4j recommends using only four levels. The priority from high to low is ERROR, WARN, INFO and DEBUG. Through the levels defined here, you can control the switch to the corresponding level of log information in the application.

If a log request with level p occurs in a Logger with level Q, if p > = q, the request will be enabled. This is the core principle of Log4j. For example, if the INFO level is defined here, all DEBUG level log information in the application will not be printed.

2. Use of output source

Selectively enabling or disabling log requests is only part of log4j. Log4j allows log requests to be output to multiple output sources. In the words of log4j, an output source is called an Appender. The Appender includes console, files, GUI components, remote socket servers, JMS, NT Event Loggers, and remote UNIX Syslog daemons. It can also do asynchronous recording

2.1 ConsoleAppender

If used ConsoleAppender,that log Information will be written to Console. The effect is equivalent to printing information directly to System.out Yes.

2.2 FileAppender

use FileAppender,that log The information will be written to the specified file. This should be used more often. Accordingly, it should be specified in the configuration file log The file name of the output. The following configuration specifies log The file name is dglog.txt log4j.appender.A2.File=dglog.txt

2.2 DailyRollingAppender

use FileAppender Can log The information is output to a file, but it is inconvenient to read if the file is too large. Then you can use DailyRollingAppender. DailyRollingAppender Can put Log The information is output to a file distinguished by date. A configuration file will be generated every day log File, each log The document only records the of the day log Information:

log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender

log4j.appender.A2.file=dglog

log4j.appender.A2.DatePattern='.'yyyy-MM-dd

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

log4j.appender.A2.layout.ConversionPattern= %5r %-5p %c{2} - %m%n

2.3 RollingFileAppender

When the file size reaches the specified size, a new file is generated.
log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File= ../logs/dglog.log 
# Control the maximum log file size log4j.appender.R.MaxFileSize=100KB 
# Archive log files (one backup file here) log4j.appender.R.MaxBackupIndex=1 log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n 
This configuration file specifies the output source R,Is a rotation log file. The largest file is 100 KB,When a log file reaches its maximum size, Log4J Will automatically example.log Rename to dglog.log.1,Then rebuild a new one dglog.log Documents, rotating in turn.

2.4 WriterAppender

Send log information in stream format to any specified place.

3. Layout configuration

3.1 layout style

org.apache.log4j.HTMLLayout(with HTML Tabular layout), org.apache.log4j.PatternLayout((layout mode can be specified flexibly)
org.apache.log4j.SimpleLayout(Level and information string containing log information)
org.apache.log4j.TTCCLayout((including log generation time, thread, category, etc.)

3.2 format

%m Output the message specified in the code
%p Output priority, i.e DEBUG,INFO,WARN,ERROR,FATAL %r Output from application startup to output log The number of milliseconds the message took 
%c The category to which the output belongs is usually the full name of the class 
%t Output the name of the thread that generated the log event 
%n Output a carriage return line feed, 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, such as:%d{yyy MMM dd HH:mm:ss,SSS},Output similar: October 18, 2002 22:10:28921 
%l Output the location of the log event, including the category name, the thread that occurred, and the number of lines in the code. give an example: Testlog4.main(Test Log4.java:10)

4. properties instance

# priority
log4j.rootLogger=DEBUG 
# Record the DAO layer log into daolog and alllog log4j logger. DAOLog=DEBUG,A2,A4
# log to the business. log layer logger. Businesslog=DEBUG,A3,A4 
# A1 -- print to screen
 log4j.appender.A1=org.apache.log4j.ConsoleAppender
 log4j.appender.A1.layout=org.apache.log4j.PatternLayout
 log4j.appender.A1.layout.ConversionPattern=%-5p [%t] %37c %3x - %m%n
 
#A2 -- print to DAOLog file -- dedicated to DAO layer
log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A2.file=DAOLog
log4j.appender.A2.DatePattern='.'yyyy-MM-dd
log4j.appender.A2.layout=org.apache.log4j.PatternLayout 
log4j.appender.A2.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n 

wait...

5. Log4j advanced configuration

5.1 configure the package path for logging

Configure log4j logger. com.int97 = debug, only the package is com The debug information of the code in int97 is output to the specified output source.

5.2 support log level inheritance

If log4j Rootlogger = debug. The default level of other loggers is debug. You can configure log4j additivity. XXX = true / false to turn on or off the inheritance function; If false, it means that the appender of the Logger does not inherit its parent Logger; If it is true, it inherits. In this way, it has its own settings and the settings of the parent Logger.

5.3 setting log output levels for different Appender s

Usually, all levels of output are put in one file. If the level of log output is DEBUG level, it is not very convenient to find exceptions. Log4j provides the function of saving only the exception log, which can be realized only by modifying the Appender Threshold in the configuration, such as the following example:

### Save exception information to a separate file ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = logs/error.log
#True indicates that the message is added to the specified file. false overwrites the content of the specified file. The default is true
log4j.appender.D.Append = true
## Only output logs above ERROR level!!!
log4j.appender.D.Threshold = ERROR 

6. Implement the logger with java code

Step 1: static Logger logger = Logger.getLogger ( ServerWithLog4j.class.getName () )

Step 2:
# Read configuration file
# Automatically and quickly use the default Log4j environment.
BasicConfigurator.configure (): 
# Read the configuration file written using Java's properties file
PropertyConfigurator.configure ( String configFilename). 
# Read configuration file in XML
DOMConfigurator.configure ( String filename ). 

Step 3: insert record information
Logger.debug ( Object message ) ;
Logger.info ( Object message ) ;
Logger.warn ( Object message ) ;
Logger.error ( Object message ) ;

Keywords: Java log4j

Added by roba59 on Tue, 08 Mar 2022 01:23:55 +0200