log4j开发配置

1. 导入相关的lib包:commons-logging-1.0.4.jar,log4j-1.2.11.jar
2. 然后配置log4j.properties
3. 配置完后就可以使用了,在类里写上
/**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(UserAction.class);
这一步你可以在网上下一个Eclipse的插件叫log4E.就省的自己写了,直接生成

4. 最后给你我的log4j.properties内容:

log4j.rootLogger = INFO, CONSOLE, ROLLING_FILE
log4j.addivity.org.apache = true

#open-source project log level
log4j.logger.com.ods.struts=WARN
log4j.logger.com.ods.spring=WARN
log4j.logger.com.ods.hibernate=WARN
log4j.logger.com.util=WARN
log4j.logger.com.common=WARN
log4j.logger.com.opensymphony.xwork2=WARN
log4j.logger.com.opensymphony.xwork2.interceptor=WARN
log4j.logger.com.opensymphony.xwork2.ognl.OgnlValueStack = WARN
log4j.logger.org.hibernate.impl.SessionFactoryImpl=WARN
log4j.logger.org.hibernate.impl.SessionImpl=WARN
log4j.logger.org.hibernate.jdbc=WARN
log4j.logger.org.hibernate.SQL=INFO
log4j.logger.org.hibernate=WARN
log4j.logger.org.springframework=WARN

# 应用于控制台
log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold = INFO
log4j.appender.CONSOLE.Target = System.out
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss} %l - %m%n

# 应用于文件回滚
log4j.appender.ROLLING_FILE = org.apache.log4j.RollingFileAppender
log4j.appender.ROLLING_FILE.Threshold = WARN
log4j.appender.ROLLING_FILE.File = logs/WARN.log
log4j.appender.ROLLING_FILE.Append = true
log4j.appender.ROLLING_FILE.MaxFileSize = 10000KB
log4j.appender.ROLLING_FILE.MaxBackupIndex = 1000
log4j.appender.ROLLING_FILE.layout = org.apache.log4j.PatternLayout
log4j.appender.ROLLING_FILE.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss} %l - %m%n

#应用于文件
log4j.appender.FILE = org.apache.log4j.FileAppender
log4j.appender.FILE.File = logs/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

#应用于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

# 发送日志给邮件
log4j.appender.MAIL = org.apache.log4j.net.SMTPAppender
log4j.appender.MAIL.Threshold = FATAL
log4j.appender.MAIL.BufferSize = 10
log4j.appender.MAIL.From = [email protected]
log4j.appender.MAIL.SMTPHost = www.wusetu.com
log4j.appender.MAIL.Subject = Log4J Message
log4j.appender.MAIL.To = [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

# 用于数据库
log4j.appender.DATABASE = org.apache.log4j.jdbc.JDBCAppender
log4j.appender.DATABASE.URL = jdbc:mysql://127.0.0.1:3306/ebid?useUnicode=true&characterEncoding=utf8
log4j.appender.DATABASE.driver = com.mysql.jdbc.Driver
log4j.appender.DATABASE.user = root
log4j.appender.DATABASE.password = lzw0310
log4j.appender.DATABASE.sql = INSERT INTO t_system_log (slog_desc, create_time) VALUES ('[%-5p] %d{yyyy-MM-dd HH:mm:ss} %l - %m%n', now())
log4j.appender.DATABASE.layout = org.apache.log4j.PatternLayout
log4j.appender.DATABASE.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss} %l - %m%n

# XML文件
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

你可能感兴趣的:(apache,mysql,log4j,Hibernate,socket)