作者fbysss
关键字:logging
commons-logging是一个通用的日志接口,commons-logging.jar包中自带了一个simplelog的实现
log4j也实现了这个接口
使用通用接口,方便在于如果更换实现的方式,只要修改一个配置项即可
配置过程:
commons-logging.properties必须放置在WEB-INF/classes/下面
log4j.properties可以放这,也可以放置在WEB-INF下,但是需要在web.xml中进行配置,这样tomcat才能找到它
<web-app>
<welcome-file-list>
<welcome-file>hello.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>log4j-init</servlet-name>
<servlet-class>com.hedong.learning.log4j.Log4jInit</servlet-class>
<init-param>
<param-name>log4j</param-name>
<param-value>WEB-INF/log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
注意这里的servlet需要自己编写。
/*
* Created on 2005-3-2
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.beltino.util;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.PropertyConfigurator;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Log4jInit extends HttpServlet { /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
String prefix = getServletContext().getRealPath("/");
String file = getInitParameter("log4j");
if (file != null) {
PropertyConfigurator.configure(prefix + file);
}
}}
commons-logging.properties:
#--------------------------------------------------------------------------------
#根据需要配置不同的logging实现
#org.apache.commons.logging.Log = org.apache.commons.logging.impl.SimpleLog
org.apache.commons.logging.Log = org.apache.commons.logging.impl.Log4JCategoryLog
#
#----------------------------------------------------------------------------------
log4j.properties:
#这里面的console和file都是自定义的,可以自行修改
#注意的是log文件的路径,如果不指定,默认是在$TOMCAT_HOME/bin目录下。
#log4j.appender.file.DatePattern='.'yyyy-MM-dd'.log'这句用来设置每天生成上一天的日志备份文件的格式。当前最新的日志就是testlog.log要看效果,可以把电脑的日期修改为第二天暂时看看
#--------------------------------------------------------------------------------
#LOGGERS#
#defile a logger named ...
log4j.rootLogger=INFO,console,file
#define an appender named console,which is set to be a ConsoleAppender
log4j.appender.console=org.apache.log4j.ConsoleAppender#APPENDERS##
#define an appender named file,which is set to be a DailyRollingFileAppender
log4j.appender.file = org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.File = ../webapps/test/logs/testlog.log
log4j.appender.file.DatePattern='.'yyyy-MM-dd'.log'
#LAYOUTS#
#assign a SimpleLayout to console appender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%p][%c] - %m%n
#=org.apache.log4j.SimpleLayout
#assign a SimpleLayout to file appender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%c] - %m%n
#--------------------------------------------------------------------------------
这是另一个参考配置,可以记录jdbc的 SQL语句
http://www.delphibbs.com/keylife/iblog_show.asp?xid=12706
#定义log
log4j.rootLogger=info,console,file,DATABASE
#log4j.rootLogger=info,file,DATABASE
#log4j.rootLogger=error,file,DATABASE
#定义控制台
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%r [%t] %-5p %c (%F:%L) %x - %m%n
#定义数据库
log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.DATABASE.URL=jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=communitymedical
log4j.appender.DATABASE.driver=com.microsoft.jdbc.sqlserver.SQLServerDriver
log4j.appender.DATABASE.user=sa
log4j.appender.DATABASE.password=btn
log4j.appender.DATABASE.sql=INSERT INTO TLOG4J VALUES ('%-5p','%d{yyyy-mm-dd hh:mm:ss} %-5p (%F:%L) - %m%n','%d{yyyy-mm-dd hh:mm:ss}')
log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout
log4j.appender.DATABASE.layout.ConversionPattern=%d %-5p (%F:%L) - %m%n
#定义文件
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=G:/Tomcat5/webapps/communitymedical/log/test
#log4j.appender.file.MaxFileSize=10KB
#log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %-5p (%F:%L) - %m%n
标签:logging,console,commons,log4j,file,apache,org,appender
From: https://blog.51cto.com/u_16245757/7352056