Log4j 日志文件输出编码设置

Log4j 日志文件输出编码设置
----------------------------

1
log4j.properties

log4j.rootCategory=INFO, CONSOLE, LOGFILE

# 包消息级别
log4j.logger.org.hibernate=INFO

# CONSOLE settings.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=ERROR
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{MM-dd HH:mm:ss.SSS} [%-5p] %c - %m%n

# LOG FILE settings.
log4j.appender.LOGFILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.File=log/debug.log
log4j.appender.LOGFILE.File.DatePattern='.'yyyy-MM-dd

#log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout 
# 特殊号码 ㄧ  ■ 显示
log4j.appender.LOGFILE.layout=cn.bisoft.component.log.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=[%d{mm:ss.SSS}] ㄧ %p ㄧ %-20C{1}.%-20M(L: %-4L) ■ %-50m ㄧ (%r ms)%n

# 输出消息编码
log4j.appender.LOGFILE.encoding=UTF-8




2
PatternLayout.java

package cn.bisoft.component.log.log4j;

import java.io.UnsupportedEncodingException;

import org.apache.log4j.helpers.PatternParser;

public class PatternLayout extends org.apache.log4j.PatternLayout
{

    @Override
    protected PatternParser createPatternParser(String pattern)
    {
        try
        {
            pattern = new String(pattern.getBytes("iso-8859-1"));
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        return super.createPatternParser((pattern));
    }
    
}





3.
日志输出:
debug.log

[33:22.437] ㄧ INFO ㄧ NativeApiIllustrationTest.setUp               (L: 39  ) ■ 我是中国人■                                             ㄧ (1297 ms)
[33:22.734] ㄧ INFO ㄧ SessionFactoryImpl  .close               (L: 927 ) ■ closing                                            ㄧ (1594 ms)


你可能感兴趣的:(log4j)