Log4j Use Notes in Java Web Development

xiaoxiao2021-03-06  39

Log4j is relatively simple (although I toss for half a day)

).

This article

The article written is good, it is Chinese, and

This e-article, how to use log4j in Web development in detail.

It mainly summarizes the use of log4j in Web.

Overview:

To output debugging information in the program, you must first get the logger. To get Logger, you must configure it first. It is always possible to get root Logger by logger.getrootlogger (), but basically doesn't do this. (Experts say, I don't know why) You can simply write a servlet to initialize and load it when starting. It seems that there is another way to initialize and provide logger with the LogFactory class.

How to configure and initialize:

There are three configurations: configuration, Java configuration file, XML configuration file. Usually the Java configuration file is simple and convenient, I like it. About the initialization in the Web application, the Chinese version of the official website of Log4J (English) Webwing translation. There is a detailed example, worth learning. Initialization Servlet section. The simplest configuration: Take the configuration file log4j.configuration, put it under Web-INF / CLASSES, everything is OK! It is initialized without using the method of Article 2. The Java profile uses the PropertyConfigurator, XML configuration files using Domconfigurator. BasicConfigurator.configure () can also be used, but most of them are not used. The Logger class is a subclass of the Category class, which will gradually replace the Category class. In the previous configuration example, there are many Category, and it is recommended to use the Logger class. Several appenders (can refer to example http://blog.9cbs.net/kengking/archive/2004/11/15/log4j.aspx)

Console: org.apache.log4j.ConsoleAppender Files: org.apache.log4j.FileAppender GUI components: ...... Remote socket servers: ...... NT Event Loggers: ...... Remote UNIX Syslog daemons: ...... PatternLayout format (like the difference between a)

Symbol Description% R The millisecond% T consumed after the start of the program indicates the thread% P generated by the log record request to represent the priority of the log statement, the class name% M% N represents the content% R of log information% R self-application Start to output the log information consumed by the millisecond or time of the log time point, the default format is ISO8601, or it can be specified in the format, such as:% D {YYY MMM DD HH: mm: SS, SSS}, Output Similar: October 18, 2002 22: 28, 921% L log event occurs, including category names, threads, and rows in the code.

Logger in the program

Get the Logger's Instance -> Output debugging information, including: Debug, Info, Warn, Error, Fatal, a total of 5, usually Info is enough. Creating Logger in the program can pass Logger Logger = logger.getlogger (this.GetClass (). GetName ()); (if there is logfactory to generate, for example: private log = logfactory.getFactory (). GetInstance (this .getclass (). getname ());) If this Logger does not have this logger in the JVM, create a new Logger. When you create a Logger, if this logger exists in the configuration file, press Configuration to create, otherwise the rootlogger is inherited. This type of code is relatively efficient, and it is the most efficient time without debugging information. IF (logger.isinfoenabled ()) logger.info (this.getclass (). getname ()); ......

转载请注明原文地址:https://www.9cbs.com/read-65649.html

New Post(0)