How to use log4j in java (reproduced)

How to use log4j in java

 
(1) The purpose of log4j: It can be used as a log file, that is, you can input some data that we run in the program into the .log file. For example, if you save a piece of information in the database, you can also use log4j The log file is used to record the information you saved, and it is more complete. It can include 1, the time of saving, 2, the function called 3, what information you want to record, what information you want to save, and so on.
(2) How to use it? Using log4j to make log files requires three things:
1. The jar package, that is, log4j.jar The following is the download address of the jar package and the download address   of log4j.jar : http://ishare.iask.sina.com.cn/f/12460553.html?from=like  Use after successful download Eclipse can be imported into the project.
2. The configuration file of log4j. There are two configuration files for log4j. You only need to use one of them. They are the xml file and the java properties file (properties). I think it will be simpler to use the java properties file, so I mainly explain the properties file here. How to use. First, create the :log4j.properties file in the src directory of the project, and then paste the following code (configuration file) into the modified file to save: (Note: The line marked with # is a comment, not represented by java note)
 
log4j.rootLogger=DEBUG,CONSOLE,A1,R,FILE1,FILE2,FILE3
log4j.addivity.org.apache = true 
 
# apply to console 
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 
log4j.appender.CONSOLE.Threshold=INFO 
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 
 
# Create a new log every day 
log4j.appender.A1 = org.apache.log4j.DailyRollingFileAppender 
log4j.appender.A1.File=C\:/log4j.log 
log4j.appender.A1.Threshold=DEBUG 
log4j.appender.A1.DatePattern='.'yyyy-MM-dd 
log4j.appender.A1.layout=org.apache.log4j.PatternLayout 
log4j.appender.A1.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}\:%L \: %m%n 
 
 
log4j.appender.R = org.apache.log4j.DailyRollingFileAppender
# output a log file every day
log4j.appender.R.DatePattern='_'yyyy-MM-dd'.log'
#Yesterday's log file name Sendmsg+"Yesterday's date".log
log4j.appender.R.File=C\:/log4j/debusssssg.log 
#Path to the log file, ${catalina.home} is under Tomcat
log4j.appender.R.layout=org.apache.log4j.HTMLLayout 
#log file output format 
log4j.appender.R.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n 
 
 
#Apply to file (debug file)
log4j.appender.FILE1=org.apache.log4j.FileAppender 
log4j.appender.FILE1.File=C\:/log4j/debug.log 
log4j.appender.FILE1.Threshold=DEBUG
log4j.appender.FILE1.Append=true
log4j.appender.FILE1.layout=org.apache.log4j.PatternLayout 
log4j.appender.FILE1.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n 
 
 
#Apply to files (INFO files)
log4j.appender.FILE2=org.apache.log4j.FileAppender 
log4j.appender.FILE2.File=C\:/log4j/INFO.log 
log4j.appender.FILE2.Threshold=INFO
log4j.appender.FILE2.Append=true
log4j.appender.FILE2.layout=org.apache.log4j.PatternLayout 
log4j.appender.FILE2.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n 
 
#Apply to file (error file)
log4j.appender.FILE3=org.apache.log4j.FileAppender 
log4j.appender.FILE3.File=C\:/log4j/error.log 
log4j.appender.FILE3.Threshold=ERROR
log4j.appender.FILE3.Append=false 
log4j.appender.FILE3.layout=org.apache.log4j.PatternLayout 
log4j.appender.FILE3.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n 
 
 
We can regard each log4j.appender.Xx as an output = the right side is the corresponding way to output, such as
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.FILE3=org.apache.log4j.FileAppender  
Wait
 
We can put each log4j.appender.Xx.File = the address and filename of the saved file, like:
log4j.appender.FILE1.File=C\:/log4j/debug.log 
 
We can put each log4j.appender.Xx.Threshold = what level this is (for level concept, please see the log ) , such as:
log4j.appender.FILE3.Threshold=ERROR
 
Wait, all of this information can be found on Baidu, so I won't go into too much detail here.
 
 

Log4j consists of three important components: Loggers, Appenders, and Layouts.

A). Obtaining or creating a Logger object :
Logger is specified as an entity, identified by the name of a String class. The name of Logger is case-sensitive, and there is an inheritance relationship between the names. The child name is prefixed with the parent name and separated by a dot ".". For example, xy is the father of xyz.
root Logger (root Logger) is the ancestor of all Logger, it has the following properties:
1. It always exists.
2. It is not obtainable by name.

 

B) Log level
Each Logger is assigned a log level, which is used to control the output of log information. The log level is divided into:
A: off the highest level, used to turn off all log records.
B: fatal indicates that every serious error event will cause the application to exit.
C: error indicates that although an error event occurs, it still does not affect the continued operation of the system.
D: warm indicates a potential error situation.
E:info generally and at a coarse-grained level, emphasizes the entire run of the application.
F: debug is generally used at a fine-grained level and is very helpful for debugging applications.
G:all Lowest level, used to turn on all logging.

 

C) Output Appender
Appender is used to specify where the log information is output, and multiple output destinations can be specified at the same time. Log4j allows information to be output to many different output devices. A log information output destination is called an Appender.
Each Logger can have one or more Appenders, and each Appender represents a log output destination. You can use Logger.addAppender(Appender app) to add an Appender to the Logger, or you can use Logger.removeAppender(Appender app) to remove an Appender for the Logger.
The following are several common output destinations for Log4j.
a: org.apache.log4j.ConsoleAppender: Output log information to the console.
b: org.apache.log4j.FileAppender: Output log information to a file.
c:org.apache.log4j.DailyRollingFileAppender: Output log information to a log file, and output to a new log file every day.
d: org.apache.log4j.RollingFileAppender: Output the log information to a log file and specify the size of the file. When the size of the file reaches the specified size, the file will be renamed automatically and a new file will be generated.
e: org.apache.log4j.WriteAppender: Send log information to any specified place in stream format.
f::org.apache.log4j.jdbc.JDBCAppender: Output log information to the database through JDBC.

 

Finally, create a Log class, copy the following code into it and run it to see that a log file will be generated under the C drive

import org.apache.log4j.Logger;  

public class Log {

     private static Logger logger = Logger.getLogger(Log.class);  

     public static void main(String[] args) {  

      // log debug level information   

       logger.debug("This is debug message.");  

       // Record info at info level   

       logger.info("This is info message.");  

       // record error level information   

       logger.error("This is error message.");  

   }  

}

 

 

 

Thanks https://www.cnblogs.com/bingbingJava/p/4103645.html 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326228955&siteId=291194637