log4j How to use

Source: Internet
Author: User

When the project is in the development run phase, it is necessary to debug or troubleshoot the logs, and log management is needed to help us solve these problems:

In Java we can use SYSTEM.OUT.PRINTLN (), but this way function is too weak, and not easy to control, if temporarily do not want to output what to do? What if I want to output to a file? What if I want partial output? ......

Why use both commons-logging and log4j? Why not just use one of them?
The purpose of Commons-loggin is to provide a unified interface for "All Java log implementations", its own log function is usually weak (only a simple simplelog?), so it is generally not used alone.

The reason for using both commons-logging and log4j is to simplify usage and configuration.

In order to simplify the configuration commons-logging, the commons-logging configuration file is not used, and the commons-logging-related system environment variables are not set, but only the log4j jar package can be placed in Classpash. In this way, it is easy to complete the fusion of commons-logging and log4j. What if you don't want to use log4j? Simply remove the log4j jar package from the classpath.

How should the code be written?
We do the following three jobs in the "Everyone" class that needs to output log information:

1, import all the required commongs-logging class:

Import org.apache.commons.logging.*;

2. Define a private static class member of the Org.apache.commons.logging.Log class in its own class:

private static log log = Logfactory.getlog (Youclassname.class);

Note that the static member is defined here to avoid producing multiple instances.

The parameters of the Logfactory.getlog () method use the class of the current classes, which is by far the best way to be considered. Why not write Logfactory.getlog (This.getclass ())? Because the static class member does not access the this pointer!

3. Use the member method of the Org.apache.commons.logging.Log class to output log information:

Log.debug ("111"); Output log information at the debug level

Log.info ("222"); Output log information at the "Information" level

Log.warn ("333"); Output the log information at the warning level

Log.error ("444"); Output log information at the "error" level

Log.fatal ("555"); Output "Fatal error" level log information

The configuration file log4j.properties is required for log4j. If the configuration file is not in Classpath or is not configured, a run-time exception will be thrown.

The following is a little introduction to the contents of the Log4j.properties file, later attached to the Log4j.properties file as an example:

Log4j.rootlogger = DEBUG, console,a1

Log4j.rootlogger is one of the most important attributes, which defines the "output level" and "Output destination" for log information.

The key to see "=" after the value, "DEBUG, console,a1" Here we want to divide it into two parts: the first comma before the first part, specify "output level", followed by the second part, specify "Output destination". Multiple "output destinations" can be specified at the same time, separated by commas.

The "Output level" has an optional five values, namely Debug, INFO, WARN, ERROR, FATAL, which are defined by the log4j system.

L "Output Destination" is our own definition, in the later part of Log4j.properties, this file defines "output destination" has console, file, Rolling_file, SOCKET, Lf5_appender, MAIL, DATABASE, A1, im. This file can be used as the main template because it defines a variety of common output destinations (console, file, e-mail, database, etc.) in a more comprehensive manner.

Attached: A useful log4j.properties file template

Log4j.rootlogger=debug, CONSOLE,A1

Log4j.addivity.org.apache=true

# Apply to Console

Log4j.appender.console=org.apache.log4j.consoleappender

Log4j.appender.threshold=debug

Log4j.appender.console.target=system.out

Log4j.appender.console.encoding=gbk

Log4j.appender.console.layout=org.apache.log4j.patternlayout

Log4j.appender.console.layout.conversionpattern=[framework]%d-%c-%-4r [%t]%-5p%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%n

#应用于文件

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]%-5p%c%x-%m%n

# Use this layout for Logfactor 5 analysis

# Apply to File rollback

Log4j.appender.rolling_file=org.apache.log4j.rollingfileappender

Log4j.appender.ROLLING_FILE. Threshold=error

Log4j.appender.ROLLING_FILE. File=rolling.log

Log4j.appender.ROLLING_FILE. Append=true

Log4j.appender.ROLLING_FILE. maxfilesize=10kb

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]%-5p%c%x-%m%n

#应用于socket

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%n

# Log Factor 5 Appender

Log4j.appender.lf5_appender=org.apache.log4j.lf5.lf5appender

Log4j.appender.LF5_APPENDER. maxnumberofrecords=2000


# Send logs to mail


Log4j.appender.mail=org.apache.log4j.net.smtpappender

Log4j.appender.mail.threshold=fatal

log4j.appender.mail.buffersize=10

[Email protected]

Log4j.appender.mail.smtphost=www.wusetu.com

Log4j.appender.mail.subject=log4j Message

[Email protected]

Log4j.appender.mail.layout=org.apache.log4j.patternlayout

Log4j.appender.mail.layout.conversionpattern=[framework]%d-%c-%-4r [%t]%-5p%c%x-%m%n


# for databases

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]%-5p%c%x-%m%n ')

Log4j.appender.database.layout=org.apache.log4j.patternlayout

Log4j.appender.database.layout.conversionpattern=[framework]%d-%c-%-4r [%t]%-5p%c%x-%m%n


Log4j.appender.a1=org.apache.log4j.dailyrollingfileappender

log4j.appender.a1.file=samplemessages.log4j

Log4j.appender.a1.datepattern=yyyymmdd-hh '. log4j '

Log4j.appender.a1.layout=org.apache.log4j.xml.xmllayout


#自定义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 = [email protected]


Log4j.appender.im.layout=org.apache.log4j.patternlayout

Log4j.appender.im.layout.ConversionPattern =[framework]%d-%c-%-4r [%t]%-5p%c%x-%m%n

log4j How to use

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.