Log4j notes, log4j Configuration

Source: Internet
Author: User
Tags truncated

Log4j notes, log4j Configuration

Log4j notes

 

I,Overview:

1.Log4j Introduction: Apache open-source project, used to manage and print output log information.

Logger + Appender + Layout

1. Logger: Logger, a program for generating and recording logs

2. Appender: logstore location, console/file/GUI component, etc. A Logger can have multiple Appender.

3. Layout: controls the log format, output, and display level.

Note: five levels of log: Debug> Info> Warn> Error> Fatal.

Display Mode: information above the configuration level is displayed.

2.Why log??

Cause: 1. Errors in the development phase; 2. records the running status in the product phase.

3.Advantages of Log4j:

Set Switch: You can enable or disable log output during running;

Define Scope: Custom classification or package output or disabled;

Classification: Classify logs;

Diverse: Not only text information, but other information can be output;

Location: You can change the location of the output log.

4.Other log plug-ins: Comments-log, JDK-log, etc.

 

II,How to Use:

1. Import the jar package and write the configuration file (Log4j supports configuration files in two formats, namely, properties files and xml files). (properties file configuration methods are common)

2. Basic Format of properties configuration file:

The log4j. properties configuration file is as follows:


# Log4j. rootLogger = debug, stdout, R
Log4j. rootLogger = debug, stdout
Log4j.logger.com. test. log4j = debug, R
Log4j.logger.com. test. log4j. hehe = debug, R2
Log4j.logger.com. test. hehe. Test = info, R3

Log4j. appender. stdout = org. apache. log4j. leleappender
Log4j. appender. stdout. layout = org. apache. log4j. PatternLayout
# Pattern to output the caller's file name and line number.
Log4j. appender. stdout. layout. conversionPattern = [% t] % d {yy/mm/dd HH: mm: ss} % p (% F: % L)-% m % n

Log4j. appender. R = org. apache. log4j. RollingFileAppender
# Log4j. appender. R. File = example. log
Log4j. appender. R. File = c:/example. log
# Log4j. appender. R. File =$ {webappHome}/example. log
Log4j. appender. R. MaxFileSize = 1KB

Log4j. appender. R = org. apache. log4j. RollingFileAppender
Log4j. appender. R. File =$ {catalina. home}/logs/rfidtest. log
Log4j. appender. R. MaxFileSize = 10 MB
Log4j. appender. R. MaxBackupIndex = 1
Log4j. appender. R. layout = org. apache. log4j. PatternLayout
Log4j. appender. r. layout. conversionPattern = % d {yyyy-MM-dd HH: mm: ss, SSS} % 5 p % t % c {1}: % L-% m % n


* Where rootLogger is the root logger and log4j.logger.com. Alvin. dao. AddUserDao is the logger of this class. If this parameter is set, the root logger will be overwritten.

 


3. Reference Information:

Parameters

Description

Example

% C

Lists the full name of a logger namespace. If {<layers>} is added, the namespace of the specified number of layers counted from the innermost layer is listed.

Example of parameters in the Log4j configuration file

Output Display Media

Assume that the current logger namespace is "a. B. c"

% C

A. B. c

% C {2}

B .C

% 20c

(If the namespace length is less than 20, fill the space on the left)

%-20c

(If the namespace length is less than 20, the right side is filled with spaces)

%. 30c

(If the namespace length exceeds 30, extra characters are truncated)

% Limit 30C

(If the namespace length is less than 20, spaces are filled on the left. If the namespace length exceeds 30, excessive characters are truncated)

%-Limit 30C

(If the namespace length is less than 20, the right side is filled with spaces. If the namespace length exceeds 30, excessive characters are truncated)

% C

List the full names (including path names) of the classes that call logger)

Suppose it is org. apache. xyz. SomeClass"

% C

Org. apache. xyz. SomeClass

% C {1}

SomeClass

% D

Display the log record time. {<date format>} uses the format defined in ISO8601.

% D {yyyy/MM/dd HH: mm: ss, SSS}

22:23:30, 117

% D {ABSOLUTE}

22:23:30, 117

% D {DATE}

12 Oct 2005 22:23:30, 117

% D {ISO8601}

22:23:30, 117

% F

Display the name of the source file that calls logger

% F

MyClass. java

% I

Location where the output log event occurs, including the category name, the thread that occurs, and the number of lines in the code

% I

MyClass. main (MyClass. java: 129)

% L

Display the code lines that call logger

% L

129

% M

Display the Output Message

% M

This is a message for debug.

% M

Display the method name for calling logger

% M

Main

% N

Line breaks on the current platform

% N

M on Windows

N on UNIX platforms

% P

Displays the priority of the log.

% P

INFO

% R

Displays the number of milliseconds that have elapsed since the program was started to record the log.

% R

1215

% T

Name of the thread that generates the log event

% T

MyClass

% X

Logs output in the NDC (Nested Diagnostic Context, thread stack) sequence

Assume that the calling sequence of a program is MyApp calling com. foo. Bar.

% C % x-% m % n

MyApp-Call com. foo. Bar.

Com. foo. Bar-Log in Bar

MyApp-Return to MyApp.

% X

Output logs according to MDC (Mapped Diagnostic Context, thread ing table. It is usually used for connecting multiple clients to the same server, so that the server can distinguish the logs left by the client.

% X {5}

(Logs of clients codenamed 5)

%

Display a percent sign

%

%

 

4. if logs are output as files, configure the relative path in the properties configuration file to avoid problems after the project is released. The configuration method is to create a servlet and. in the xml file, set it to start loading. In the servlet, read the path of the current project in the Init () method and put it into the configuration. Then, the configuration file reads the scope $ {webappHome }.

 

5. How to use it in the program:

Declaration: private static Logger logger = Logger. getLogger (class name. class );

Use: logger. debug ("..."); Logger.info ("..."); Logger. warn ("..."); Log can be printed.

6. If you need to change the output, you only need to change the level in properties.

 

III,Notes:

1. The properties configuration file must be placed in the src root directory and not in the package.

2. The name of the properties configuration file cannot be changed either. It must be log4j. properties.

3. If logs are output as files, set relative paths rather than absolute paths in the configuration file to prevent directory changes after the project goes online. The setting method is as follows:

<1>. create a servlet, implement its Init () method, and then obtain the current project path in the Init () method and save it to the configuration, that is, System. setProperty ("webappHome", this. getServletContext (). getRealPath ("");

<2>. configure web. in the xml configuration file, set <loan-on-start> to 1 or a positive number greater than 1 so that the server can instantiate this type at startup. Note: you do not need to configure the <servlet-mapping> label;

<3> In the log4j. properties configuration file, change the path to $ {webappHome }.

4. In the Log4j. properties configuration file, two steps are required to output logs to the file:

<1>. log4j. appender. stdout. layout = org. apache. log4j. PatternLayout

<2>. log4j. appender. stdout. layout. ConversionPattern = "output format"

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.