Use log4j for log file Recording classic steps:
01. Create a Lib folder in the project and copy the downloaded jar package to the folder
02. Right-click on the jar package that has been copied, then perform the operation
There will be an extra project in the project that introduces the external library.
03. Create a name called Log4j.properties file in the SRC directory
04. Write and paste the content of the configuration file that has been written, and the encoding method can be amended. write the Log4j.properties file as follows:
# # # Set Logger output level and output Destination # # #log4j. rootlogger=debug, logfile### output log information to file: Jbit.log # # #log4j. appender.logfile= org.apache.log4j.fileappenderlog4j.appender.logfile.file=jbit.loglog4j.appender.logfile.layout= Org.apache.log4j.patternlayoutlog4j.appender.logfile.layout.conversionpattern=%d{yyyy-mm-dd HH:mm:ss}%l%F%p%m%n
In the same class as the main method, add the following code:
public static Logger Logger=logger.getlogger ("log4j");p ublic static void Main (string[] args) {logger.debug ("log file!"); SYSTEM.OUT.PRINTLN ("Success!");}
Two, where [level] is the log output levels, a total of 5 levels
①fatel: Indicates that a critical error event will cause the application to exit
②error: Indicates that although an error event occurs, it still does not affect the system's continued operation
③warn: A scenario that indicates a potential error
④info: Indicates the message at a coarse-grained level, emphasizing the application's running process
⑤debug: Indicates fine-grained information events that are very helpful for debugging applications
Third, Appender for the log output destination, LOG4J provides the following types of Appender:
Org.apache.log4j.ConsoleAppender (console), Org.apache.log4j.FileAppender (file), Org.apache.log4j.DailyRollingFileAppender (generates a log file every day), Org.apache.log4j.RollingFileAppender (creates a new file when the file size reaches the specified size), Org.apache.log4j.WriterAppender (send log information in stream format to any specified location)
Four, Layout: Log output format,log4j provides the following types of layout:
Org.apache.log4j.HTMLLayout (layout in an HTML table), Org.apache.log4j.PatternLayout (with the flexibility to specify layout mode), Org.apache.log4j.SimpleLayout (contains the level and information string for log information), Org.apache.log4j.TTCCLayout (contains information such as the time, thread, category, etc.) of the log generation
Printing Parameters : log4j Format the log information in a print format similar to the printf function in C , as follows:
%m The output priority of the message specified in the output code, which is the number of milliseconds that the debug,info,warn,error,fatal %r output is consumed from the application boot to output the log information C output belongs to the class, usually the full name of the class %t output The thread name that generated the log event %n output a carriage return newline character, the Windows platform is "\ r \ n", the UNIX platform is "\ n" %d< /c6> the date or time of the output log time, the default format is ISO8601, or you can specify a format after that, such as:%d{yyy MMM dd HH:mm:ss, SSS}, output similar to: October 18, 2002 22:10:28, 921 %l where the output log event occurs, including the class name, the thread that occurred, and the number of rows in the code. Example: Testlog4.main (testlog4.java:10)
Log4j.properties Configuration