Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Concise use of log4j's multi-logger logging

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Log should be one of the basic functions of an application software.

Using the java language, I am bound to use a log library. I use log4j's log library. There are many articles on the Internet, but there are not many clear introductions of different functions and examples of logger. At least I have flipped through a lot of web pages, and then get these functions according to the experiment.

I think there are two main concepts in log4j: logger and appender.

Logger is the main concept of the logging component, which is used to specify that different packages use different logging levels, or to specify a logger component so that the definition of the logger can be obtained using getLogger (component name) when using it. Logger mainly specifies a log level, as well as several specific loggers appender, so that the program can choose which logger to log, and what level of logging. Appender is a component that specifically performs logging to a file or terminal. How to use multiple logger components

GetLogger (parameter), which is used if the parameter can find the corresponding logger in the configuration file, or rootlogger if it cannot be found.

The code is easy to use, and the following libraries are introduced into the java project

Package logStudy;import org.apache.log4j.Logger;public class useLog4J {public static Logger logger1 = Logger.getLogger (useLog4J.class); public static Logger logger2 = Logger.getLogger ("logStudy"); public static void main (String [] args) {int I = 100; while (I > 0) {logger1.debug ("this is a test log data, level should be debug") Logger1.debug ("this is a test log data, level should be debug"); logger1.info ("this is a test log data, level info"); logger1.info ("this is a test log data, level info"); logger1.info ("this is a test log data, level info") Logger1.debug ("this is a test log data, level should be debug"); logger1.warn ("this is a test log data, level should be warn"); Imuri;} logger2.debug ("logger2 debug"); logger2.info ("logger2 info") Logger2.warn ("logger2 warn"); logger2.fatal ("logger2 fatal");}}

In the above code, two logger components are used, mainly to demonstrate the difference between the two. Logger1 uses the content of rootlogger, while logger2 uses a logger named logStudy, and this logger uses only CONSOLE. The following configuration will be described.

The code for using log4j is very simple and straightforward, and the main work is in the configuration file, that is, the configuration in log4j.properties. The configuration example in the example is as follows.

ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF### setting level and destination (multiple destinations here) # log4j.rootLogger = INFO,CONSOLE,HelloLog,Hello2Log### is the first use. By default, the logStudy here is a package, that is, when this package logs, only logs at or above the WARN level are recorded. # the second use is that you can actively use this logger named logStudy through Logger.getLogger ("logStudy") anywhere in the program. Log4j.logger.logStudy=WARN,CONSOLE### configuration log information output destination Appender, where the first appender is named CONSOLE, and the output method is org.apache.log4j.ConsoleAppender, that is, output to the console log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppenderlog4j.appender.CONSOLE.Target = System.outlog4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout# output format For specific format content, you can find log4j.appender.CONSOLE.layout.ConversionPattern =% c% d {ABSOLUTE}% 5p% c {1}:% L -% m%n# to create a log every day log4j.appender.HelloLog = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.HelloLog.File =.\\ log\\ debug.log#log4j.appender.HelloLog.File = / var/log/debug.loglog4j.appender.HelloLog.Append = true## only output DEBUG level to Log log4j.appender.HelloLog.Threshold = INFO#'.'yyyy-MM-dd on: generate a new file every day log4j.appender.HelloLog.DatePattern ='. 'yyyy-MM-ddlog4j.appender.HelloLog.layout = org.apache.log4j.PatternLayoutlog4j.appender.HelloLog.layout.ConversionPattern =%-d {yyyy-MM-dd HH:mm:ss} [% tGV% r]-[% p] [% c {1}:% L] [% M] % m%nlog4j.additivity.logStudy = false # does not inherit the root rootLogger This will not output the log to the appender specified in rootLogger. # when the text file is 3KB, create a new file log4j.appender.Hello2Log = org.apache.log4j.RollingFileAppenderlog4j.appender.Hello2Log.File =.\ log\\ debug2.log#log4j.appender.Hello2Log.File = / var/log/debug.loglog4j.appender.Hello2Log.Append = true## only output logs above the DEBUG level log4j.appender.Hello2Log.Threshold = INFOlog4j.appender.Hello2Log.MaxFileSize = 2KBlog4j.appender.Hello2Log.MaxBackupIndex = 5#'.'yyyy-MM-dd: A new file log4j.appender.Hello2Log.layout = org.apache.log4j.PatternLayoutlog4j.appender.Hello2Log.layout.ConversionPattern =%-d {yyyy-MM-dd HH:mm:ss} [% tRV% r]-[% p] [% c {1}:% L] [% M]% m% n is generated every day

The following is for each introduction:

1 、 log4j.rootLogger

Log4j.rootLogger= [level], appenderName, appenderName2,...

Level: is the log level of log4j. The priority from high to low is FATAL, ERROR, WARN, INFO, DEBUG, ALL. AppenderName: specifies where the log information is output.

2. Configure the log information output destination Appender. Its syntax is as follows:

Log4j.appender.appenderName = fully.qualified.name.of.appender.class

Among them, the appender provided by Log4j includes the following:

Org.apache.log4j.ConsoleAppender (console)

Org.apache.log4j.FileAppender (file)

Org.apache.log4j.DailyRollingFileAppender (generates a log file every day)

Org.apache.log4j.RollingFileAppender (generates a new file when the file size reaches the specified size)

Org.apache.log4j.WriterAppender (sends log information to any specified place in stream format)

3. After setting up appender, the configuration for appender

That is, after setting where to output, other configuration options, many configuration options are the same, and each appender type has some special options.

1.ConsoleAppender option

Threshold=WARN: specifies the lowest level of output of log messages.

ImmediateFlush=true: the default value is true, which means that all messages are output immediately.

Target=System.err: default is: System.out, which specifies the output console

2.FileAppender option

File=log.txt: specifies that the message is output to the log.txt file.

Append=false: the default value is true, which adds the message to the specified file, and false means that the message overwrites the specified file content.

3.DailyRollingFileAppender option

Append=false: the default value is true, which adds the message to the specified file, and false means that the message overwrites the specified file content.

DatePattern='.'yyyy-ww: scroll the file once a week, that is, generate a new file every week. Of course, you can also specify by month, week, day, hour and minute.

4.RollingFileAppender option

MaxFileSize=100KB: the suffix can be KB, MB or GB. When the log file reaches this size, it will scroll automatically, moving the original content to the mylog.log.1 file.

MaxBackupIndex=2: specifies the maximum number of scrolling files that can be generated.

4. Introduction to output format

Log output format, the parameters used are as follows, add as needed:% p: output log information priority, that is, DEBUG,INFO,WARN,ERROR,FATAL,%d: the date or time of the output log point in time, the default format is ISO8601, or you can specify the format after that, for example:% d {yyy MMM dd HH:mm:ss,SSS}, the output is similar: 22:10:28 on October 18, 2002 921% r: output the number of milliseconds it took since the application was started to output the log information% c: the category to which the output log information belongs, usually the full name of the class% t: the name of the thread that produced the log event% l: the location of the output log event, which is equivalent to the combination of% C.% M (% FRV% L), including the category name, the thread that occurred, and the number of lines in the code. For example: Testlog4.main (TestLog4.java:10)% x: outputs the NDC (nested diagnostic environment) associated with the current thread, especially in multi-client multithreaded applications such as java servlets. %%: output a "%" character% F: output the name of the file in which the log message was generated% L: line number in the output code% m: output the message specified in the code, generated log specific information% n: output a carriage return newline character, Windows platform is "\ r\ n" The Unix platform outputs log information for "\ n". Line wrapping can add modifiers between% and pattern characters to control its minimum width, maximum width, and text alignment. For example: 1)% 20c: specifies the name of the output category, with a minimum width of 20. If the name of the category is less than 20, it is right-aligned by default. 2)%-20c: specifies the name of the output category, with a minimum width of 20, and the "-" sign specifies left alignment if the name of the category is less than 20. 3)% .30c: specify the name of the output category. The maximum width is 30. If the name of the category is greater than 30, the extra characters on the left will be truncated, but if it is less than 30, there will be no spaces. 4)% 20.30c: if the name of the category is less than 20, fill in the blanks and align to the right, and if the name is longer than 30 characters, truncate the characters from the left.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report