Applying log4j in Spring

Source: Internet
Author: User
Tags html form log4j
Log4j is an open source project for Apache, by using log4j, we can control the destination of log information delivery is console, file, GUI component, even interface server, NT Event recorder, UNIX syslog daemon, etc. We can also control the output format of each log, and by defining the level of each log information, we can control the log generation process more carefully.      Most interesting of all, these can be configured flexibly with a single configuration file without the need to modify the applied code.     Such a powerful advantage, in fact, is not difficult to hand, especially in the spring framework, the use of log4j is easier, the following introduction of Spring's log4j application. Of course, download the corresponding jar package (Log4j.jar) First is the Web.xml configuration, add the following configuration in Web.xml <context-param>
<param-name> log4jconfiglocation</param-name>
<param-value>/WEB-INF/props/log4j.properties</param-value>
</context-param>
<context-param>
<param-name> log4jrefreshinterval</param-name>
<param-value>6000</param-value>
</context-param>
<listener>
<listener-class>
Org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener> Note: In the above configuration, in the above configuration, Log4jconfiglistener will go to web-inf/props/log4j.propeties read the configuration file; Open a watchdog thread scans the configuration file every 60 seconds (so that the configuration file is not restarted after the Web service starts); The path to the Web directory is pressed into a system variable called Webapp.root (Webapp.root will be used in the Log4j.properties file).

Next is the log4j.properties configuration file, put it under Web-inf/props, and configure as follows:

#log4j. Rootlogger = [level], Appendername, Appendername, ...
Log4j.rootlogger = INFO, console, R
#level =info,all can be output
#console is set to being a Consoleappender
Log4j.appender.console = Org.apache.log4j.ConsoleAppender
#console have four patterns
#org. apache.log4j.HTMLLayout
#org. apache.log4j.PatternLayout
#org. apache.log4j.SimpleLayout
#org. apache.log4j.TTCCLayout
Log4j.appender.console.layout = Org.apache.log4j.PatternLayout
#define The output type
Log4j.appender.console.layout.ConversionPattern =%-d{yyyy-mm-dd HH:mm:ss} [%c]-[%p]%m%n
#file is set to output to a extra file
LOG4J.APPENDER.R = Org.apache.log4j.RollingFileAppender
#the Absolute route of the log4j file
Log4j.appender.r.file = ${webapp.root}/log.txt
#the size
Log4j.appender.r.maxfilesize = 500KB
#back up a file
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} [%c]-[%p]-%m%n

The configuration file above indicates that log information will be output in two ways (files and consoles), ${webapp.root} to represent the root directory of the application (for example, the application name is ABC, the Log.txt position is TOMACT/WEBAPP/ABC)

Finally, add log4j support in the program where you want to output log

(1) Introduction of import Org.apache.log4j.Logger

(2) Declaring a logger

private static Logger Logger = Logger.getlogger (Classname.class);

(3) Add the output information to the corresponding position in the program

Logger.info ("User login:" +user.getaccount ());

OK, done, when there is login will be in the console and file in the same time output log information as follows

2007-01-10 16:02:54 [com.my.web.useraction]-[info] User login: YANGSQ

Note (turn):
Here are some important syntax for the configuration file (log4j.properties)
To define a configuration file

You can actually configure the LOG4J environment in your code without using a configuration file at all. However, using a configuration file will make your application more flexible.

LOG4J supports two configuration file formats, one in XML format and one for Java attribute files (key = value). Here's how to use the Java attribute file as a configuration file:

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.

Configure log information output destination Appender, whose syntax is

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 a specified size),
Org.apache.log4j.WriterAppender (send log information to any specified place in streaming format)

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)

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.