日期:2014-05-18  浏览次数:20809 次

如何利用log4j 写日志
我想问下,例如我要将error信息写到 1.log文件里面,
要将正常的信息写到2.log文件;里面,,
想问的是
在log4j.properties里如何的作设置,,
还有在使用的时候怎么用的,,
我自己试了好久都不行

------解决方案--------------------
发个例子让你看看吧.
1.log4j的配置文件.
Java code

# Configuration File for the Logger component in the framework
# This property file needs to be accessible in the classpath in order for the
# logger to configure properly

# set to false to turn off log4j debug statements
log4j.debug=true

# Turns off logging in log4j
# log4j.threshold=OFF

log4j.rootLogger=ERROR,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.MaxFileSize=50000KB
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-4r [%d{ISO8601}] [%t] %-5p %c %x - %m%n

# Configuration for Web Logger
#
log4j.logger.WEB=debug,weblog
log4j.appender.weblog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.weblog.File=f:/hubstar-web.log
log4j.appender.weblog.DatePattern='.'yyyyMM
log4j.appender.weblog.layout=org.apache.log4j.PatternLayout
log4j.appender.weblog.layout.ConversionPattern=%-4r [%d{ISO8601}] [%t] %-5p %c %x - %m%n

# Configuration for BIZ Logger
#
log4j.logger.BIZ=debug,bizlog
log4j.appender.bizlog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.bizlog.File=f:/hubstar-biz.log
log4j.appender.bizlog.DatePattern='.'yyyyMM
log4j.appender.bizlog.layout=org.apache.log4j.PatternLayout
log4j.appender.bizlog.layout.ConversionPattern=%-4r [%d{ISO8601}] [%t] %-5p %c %x - %m%n

# Configuration for PERSISTENCE Logger
#
log4j.logger.PERSISTENCE=debug,persistlog
log4j.appender.persistlog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.persistlog.File=f:/hubstar-persist.log
log4j.appender.persistlog.DatePattern='.'yyyyMM
log4j.appender.persistlog.layout=org.apache.log4j.PatternLayout
log4j.appender.persistlog.layout.ConversionPattern=[%d{ISO8601}] [%t] %-5p %c %x- %m%n

# Configuration for Auditor Logger
#
log4j.logger.AUDITOR=debug,auditorlog
log4j.appender.auditorlog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.auditorlog.File=f:/hubstar-audit.log
log4j.appender.auditorlog.DatePattern='.'yyyyMM
log4j.appender.auditorlog.layout=org.apache.log4j.PatternLayout
log4j.appender.auditorlog.layout.ConversionPattern=[%d{ISO8601}] [%t] %-5p %c - %m%n

log4j.logger.ALERT=debug,alertlog

log4j.appender.alertlog=org.apache.log4j.RollingFileAppender
log4j.appender.alertlog.File=f:/hubstar-alert.log
log4j.appender.alertlog.DatePattern='.'yyyyMM
log4j.appender.alertlog.layout=org.apache.log4j.PatternLayout
log4j.appender.alertlog.layout.ConversionPattern=[%d{ISO8601}] - %m%n

------解决方案--------------------
2.log4j的类.
Java code

package com.ufinity.mealsordering.common;

public interface ILogger {
    public abstract String getLoggerType();

    public abstract boolean debug(String userId, String  method, String message);

    public abstract boolean debug(String userId, String  method, String message, Throwable throwable);

    public abstract boolean info(String userId, String  method, String message);

    public abstract boolean info(String userId, String  method, String message, Throwable throwable);

    public abstract boolean warn(String userId, String  method, String message);

    public abstract boolean warn(String userId, String  method, String message, Throwable throwable);

    public abstract boolean error(String userId, String  method, String message);

    public abstract boolean error(String userId, String  method, String message, Throwable throwable);

    public abstract boolean fatal(String userId, String  method, String message);

    public abstract boolean fatal(String userId, String  method, String message, Throwable throwable);

}


-------------------------------------------------
package com.ufinity.mealsordering.common;

import java.t