1. log4j (log4j.properties) configuration file
Define the log configuration file and place it in the Web-info directory.
LOG4J.LOGGER.OPERATELOG=INFO,A1
# # #输出到日志文件 ###
log4j.appender.a1= Org.apache.log4j.DailyRollingFileAppender
log4j.appender.a1.file=e:\\file.log
log4j.appender.A1.layout =org.apache.log4j.patternlayout
log4j.appender.a1.layout.conversionpattern=%-d{yyyy-mm-dd HH\:mm\:ss} [%t\:% R]-[%P]%m%n
Where the Operatelog is the name of the custom log, used when invoked, which can define multiple different log output configurations, which are invoked when invoked.
Info is the log output level, mainly: ERROR, WARN, INFO, DEBUG
Error is a critical error, mostly a program bug.
WARN as a general warning, such as session loss
Info for general information to be displayed, such as login logout
Debug is debugging information for the program
A1 is the destination of the output, can be multiple, directly following, separated by commas. Represents the output of log level info to A1 this destination. The next four lines of code are defined as A1.
About the Appender output destination, output format, such as the detailed definition, you can Google, here is not described in detail.
Log4j three important parts: the priority of the log information, the output destination of the log information, the output format of the log information.
2. Web.xml Configuration
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value> web-inf/log4j.properties</param-value>
</context-param>
<context-param>
< param-name>log4jrefreshinterval</param-name>
<param-value>60000</param-value>
</context-param>
<!--need to add Spring-web.jar package, otherwise error message-->
<listener>
< Listener-class>org.springframework.web.util.log4jconfiglistener</listener-class>
The first parameter defines the location of the log4j configuration file, the second parameter defines the refresh cycle, and the log4j configuration file is modified without redeployment. Behind the listener, the Spring-web.jar package must be included in the Project project.
3. Call
In the class that requires log output, define the log object
Private final Logger Logger = Logger.getlogger ("Operatelog");
Where Operatelog is a custom log name that can be defined and used as needed.
Used where output logs are required
logger.info (Session.getattribute ("user") + "login System");