一个有用的log4j.properties文件模板
- ##Log4J的配置之简单使它遍及于越来越多的应用中了
- ##Log4J配置文件实现了输出到控制台、文件、回滚文件、发送日志邮件、输出到数据库日志表、自定义标签等全套功能。择其一二使用就够用了。
- ##此文件(log4j.properties)内容来自网络,非本文作者liigo原创。
- log4j.rootLogger = DEBUG, CONSOLE,A1
- log4j.addivity.org.apache = true
- # 应用于控制台
- log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender
- log4j.appender.Threshold = DEBUG
- log4j.appender.CONSOLE.Target = System.out
- log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
- log4j.appender.CONSOLE.layout.ConversionPattern = [framework] % d - % c -%- 4r [ % t] %- 5p % c % x - % m % n
- #log4j.appender.CONSOLE.layout.ConversionPattern = [start] % d {DATE} [DATE] % n % p[PRIORITY] % n % x[NDC] % n % t[THREAD] n % c[CATEGORY] % n % m[MESSAGE] % n % n
- #应用于文件
- log4j.appender.FILE = org.apache.log4j.FileAppender
- log4j.appender.FILE.File = 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
- # 应用于文件回滚
- log4j.appender.ROLLING_FILE = org.apache.log4j.RollingFileAppender
- log4j.appender.ROLLING_FILE.Threshold = ERROR
- log4j.appender.ROLLING_FILE.File = rolling.log
- log4j.appender.ROLLING_FILE.Append = true
- log4j.appender.ROLLING_FILE.MaxFileSize = 10KB
- log4j.appender.ROLLING_FILE.MaxBackupIndex = 1
- log4j.appender.ROLLING_FILE.layout = org.apache.log4j.PatternLayout
- log4j.appender.ROLLING_FILE.layout.ConversionPattern = [framework] % d - % c -%- 4r [ % t] %- 5p % c % x - % m % n
- #应用于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 = FATA
- log4j.appender.MAIL.BufferSize = 10
- log4j.appender.MAIL.From = web@www.wuset.com
- log4j.appender.MAIL.SMTPHost = www.wusetu.com
- log4j.appender.MAIL.Subject = Log4J Message
- log4j.appender.MAIL.To = web@www.wusetu.com
- 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: // localhost:3306/test
- log4j.appender.DATABASE.driver = com.mysql.jdbc.Driver
- log4j.appender.DATABASE.user = root
- log4j.appender.DATABASE.password =
- log4j.appender.DATABASE.sql = INSERT INTO LOG4J (Message) VALUES ( ' [framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n ' )
- log4j.appender.DATABASE.layout = org.apache.log4j.PatternLayout
- log4j.appender.DATABASE.layout.ConversionPattern = [framework] % d - % c -%- 4r [ % t] %- 5p % c % x - % m % n
- 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 = corlin@cybercorlin.net
- log4j.appender.im.layout = org.apache.log4j.PatternLayout
- log4j.appender.im.layout.ConversionPattern = [framework] % d - % c -%- 4r [ % t] %- 5p % c % x - % m % n
- # 结束
##Log4J的配置之简单使它遍及于越来越多的应用中了 ##Log4J配置文件实现了输出到控制台、文件、回滚文件、发送日志邮件、输出到数据库日志表、自定义标签等全套功能。择其一二使用就够用了。 ##此文件(log4j.properties)内容来自网络,非本文作者liigo原创。 log4j.rootLogger = DEBUG, CONSOLE,A1 log4j.addivity.org.apache = true # 应用于控制台 log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender log4j.appender.Threshold = DEBUG log4j.appender.CONSOLE.Target = System.out log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern = [framework] % d - % c -%- 4r [ % t] %- 5p % c % x - % m % n #log4j.appender.CONSOLE.layout.ConversionPattern = [start] % d {DATE} [DATE] % n % p[PRIORITY] % n % x[NDC] % n % t[THREAD] n % c[CATEGORY] % n % m[MESSAGE] % n % n #应用于文件 log4j.appender.FILE = org.apache.log4j.FileAppender log4j.appender.FILE.File = 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 # 应用于文件回滚 log4j.appender.ROLLING_FILE = org.apache.log4j.RollingFileAppender log4j.appender.ROLLING_FILE.Threshold = ERROR log4j.appender.ROLLING_FILE.File = rolling.log log4j.appender.ROLLING_FILE.Append = true log4j.appender.ROLLING_FILE.MaxFileSize = 10KB log4j.appender.ROLLING_FILE.MaxBackupIndex = 1 log4j.appender.ROLLING_FILE.layout = org.apache.log4j.PatternLayout log4j.appender.ROLLING_FILE.layout.ConversionPattern = [framework] % d - % c -%- 4r [ % t] %- 5p % c % x - % m % n #应用于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 = FATA log4j.appender.MAIL.BufferSize = 10 log4j.appender.MAIL.From = web@www.wuset.com log4j.appender.MAIL.SMTPHost = www.wusetu.com log4j.appender.MAIL.Subject = Log4J Message log4j.appender.MAIL.To = web@www.wusetu.com 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: // localhost:3306/test log4j.appender.DATABASE.driver = com.mysql.jdbc.Driver log4j.appender.DATABASE.user = root log4j.appender.DATABASE.password = log4j.appender.DATABASE.sql = INSERT INTO LOG4J (Message) VALUES ( ' [framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n ' ) log4j.appender.DATABASE.layout = org.apache.log4j.PatternLayout log4j.appender.DATABASE.layout.ConversionPattern = [framework] % d - % c -%- 4r [ % t] %- 5p % c % x - % m % n 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 = corlin@cybercorlin.net log4j.appender.im.layout = org.apache.log4j.PatternLayout log4j.appender.im.layout.ConversionPattern = [framework] % d - % c -%- 4r [ % t] %- 5p % c % x - % m % n # 结束
Log4J发日志邮件给多个接收者及标题、正文乱码问题
以前开发的系统没有单独的日志管理,所有的日志统一输出到tomcat后台一个文件里,不几天就是好几G,现在要整体增加一个Log4J管理日志的功能,其实这方面的资料网上多的是。发邮件的配置说明也有,但是具体怎么发,乱码问题怎么解决那就比较少了。
利用javamail发送邮件,你需要导入包mail.jar和activation.jar这两个包 ,否则是没法发邮件的 ,下边配置文件里绿色行显示的就是发给两个接收者ac和ae。
这里会出现中文乱码问题,主要有两方面的乱码,一是标题乱码;二是正文乱码。下边具体说明这两种乱码的解决方案。
一、 标题乱码
Log4J日志邮件的标题在配置文件log4j.properties里设定,如下
log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender
log4j.appender.MAIL.Threshold=FATAL
log4j.appender.MAIL.BufferSize=10
log4j.appender.MAIL.From=ab@163.com
log4j.appender.MAIL.SMTPHost=smtp@163.com
log4j.appender.MAIL.Subject= Log4J提醒您:系统发生了严重错误 log4j.appender.MAIL.To=ac@163.com,ae@163.com
log4j.appender.MAIL.layout=com.sun.DefineLayOut
log4j.appender.MAIL.layout.LocationInfo=true
绿色的行就是标题,log4J配置文件默认的读取方式是ISO-88591,遇到中文会出现乱码,我们可以把这个配置文件log4j.properties用jdk的工具native2asii转换一下编码方式。
命令:native2asii log4j.properties log4jxx.properties
把这个log4jxx.properties改名为log4j.properties取代原来的log4j.properties就ok了。
灰色行重新编码后是:
log4j.appender.MAIL.Subject=Log4J\u63d0\u9192\u60a8\uff1a\u7cfb\u7edf\u53d1\u751f\u4e86\u4e25\u91cd\u9519\u8bef
二、 正文乱码
正文乱码,解决也比较简单。阅读Log4J的源码类SMTPAppender,我们可以发现sendBuffer()方法中有这样一句:
part.setContent(sbuf.toString(), layout.getContentType());
我们继续追踪发现layout就是配置文件里的layout属性对应的布局模式。但是这些布局模式都是继承自Layout,而contentType是只可通过getContentType方法取得,不能修改。所有的布局模式getContentType方法返回的都是”text/plain”;
为处理中文乱码,我们可以写一个布局模式。如果你要使用HTMLLayout,我们就写一个HTMLLayout的子类,覆盖HTMLLayout的getContentType方法即可。假如我要用org.apache.log4j.HTMLLayout。我们就可以写一个DefineLayOut类,代码如下:
package com.sun;
import org.apache.log4j.HTMLLayout;
public class DefineLayOut extends HTMLLayout{
public String getContentType() {
return "text/html;charset=GBK";
}
}