Log4j Basic Use Method

Source: Internet
Author: User
      

A Brief introduction:

In order to better monitor the changes in the code and trace the trajectory of the code, while acting as a debugger in the development environment. The Apache Common Log Pack is an open source project for the Apache organization that provides developers with a wide range of common log nooplog interfaces. Users are free to select and implement a common log interface. The common log currently supports the following several logger:

1. LOG4J: Is the third party software, consists of logger, appender, layout three components.

2. Simplelog: The Org.apache.commons.logging.impl.SimpleLog Log implementation class is brought in the common log pack. It implements the log interface, and outputs the log messages to the system error stream system.err.

3. Nooplog: The Org.apache.commons.logging.impl.NoOpLog Log implementation class is brought in the common log pack. It implements the log interface, which does not perform any operation in its output log method.

Log4j is a member of the Log4 family, the Log4 family hanging: Look at the letters after 4 know what kind of environment to develop the API. The log4j we are going to introduce is of course the API developed for Java.

LOG4 Family
Log4c
Suitable for C
Log4cplus/log4cpp
For C + +
Log4j
for Java
Log4net
Suitable for dotnet
Log4perl
For Perl
log4php
for PHP
Log4plsql
Suitable for Pl/sql
Log4ls
Suitable for LotusScript
log4py/log4p
For Python
Qmmslog
Suitable for qt/c++ platform
log4r
For Ruby
JDK1.4 ' s util.logging framework
Suitable for Jdk1.4util

Two To use logs in a Web application system:

1. Download related jar files: Logging-log4j-1.2.13.zip extract Log4j-1.2.14.jar files

2. Deploy the jar file to the project. Copy Log4j-1.2.14.jar to the project folder \web-inf\lib folder and import the file in the project's build path: Right -click Project Name-Properties-Java Build path-add external jar (X)- Choose Log4j-1.2.8.jar,

3. Specify the Logger (log4j, Simplelog, Nooplog) to use in the properties file (Log4j.properties or common-logging.properties) where the common log package is established under the root directory of the project folder. Specify the log4j properties file log4j.properties the following:

Org.apache.commons.logging.log=org.apache.commons.logging.impl.log4jcategorylog

Specify the Simplelog logger's properties file Simplelog.properties as follows:

4. Set the log level, output format, location, and so on for the logger.

Org.apache.commons.logging.simplelog.defaultlog=info

Three Three log4j.properties content Settings instance:

#设置com. Unmi domain corresponds to the log level info,debug,warn,error and output a1,a2
#log4j. category.com.unmi=error,a1
#log4j. CATEGORY.COM.UNMI=INFO,A2 #可以设置级别: Debug>info>error #debug: Show debug, info, error #info: Show INFO, error #err Or: Only error


#category在log4j的早期版本中使用, 1.2 is recommended to replace the logger
#一般把上面两行写如下方式, unified control log output, and then set the package independent output level with Log4j.logger
Log4j.rootlogger=debug,a1
# You can specify the output level of a particular package, limited to this package: Log4j.logger. Package name = Level

# log4j.logger.com.unmi=error,a1--> child nodes Com.unmi can inherit the configuration properties of the parent node Rootlogger about A1
LOG4J.LOGGER.COM.UNMI=INFO,A2
#log4j .logger.com.unmi.special=error,a2--> can also inherit the parent node Com.unmi
#设置输出地A1, #设置A1的输出布局格式PatterLayout for the Consoleappender (console)
Log4j.appender.a1=org.apache.log4j.consoleappender
, ( Flexibility to specify layout patterns) log4j.appender.a1.layout=org.apache.log4j.patternlayout
#配置日志输出的格式
Log4j.appender.a1.layout.conversionpattern=%d{yyyy-mm-dd hh:mm:ss,sss} [%c]-[%p]%m%n
# Sets the output A2 to a file (a new file is generated when the file size reaches a specified size)
Log4j.appender.a2=org.apache.log4j.rollingfileappender
#文件位置
log4j.appender.a2.file=d:/javas/tomcat4/webapps/eclipse/test.html
#文件大小
log4j.appender.a2.maxfilesize= 500KB
Log4j.appender.a2.maxbackupindex=1
#指定采用html方式输出
log4j.appender.a2.layout= Org.apache.log4j.HTMLLayout


Procedures are as follows
Propertyconfigurator.configure ("d:/javas/tomcat4/webapps/eclipse/test.properties");//load. properties file
Logger Log=logger.getlogger ("Chan.test");----------->chan.test node has no special configuration and can only inherit Rootlogger output to console
Logger log = Logger.getlogger ("Com.unmi");
Logger log1 = Logger.getlogger ("com.unmi.special");

Log.info ("test");
Log1.error ("Test error");

The log file default output is the project root folder. We can define a folder to store the log files specifically.

1. NETP04 the Log4j.properties settings in the project:

Log4j.rootcategory=error, stdout, logfile

Log4j.logger.neo=debug

Log4j.appender.stdout=org.apache.log4j.consoleappender # Output to console settings

Log4j.appender.stdout.layout=org.apache.log4j.patternlayout # is stored in the specified layout mode

log4j.appender.stdout.layout.conversionpattern=%d%p [%c]-<%m>%n

# Specifies the format for output to the console, as follows:

#2006 -11-21 12:49:34,984 INFO [Neo.core.bean.BeanManager]-<beanmanager init finished.>

Log4j.appender.logfile=org.apache.log4j.dailyrollingfileappender # produces a file every day

Location of Log4j.appender.logfile.file=${netp.root}/web-inf/logs/netp.log # log files

Log4j.appender.logfile.datepattern=.yyyy-mm-dd

# The backup file of the day is named Netp.log, and the past record is in the file named Netp.log.yyyy-mm-dd.

# Log4j.appender.r.datepattern =. yyyy-mm produces a file every month

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

# Specifies the format of the output to the log file: Date priority [category]- Line_separator

log4j.appender.logfile.layout.conversionpattern=%d%p [%c]-<%m>%n

2. Another log4j.properties set of contents:

Log4j.rootcategory=info, stdout, logfile

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

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

log4j.appender.stdout.layout.conversionpattern=%d%p [%c]-<%m>%n

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

Log4j.appender.logfile.file=springapp.log

log4j.appender.logfile.maxfilesize=512kb

# Keep Three backup files

Log4j.appender.logfile.maxbackupindex=3

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

#Pattern to Output:date Priority [Category]- line_separator

log4j.appender.logfile.layout.conversionpattern=%d%p [%c]-<%m>%n

Four log4j configuration file:

Log4j is composed of three important components: priority of log information, output destination of log information, and output format of log information. the priority of log information from high to low has ERROR, WARN, INFO, DEBUG, respectively, to specify the importance of this log information; the output destination of the log information specifies whether the log will print to the console or file and the output format controls the display of the log information . LOG4J supports two configuration file formats, one in XML format and one for Java attribute files (key = value).

1. Configure the root logger, whose syntax is:

Log4j.rootlogger = [level], Appendername, Appendername, ...

Where level is the priority of logging, divided into off, FATAL, ERROR, WARN, INFO, DEBUG, all, or levels you define. LOG4J recommends using only four levels, from high to low, respectively, for error, WARN, INFO, and DEBUG. By the level defined here, you can control the switch to log information at the appropriate level in your application. For example, if the info level is defined here, all debug-level log information in the application will not be printed. Appendername is where you specify the output of the log information. You can specify multiple output destinations at the same time.

2. Configure log information output destination Appender, which has the following syntax:

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

Log4j.appender.appenderName.option1 = value1

...

Log4j.appender.appenderName.option = Valuen

Among them, LOG4J provides the following appender:

  Org.apache.log4j.ConsoleAppender (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 streaming format)

3. Configure the format (layout) of the log information, and its syntax is:

Log4j.appender.appenderName. Layout = Fully.qualified.name.of.layout.class

Log4j.appender.appenderName. Layout.option1 = value1

...

Log4j.appender.appenderName.layout.option = Valuen

Among them, LOG4J provides the following layout:

Org.apache.log4j.HTMLLayout (layout in HTML form),

  Org.apache.log4j.PatternLayout (You can specify layout patterns flexibly),

Org.apache.log4j.SimpleLayout (The level and information string that contains the log information),

Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, and so on that the log was generated)

LOG4J uses a print format similar to the printf function in c to format the log information, printing parameters as follows:%m the message specified in the output code

%p output priority, i.e. Debug,info,warn,error,fatal

%r output the number of milliseconds it takes to boot to output the log information

The class to which the%c output belongs, usually the full name of the class in which it is located

%t output The name of the thread that generated the log event

%n output a carriage return line feed, Windows platform "\ r \ n", UNIX platform "\ n"

%d output log point-in-time date or time, the default format is ISO8601, you can also specify the format after, such as:%d{yyy MMM dd hh:mm:ss,sss}, output similar: October 18, 2002 22:10:28,921

%l the location where the output log event occurs, including the class name, the thread that occurred, and the number of lines in the code. Example: Testlog4.main (testlog4.java:10)

Five Using log4j in Java class files:

1. Import Required Package file: Import org.apache.log4j.*;

2. Defining Logger objects

static Logger Logger = Logger.getlogger (Frame1.class.getName ()), defines the object as the program declares the variable, and is a static object. The Frame1 is the name of your current class.

3. To load a property file:

Propertyconfigurator.configure ("Log4j.properties");
Log4j creates example.log files in the same directory as log4j.properties and logs log information, and when the file size is greater than 100KB, Log4j creates a new Example.log file and backs up Example.log files. The number of files backed up depends on the Maxbackupindex property.
Six The application process of log4j on eclipse
Package com.log4j;
Import org.apache.log4j.*;
public class Mylogtest

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.