Log4j usage

zhaozj2021-02-16  83

How to use log4j

When practicing Spring's AOP today, I always give me a few warnings first, saying that I have not set up log4j, um, this is not good, so spending some time looking at Manual and online article, finally, it is almost the same, below It is some experience. 1, what is log4j? I believe that people who have developed over large a little system know that it is very important to output some of the information in the program development or running process to the screen or file, it can help debug (sometimes Debug can't play a role) and analysis, Log4j is an open source project to provide this feature, to download and learn more about more detailed content, or visit its official website: http://jakarta.apache.org/log4j. 2, log4j concept

There are three main components in Log4j, which are Logger, Appender and Layout, L

The OG4J allows developers to define multiple loggers, each logger has its own name, and the Logger is through the name to indicate the affiliate relationship. There is a Logger called root, it will always exist, and cannot be retrieved or referenced by name, and other loggers can be obtained through the logger.getlogger () method. Appender is used to indicate where all LOG information is stored, and multiple appenders are supported in log4j, such as

Console, FILES, GUI Components, NT Event Loggers, etc., a logger can have multiple appenders, that is, you can output log information to the screen while stored in a file. The role of layout is to control the output of log information, which is the formatting information. The log information to be output is defined in the log4j, which is defined by Debug, Info, Warn, Error and Fatal. When output, only the level of information specified in the configuration is true to real output, which is very convenient. To configure the content you want to output in different situations without changing the code, this is really convenient. 3, although the profile of log4j can be implemented in the program without a configuration file, but this method is obviously unsuitable in today's system development, and the configuration file can be used to use the configuration file. Log4j supports two format configuration files: XML format and Java's Property format, I prefer the latter, first look at a simple example, as follows: log4j.rootlogger = debug, stdout, r log4j.appender.stdout = org. apache.log4j.ConsoleAppender log4j.appender.stdout.layout = org.apache.log4j.PatternLayout # Pattern to output the caller's file name and line number. log4j.appender.stdout.layout.ConversionPattern =% 5p [% t] (% F:% L) -% m% n log4j.appender.r = org.apache.log4j.rollingfileAppender log4j.rapnder.r.file = example.log log4j.Appender.r.maxfilesize = 100kb # Keep One Backup file log4j. appender.r.maxbackupindex = 1 log4j.appender.r.Layout = org.apache.log4j.patternLayout log4j.Appender.r.Layout.conversionPattern =% P% T% C -% M% N first, is setting root, format For log4j.rootlogger = [level], appendername, ..., where Level is to set the level of the output information, the back is the destination of the Appender output,

Appendername is where the specified log information is output to which place. You can specify multiple output destinations at the same time.

Configuration information log output destination Appender, log4j.appender.appenderName = fully.qualified.name.of.appender.class log4j.appender.appenderName.option1 = value1 ... log4j.appender.appenderName.option = valueNLog4j provide syntax is Appender has the following: org.apache.log4j.consolerappender org.apache.log4j.fileAppender (file) org.apache.log4j.dailyRollingFileAppender (a log file is generated) org.apache.log4j.rollingFileAppender When the file size arrives at the specified size, it generates a new file) org.apache.log4j.writerappender (layout) of the log information (layout), which is: log4j.appender.Appendername . ret.Name.Of.Layout.class log4j.Appender.Appendername.Layout.Option1 = value1 .... log4j.Appender.Appendername.Layout.Option = Valuenlog4J provides the following: ORG. Apache.log4j.htmlLayout (layout in HTML form), org.apache.log4j.patternlayout (can be flexibly specified), org.apache.log4j.simplelayout (level and information string containing log information), ORG. Apache.log4j.tccLayout (including information, thread, category, etc.) 4, log4j uses log4j in the program to use log4j in its own class, first declare a static variable logger logger = logger.getlog ("ClassName "); Before use, configure it with PropertyConfigurator.configure (" Profile "). Now you can use, use as follows: logger.debug ("debug message") or logger.info ("info message"), look at a small example of the following: import com.lorcel.bar; import org.apache.log4j.logger ; import org.apache.log4j.PropertyConfigurator; public class MyApp {static Logger logger = Logger.getLogger (MyApp.class.getName ());. public static void main (String [] args) {// BasicConfigurator replaced with PropertyConfigurator PropertyConfigurator .configure (""

Log4j.properties "); // log4j.properties file can be placed in the classpath or web-inf / classes directory logger.info (" Entering Application. "); bar bar = new bar (); bar.doot (); logger .info ("exiting application.");}}

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

New Post(0)