Introductory Introduction to LOG4J Learning
Brief introduction:
LOG4J is an Apache open source project, through the use of log4j, we can control the log information delivery destination is the console, files, GUI components, even the socket server, NT Event recorder, UNIX syslog daemon, etc. We can also control the output format of each log, and by defining the level of each log information, we can control the log generation process more carefully. Most interesting of all, these can be configured flexibly with a single configuration file, without the need to modify the code of the application.
In addition, by log4j other language interfaces, you can use log4j in C, C + +,. Net, PL/SQL programs with syntax and usage as in Java programs, enabling a unified, consistent Log component module for multilingual distributed systems. and by using a variety of third-party extensions, you can easily integrate log4j into a Java EE, Jini, or even SNMP application.
Application Routines:
1. Download the log4j jar package
: http://download.csdn.net/source/2637105
2. Create a new project and import the downloaded jar package
3. Generate a log4j.properties file under the SRC folder, create a new log folder under the project root directory, and create a new Log.log file and Errorlog.log file under the folder.
The concrete structure is as follows:
PS: Here the Log4j.properties file is mainly log4j related configuration, and log.log is generally normal log, and Errorlog.log is the error log
4. Open Log4j.properties File
Enter the following information:
Log4j.rootlogger=info, stdout, log, errorloglog4j. logger=search,test## #Console # # #log4j. appender.stdout = Org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target = System.outlog4j.appender.stdout.layout = Org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern =%d{absolute} [%t] [%p]:%l-%m%n### Lo G # # # Log4j.appender.log = Org.apache.log4j.DailyRollingFileAppenderlog4j.appender.log.File = log/ Log.loglog4j.appender.log.Append = Truelog4j.appender.log.Threshold = infolog4j.appender.log.datepattern= '. ' Yyyy-mm-ddlog4j.appender.log.layout = Org.apache.log4j.PatternLayoutlog4j.appender.log.layout.ConversionPattern = %-d{yyyy-mm-dd HH:MM:SS} [%t]%m%n### Error # # #log4j. Appender.errorlog = Org.apache.log4j.DailyRollingFileAppenderlog4j.appender.errorlog.File = log/ Errorlog.loglog4j.appender.errorlog.Append = Truelog4j.appender.errorlog.Threshold = ERROR Log4j.appender.errorlog.datepattern= '. ' Yyyy-mm-ddlog4j.appender.errorlog.layout = Org.apache.log4j.PatterNLayoutlog4j.appender.errorlog.layout.ConversionPattern =%-d{yyyy-mm-dd HH:mm:ss} [%t]%m%n
5. Write the test function, create a new Java class, the file name is testlog4j, the content is as follows
Import Org.apache.log4j.logger;public class Testlog4j {public static void main (string[] args) {Logger Logger = Logger.getl Ogger (Testlog4j.class); Logger.info ("Zhujiadun");}}
6. Operation Result:
Console information:
Log.log information in the log file
Reference Documentation:
http://www.hudong.com/wiki/log4j
http://download.csdn.net/source/2637095
Log4j Getting Started tutorial (i) Getting Started routines