Log4j usage Summary

Source: Internet
Author: User
Tags ranges truncated

I. Introduction
Log4j is an open-source project of Apache. By using log4j, we can control the log information delivery destination, including the console, files, Gui components, and even the interface service set, NT event recorder, and Unix Syslog daemon; we can also control the output format of each log. By defining the level of each log information, we can control the log generation process in more detail.
Log4j consists of three important components: log information priority, log information output destination, and log information output format. The log priority ranges from high to low, including error, warn, info, and debug, which are used to specify the importance of the log information; the log output destination specifies whether the log will be printed to the console or a file. The output format controls the display of log information.

Ii. Configuration File
In fact, you can configure the log4j environment in code instead of using the configuration file. However, using the configuration file will make your application more flexible.
Log4j supports two configuration file formats: XML and properties. The following describes how to use the properties format as a configuration file:
Example:
Log4j. rootlogger = info, A1
Log4j. appender. A1 = org. Apache. log4j. leleappender
Log4j. appender. a1.layout = org. Apache. log4j. patternlayout
Log4j. appender. a1.layout. conversionpattern = %-4r %-5 p [% T] % 37C % 3x-% m % N

1. Configure the root logger with the Syntax:
Log4j. rootlogger = [level], appendername, appendername ,...
Level is the log record priority, which can be off, fatal, error, warn, info, debug, all, or the level you define. We recommend that you use only four levels of log4j. The priority ranges from high to low: error, warn, info, and debug. By defining the level here, you can control the switch to the corresponding level of log information in the application. For example, if the info level is defined here, all debug-level logs in the application will not be printed.
Appendername specifies where the log information is output. You can specify multiple output destinations at the same time.

2. Configure the log output destination appender. Its syntax is:
Log4j. appender. appendername = fully. Qualified. Name. Of. appender. Class
Log4j. appender. appendername. option1 = value1
...
Log4j. appender. appendername. Option = valuen
Log4j provides the following types of appender:
Org. Apache. log4j. leleappender (console ),
Org. Apache. log4j. fileappender (file ),
Org. Apache. log4j. dailyrollingfileappender (a log file is generated every day ),
Org. Apache. log4j. rollingfileappender (a new file is generated when the file size reaches the specified size ),
Org. Apache. log4j. writerappender (send log information to any specified place in stream format)
(1). leleappender options
Threshold = warn: specify the minimum output level of log messages.
Immediateflush = true: the default value is true, meaning that all messages will be output immediately.
Target = system. Err: system. Out by default, which specifies the output Console
(2). fileappender options
Threshold = warn: specify the minimum output level of log messages.
Immediateflush = true: the default value is true, meaning that all messages will be output immediately.
File1_mylog.txt: refers to the output to the mylog.txt file.
Append = false: The default value is true. To add a message to a specified file, false means to overwrite the specified file content.
(3). dailyrollingfileappender Option
Threshold = warn: specify the minimum output level of log messages.
Immediateflush = true: the default value is true, meaning that all messages will be output immediately.
File1_mylog.txt: refers to the output to the mylog.txt file.
Append = false: The default value is true. To add a message to a specified file, false means to overwrite the specified file content.
Datepattern = '. 'yyyy-ww: The file is rolled every week, that is, a new file is generated every week. You can also specify monthly, weekly, daily, and hour/minute. The corresponding format is as follows:
1) '. 'yyyy-MM: monthly
2) '. 'yyyy-ww: weekly
3) '. 'yyyy-mm-DD: daily
4) '. 'yyyy-mm-dd-A: twice a day
5) '. 'yyyy-mm-dd-hh: Hourly
6) '. 'yyyy-mm-dd-hh-MM: minute
(4). rollingfileappender Option
Threshold = warn: specify the minimum output level of log messages.
Immediateflush = true: the default value is true, meaning that all messages will be output immediately.
File1_mylog.txt: refers to the output to the mylog.txt file.
Append = false: The default value is true. To add a message to a specified file, false means to overwrite the specified file content.
Maxfilesize = 100kb: The suffix can be kb, MB, or GB. When the log file reaches this hour, it will automatically scroll, moving the original content to the mylog. log.1 file.
Maxbackupindex = 2: specify the maximum number of rolling files that can be generated.

3. Configure the log information layout. The syntax is:
Log4j. appender. appendername. layout = fully. Qualified. Name. Of. layout. Class
Log4j. appender. appendername. layout. option1 = value1
...
Log4j. appender. appendername. layout. Option = valuen
Log4j provides the following layout types:
Org. Apache. log4j. htmllayout (in the form of HTML tables ),
Org. Apache. log4j. patternlayout (you can flexibly specify the layout mode ),
Org. Apache. log4j. simplelayout (including the log information level and information string ),
Org. Apache. log4j. ttcclayout (including the log generation time, thread, category, and so on)

4. output format settings
In the configuration file, you can use log4j. appender. a1.layout. conversionpattern to set the log output format.
Parameters:
% P: Priority of output log information, that is, debug, info, warn, error, fatal,
% D: date or time of the log output time point. The default format is iso8601. You can also specify the format after the time point, for example, % d {YYY Mmm dd hh: mm: SS, SSS}, output is similar to: October 18, 2002 22:10:28, 921
% R: The number of milliseconds it takes to output the log information from application startup to application startup.
% C: the category of the output log, usually the full name of the class.
% T: name of the thread that outputs the log event
% L: the location where the output log event occurs. It is equivalent to a combination of % C. % m (% F: % L), including the category name, the thread that occurs, and the number of lines in the code. Example: testlog4.main (testlog4.java: 10)
% X: NDC associated with the current thread (nested Diagnostic Environment), especially used in applications with multiple clients and multithreading such as Java Servlets.
%: Output A "%" Character
% F: name of the file in which the output Log message is generated
% L: the row number in the output code
% M: output the specific log information of the specified message in the code.
% N: returns a carriage return line break. For Windows, the line feed is "\ r \ n", and for UNIX, the line feed is "\ n ".
You can add modifiers between the % and mode characters to control the alignment of the minimum width, maximum width, and text. For example:
1) % 20c: specify the name of the output category. The minimum width is 20. If the category name is smaller than 20, it is right aligned by default.
2) %-20C: specify the name of the output category. The minimum width is 20. If the category name is smaller than 20, the "-" sign specifies the left alignment.
3) %. 30C: specify the name of the output category. The maximum width is 30. If the category name is greater than 30, the extra characters on the left will be truncated, but there will be no spaces if the value is less than 30.
4) % Category 30C: If the category name is less than 20, fill in spaces and align right. If the category name is longer than 30 characters, it will be truncated from the character sold on the left.

3. Use in programs
Before using log4j in a program, you first need to import the commons-logging.jar and logging-log4j-1.2.9.jar into classpath and put log4j. properties in the SRC root directory. Then you can use it.

1. Get the recorder
When using log4j, the first step is to obtain the logger, which controls the log information. Its syntax is:
Public static logger getlogger (string name ),
Obtain the recorder by the specified name. If necessary, create a new recorder for the name. The name is generally the name of this class, for example:
Static logger = logger. getlogger (serverwithlog4j. Class. getname ());
Note: We recommend that you use commons-logging in combination with log4j for logging.
Private Static log logger = logfactory. getlog (yourclass. Class );

2. Insert record information (format log information)
When the two necessary steps are completed, you can easily Insert the log record statements with different priorities to any location where you want to record the log. The syntax is as follows:
Logger. debug (Object message );
Logger.info (Object message );
Logger. Warn (Object message );
Logger. Error (Object message );

This article is from the "Meteor Shower it journey" blog, please be sure to keep this source http://lxy2020.blog.51cto.com/2528961/1569806

Log4j usage Summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.