Log4J log management

Source: Internet
Author: User
Tags html form
Details on the use of Log4J log management, which is essential for java Development

I. Preface:
Log4j is an open source project and is a widely used logging package written in Java. Due to the outstanding performance of log4j, at the time when log4j was completed, the log4j development organization suggested that sun replace jdk1.4's log tool class with log4j in jdk1.4, but jdk1.4 was nearing completion, therefore, sun refused to use log4j. The most actually used log4j in java Development was log4j, and sun's log tool class was forgotten. One of its unique features includes the concept inherited in categories. By using the category hierarchy, this reduces the log record output and minimizes the overhead of log records.

It allows developers to control which log statements are output at any interval. By using an external configuration file, you can configure it at runtime. Almost every large application includes its own logging or tracking API. Experience shows that logging is an important part of the development cycle. Similarly, logging provides some advantages. First, it provides the exact context for running the application. Once inserted into the code, no human interference is required to generate the log output. Second, log output can be saved to permanent media for future research. Finally, in addition to being used in the development phase, a wide range of log records can also be used as an audit tool.
According to this rule, in early 1996, the eu semper (European security electronic market) project decided to write its own tracking API. This API has evolved into log4j, a popular Java logging package, after numerous improvements, several implementations, and a lot of work. This package is distributed under the IBM Public License and is certified by an open source authority.

Log records have their own shortcomings. It will speed down applications. If it is too detailed, it may make the screen scroll invisible. To reduce these impacts, log4j is designed to be fast and flexible. Log4j APIs strive to be easy to understand and use because the application uses logging as the main function.
Log4j, which can control which log statements are output at any interval.

Ii. Main Components

1. Root category (at the top of the category hierarchy, that is, the global Log Level)

Configure the root Logger with the Syntax:

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

Level is the log record type
AppenderName specifies where the log information is output. You can specify multiple output destinations at the same time.

The category level is OFF, FATAL, ERROR, WARN, INFO, DEBUG, log, ALL, or custom priority.
Common priorities of og4j: FATAL> ERROR> WARN> INFO> DEBUG
Configure the root Logger with the Syntax:
Log4j. rootLogger = [level], appenderName, appenderName ,...
If it is log4j. rootLogger = WARN, it means that only WARN, ERROR, and FATAL are output, and DEBUG and INFO are blocked.

Example: log4j. rootCategory = INFO, stdout, Runlog, Errorlog
The root log category is INFO, DEBUG is blocked, and others are output. Stdout, Runlog, and Errorlog are three output destinations respectively.

2. Common output formats

-X: left aligned when X information is output;
% P: log information level
% D {}: log generation time
% C: Location of log information (class name)
% M: log details
% N: line feed of output log information
Example:
Log4j. appender. stdout. layout. ConversionPattern = % 5 p % d {yyyy-MM-dd HH: mm: ss} % c % m % n
Log4j. appender. Runlog. layout. ConversionPattern = % 5 p % d {yyyy-MM-dd HH: mm: ss} % c % m % n
Log4j. appender. Errorlog. layout. ConversionPattern = % 5 p % d {yyyy-MM-dd HH: mm: ss} % c % m % n

3. Layout
The output layout used. log4j provides four la s:
Org. apache. log4j. HTMLLayout (layout in HTML form)
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)

Example:
The output format is an HTML table.
Log4j. appender. stdout. layout = org. apache. log4j. HTMLLayout

The output format is the layout mode that can be flexibly specified.
Log4j. appender. stdout. layout = org. apache. log4j. PatternLayout

The output format is the log information level and information string.
Log4j. appender. stdout. layout = org. apache. log4j. SimpleLayout

The output format contains the log generation time, thread, category, and other information.
Log4j. appender. stdout. layout = org. apache. log4j. TTCCLayout

4. Destination

Configure the Appender of the log output destination. Its syntax is

Log4j. appender. appenderName = fully. qualified. name. of. appender. class
Log4j. appender. appenderName. option1 = value1
...
Log4j. appender. appenderName. option = valueN
AppenderName specifies where the log information is output. You can specify multiple output destinations at the same time.

Output destinations supported by log4j:
Org. apache. log4j. leleappender Console
Org. apache. log4j. FileAppender File
Org. apache. log4j. DailyRollingFileAppender generates a log file 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)
Org.apache.log4j.net. SMTPAppender email
Org. apache. log4j. jdbc. JDBCAppender Database
Others include GUI components, even interface servers, NT Event recorders, and UNIX Syslog daemon.

Example:

Output to console
Log4j. appender. CONSOLE = org. apache. log4j. leleappender (specify the output to the CONSOLE)
Log4j. appender. Threshold = DEBUG (specify the output category)
Log4j. appender. CONSOLE. Target = System. out
Log4j. appender. CONSOLE. layout = org. apache. log4j. PatternLayout (specify the output layout)
Log4j. appender. CONSOLE. layout. conversionPattern = [framework] % d-% c-%-4r [% t] %-5 p % c % x-% m % n (specified output format)

Output to file

Log4j. appender. FILE = org. apache. log4j. FileAppender (specify output to FILE)
Log4j. appender. FILE. File = file. log (specify the output path and FILE name)
Log4j. appender. FILE. Append = false
Log4j. appender. FILE. layout = org. apache. log4j. PatternLayout (specify the output layout)
Log4j. appender. FILE. layout. conversionPattern = [framework] % d-% c-%-4r [% t] %-5 p % c % x-% m % n (specified output format)

Output to a file (rotate the "Log File". When the log file reaches the specified size, the file will be closed and backed up, and a new log file will be created)

Log4j. appender. ROLLING_FILE = org. apache. log4j. RollingFileAppender (specify output to file)
Log4j. appender. ROLLING_FILE.Threshold = ERROR (specify the output category)
Log4j. appender. ROLLING_FILE.File = rolling. log (specify the output path and file name)
Log4j. appender. ROLLING_FILE.Append = true
Log4j. appender. ROLLING_FILE.MaxFileSize = 10KB (specify the size of the output file)
Log4j. appender. ROLLING_FILE.MaxBackupIndex = 1
Log4j. appender. ROLLING_FILE.layout = org. apache. log4j. PatternLayout (specify the output layout)
Log4j. appender. ROLLING_FILE.layout.ConversionPattern = [framework] % d-% c-%-4r [% t] %-5 p % c % x-% m % n (specified in output format)

Output to Socket
Log4j. appender. SOCKET = org. apache. log4j. RollingFileAppender (specify output to Socket)
Log4j. appender. SOCKET. RemoteHost = localhost (remote host)
Log4j. appender. SOCKET. Port = 5001 (remote host Port)
Log4j. appender. SOCKET. LocationInfo = true
Log4j. appender. SOCKET. layout = org. apache. log4j. PatternLayout (layout)
Log4j. appender. SOCET. layout. conversionPattern = [start] % d {DATE} [DATE] % n % p [PRIORITY] % n % x [NDC] % n % t [THREAD] % n % c [CATEGORY] % n % m [MESSAGE] % n (output format)

Output to email
Log4j. appender. MAIL = org.apache.log4j.net. SMTPAppender (specify the output to the email)
Log4j. appender. MAIL. Threshold = FATAL
Log4j. appender. MAIL. BufferSize = 10
Log4j. appender. MAIL. From = chenyl@hollycrm.com (sender)
Log4j. appender. MAIL. SMTPHost = mail.hollycrm.com (SMTP server)
Log4j. appender. MAIL. Subject = Log4J Message
Log4j. appender. MAIL. To = chenyl@hollycrm.com (recipient)
Log4j. appender. MAIL. layout = org. apache. log4j. PatternLayout (layout)
Log4j. appender. MAIL. layout. conversionPattern = [framework] % d-% c-%-4r [% t] %-5 p % c % x-% m % n (Format)

Output to database
Log4j. appender. DATABASE = org. apache. log4j. jdbc. JDBCAppender (specify the output to the DATABASE)
Log4j. appender. DATABASE. URL = jdbc: mysql: // localhost: 3306/test (specify the database url)
Log4j. appender. DATABASE. driver = com. mysql. jdbc. Driver (specify the DATABASE driver)
Log4j. appender. DATABASE. user = root (specified DATABASE user)
Log4j. appender. DATABASE. password = root (specify the DATABASE User password)
Log4j. appender. DATABASE. SQL = INSERT INTO LOG4J (Message) VALUES ('[framework] % d-% c-%-4r [% t] %-5 p % c % x-% m % n') (organizes SQL statements)
Log4j. appender. DATABASE. layout = org. apache. log4j. PatternLayout (layout)
Log4j. appender. DATABASE. layout. conversionPattern = [framework] % d-% c-%-4r [% t] %-5 p % c % x-% m % n (Format)

5. Log category supplement
Sometimes we need to specify a log category different from the root category for a specific part. We can specify the priority of a package.
For example:
Log4j.category.com. neusoft. mbip. dm. util = ERROR, where com. neusoft. mbip. dm. util is the part where we need to specify the log category.

Or you can specify the priority of the output file.
Log4j. appender. Errorlog. Threshold = ERROR

Iii. Common log4j configurations

Log4j configuration is commonly used. Two methods are generally used:. properties AND. xml. The following two simple examples are provided:

1. log4j. properties

### Set the levels of INFO, DEBUG, WARN, ERROR, and output location A1, A2 for the org. zblog domain ##
Log4j.category.org. zblog = ERROR, A1
Log4j.category.org. zblog = INFO, A2

Log4j. appender. A1 = org. apache. log4j. leleappender
### Set output location A1 to leleappender (console )##
Log4j. appender. A1.layout = org. apache. log4j. PatternLayout
### Set the output layout format PatterLayout of A1. (you can flexibly specify the layout mode )##
Log4j. appender. A1.layout. ConversionPattern = % d {yyyy-MM-dd HH: mm: ss, SSS} [% c]-[% p] % m % n
### Configure the log output format ##

Log4j. appender. A2 = org. apache. log4j. RollingFileAppender
### Set the output location A2 to the file (a new file is generated when the file size reaches the specified size )##
Log4j. appender. A2.File = E:/study/log4j/zhuwei.html
### File location ##
Log4j. appender. A2.MaxFileSize = 500KB
### File size ##
Log4j. appender. A2.MaxBackupIndex = 1
Log4j. appender. A2.layout = org. apache. log4j. HTMLLayout
# Specify html output

2. log4j. xml

<? Xml version = "1.0" encoding = "GB2312"?>
<! DOCTYPE log4j: configuration SYSTEM "log4j. dtd">

<Log4j: configuration xmlns: log4j = "http://jakarta.apache.org/log4j/">

<Appender name = "org. zblog. all" class = "org. apache. log4j. RollingFileAppender">
<! -- Set the channel ID: org. zblog. all and the output method: org. apache. log4j. RollingFileAppender -->
<Param name = "File" value = "E:/study/log4j/all. output. log"/> <! -- Set the File parameter: log output File name -->
<Param name = "Append" value = "false"/> <! -- Set whether to add a new log to the base of the original log when the service is restarted -->
<Layout class = "org. apache. log4j. PatternLayout">
<Param name = "ConversionPattern" value = "% p (% c: % L)-% m % n"/> <! -- Set the output file project and format -->
</Layout>
</Appender>

<Appender name = "org. zblog. zcw" class = "org. apache. log4j. RollingFileAppender">
<Param name = "File" value = "E:/study/log4j/zhuwei. output. log"/>
<Param name = "Append" value = "true"/>
<Param name = "MaxFileSize" value = "10240"/> <! -- Set the file size -->
<Layout class = "org. apache. log4j. PatternLayout">
<Param name = "ConversionPattern" value = "% p (% c: % L)-% m % n"/>
</Layout>
</Appender>

<Logger name = "zcw. log"> <! -- Set the domain name limit, that is, the zcw. log domain and the following logs are output to the corresponding channel below -->
<Level value = "debug"/> <! -- Set level -->
<Appender-ref = "org. zblog. zcw"/> <! -- Corresponds to the preceding channel id -->
</Logger>

<Root> <! -- Set the channel for receiving all output -->
<Appender-ref = "org. zblog. all"/> <! -- Corresponds to the preceding channel id -->
</Root>

</Log4j: configuration>

3. configuration file loading method:

Import org. apache. log4j. Logger;
Import org. apache. log4j. PropertyConfigurator;
Import org. apache. log4j. xml. DOMConfigurator;

Public class Log4jApp {
Public static void main (String [] args ){
DOMConfigurator. configure ("E:/study/log4j/log4j. xml"); // load the. xml file
// PropertyConfigurator. configure ("E:/study/log4j/log4j. properties"); // load the. properties File

Logger log = Logger. getLogger ("org. zblog. test ");
Log.info ("test ");
}
}

4. The project uses log4j
In a web application, you can load the configuration file in a separate servlet, and configure the servlet in web. xml to load when the application starts.
In a multi-person project, you can set an output channel for each person, so that when each person builds a Logger, they can use their own domain name to let the debugging letter
Output to your own log file.

4. log4j configuration example (properties)

# Log4j. rootLogger = [level], appenderName, appenderName,
# The category level is OFF, FATAL, ERROR, WARN, INFO, DEBUG, log, ALL, or custom priority.
# Common Log4j priority FATAL> ERROR> WARN> INFO> DEBUG

# Stdout is the console, Errorlog is the error log,
Log4j. rootCategory = INFO, stdout, Runlog, Errorlog

# The output appender format is
# Log4j. appender. appenderName = fully. qualified. name. of. appender. class
# Log4j. appender. appenderName. option1 = value1
# Log4j. appender. appenderName. option = valueN
# Output supported by appender in Log4j
# Org. apache. log4j. leleappender Console
# Org. apache. log4j. FileAppender File
# Org. apache. log4j. DailyRollingFileAppender generates a log file 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)
# Org.apache.log4j.net. SMTPAppender email
# Org. apache. log4j. jdbc. JDBCAppender Database

# Define the output format
Log4j. appender. stdout = org. apache. log4j. leleappender
Log4j. appender. Runlog = org. apache. log4j. DailyRollingFileAppender
Log4j. appender. Errorlog = org. apache. log4j. DailyRollingFileAppender

# Specify the priority of the output file
Log4j. appender. Errorlog. Threshold = ERROR

# Specify the output file
Log4j. appender. Runlog. File = D: UserInfoSynWebRootWEB-INFrunlogrunlog.log
Log4j. appender. Errorlog. File = D: UserInfoSynWebRootWEB-INFerrorlogerrorlog.log

# Layout of Log4j
# Org. apache. log4j. HTMLLayout layout in the form of HTML tables
# Org. apache. log4j. PatternLayout can flexibly specify the layout mode
# Org. apache. log4j. SimpleLayout contains the log information level and information string
# Org. apache. log4j. TTCCLayout contains the log generation time, thread, category, and other information.

Log4j. appender. stdout. layout = org. apache. log4j. PatternLayout
Log4j. appender. Runlog. layout = org. apache. log4j. PatternLayout
Log4j. appender. Errorlog. layout = org. apache. log4j. PatternLayout

# Output format: log4j javadoc org. apache. log4j. PatternLayout
#-X: left aligned when X information is output;
# % P: log information level
# % D {}: log generation time
# % C: log information location (class name)
# % M: log details
# % N: line feed of output log information
Log4j. appender. stdout. layout. ConversionPattern = % 5 p % d {yyyy-MM-dd HH: mm: ss} % c % m % n
Log4j. appender. Runlog. layout. ConversionPattern = % 5 p % d {yyyy-MM-dd HH: mm: ss} % c % m % n
Log4j. appender. Errorlog. layout. ConversionPattern = % 5 p % d {yyyy-MM-dd HH: mm: ss} % c % m % n

# Specify the priority of a package
Log4j.category.com. neusoft. mbip. dm. util = ERROR

# Example
###################
# Console Appender
###################
# Log4j. appender. CONSOLE = org. apache. log4j. leleappender
# Log4j. appender. Threshold = DEBUG
# Log4j. appender. CONSOLE. Target = System. out
# Log4j. appender. CONSOLE. layout = org. apache. log4j. PatternLayout
# Log4j. appender. CONSOLE. layout. ConversionPattern = [framework] % d-% c-%-4r [% t] %-5 p % c % x-% m % n
# Log4j. appender. CONSOLE. layout. conversionPattern = [start] % d {DATE} [DATE] % n % p [PRIORITY] % n % x [NDC] % n % t [THREAD] n % c [CATEGORY] % n % m [MESSAGE] % n

#####################
# File Appender
#####################
# Log4j. appender. FILE = org. apache. log4j. FileAppender
# Log4j. appender. FILE. File = file. log
# Log4j. appender. FILE. Append = false
# Log4j. appender. FILE. layout = org. apache. log4j. PatternLayout
# Log4j. appender. FILE. layout. ConversionPattern = [framework] % d-% c-%-4r [% t] %-5 p % c % x-% m % n
# Use this layout for LogFactor 5 analysis

########################
# Rolling File ????? RollingFileAppender ??????????????????
########################
# Log4j. appender. ROLLING_FILE = org. apache. log4j. RollingFileAppender
# Log4j. appender. ROLLING_FILE.Threshold = ERROR
# File Location
# Log4j. appender. ROLLING_FILE.File = rolling. log
# Log4j. appender. ROLLING_FILE.Append = true
# File Size
# Log4j. appender. ROLLING_FILE.MaxFileSize = 10KB
# Specify the output layout and format
# Log4j. appender. ROLLING_FILE.MaxBackupIndex = 1
# Log4j. appender. ROLLING_FILE.layout = org. apache. log4j. PatternLayout
# Log4j. appender. ROLLING_FILE.layout.ConversionPattern = [framework] % d-% c-%-4r [% t] %-5 p % c % x-% m % n

####################
# Socket Appender
####################
# Log4j. appender. SOCKET = org. apache. log4j. RollingFileAppender
# Log4j. appender. SOCKET. RemoteHost = localhost
# Log4j. appender. SOCKET. Port = 5001
# Log4j. appender. SOCKET. LocationInfo = true
# Set up for Log Facter 5
# Log4j. appender. SOCKET. layout = org. apache. log4j. PatternLayout
# Log4j. appender. SOCET. layout. conversionPattern = [start] % d {DATE} [DATE] % n % p [PRIORITY] % n % x [NDC] % n % t [THREAD] % n % c [CATEGORY] % n % m [MESSAGE] % n

########################
# SMTP Appender
#######################
# Log4j. appender. MAIL = org.apache.log4j.net. SMTPAppender
# Log4j. appender. MAIL. Threshold = FATAL
# Log4j. appender. MAIL. BufferSize = 10
# Log4j. appender. MAIL. From = chenyl@hollycrm.com
# Log4j. appender. MAIL. SMTPHost = mail.hollycrm.com
# Log4j. appender. MAIL. Subject = Log4J Message
# Log4j. appender. MAIL. To = chenyl@hollycrm.com
# Log4j. appender. MAIL. layout = org. apache. log4j. PatternLayout
# Log4j. appender. MAIL. layout. ConversionPattern = [framework] % d-% c-%-4r [% t] %-5 p % c % x-% m % n

########################
# JDBC Appender
#######################
# Log4j. appender. DATABASE = org. apache. log4j. jdbc. JDBCAppender
# Log4j. appender. DATABASE. URL = jdbc: mysql: // localhost: 3306/test
# Log4j. appender. DATABASE. driver = com. mysql. jdbc. Driver
# Log4j. appender. DATABASE. user = root
# Log4j. appender. DATABASE. password =
# Log4j. appender. DATABASE. SQL = INSERT INTO LOG4J (Message) VALUES ('[framework] % d-% c-%-4r [% t] %-5 p % c % x-% m % n ')
# Log4j. appender. DATABASE. layout = org. apache. log4j. PatternLayout
# Log4j. appender. DATABASE. layout. ConversionPattern = [framework] % d-% c-%-4r [% t] %-5 p % c % x-% m % n

########################
# Log Factor 5 Appender
########################
# Log4j. appender. LF5_APPENDER = org. apache. log4j. lf5.LF5Appender
# Log4j. appender. LF5_APPENDER.MaxNumberOfRecords = 2000

###################
# Custom Appender
###################
# Log4j. appender. im = net. cybercorlin. util. logger. appender. IMAppender
# Log4j. appender. im. host = mail.cybercorlin.net
# Log4j. appender. im. username = username
# Log4j. appender. im. password = password
# Log4j. appender. im. recipient = corlin@cybercorlin.net
# Log4j. appender. im. layout = org. apache. log4j. PatternLayout
# Log4j. appender. im. layout. ConversionPattern = [framework] % d-% c-%-4r [% t] %-5 p % c % x-% m % n

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.