本文介绍了如何使用相同的 log4j 记录器为不同的包创建不同的日志文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为不同的包设置单独的日志文件.我正在为 log4j 记录器使用 Wrapper 类.我的应用程序中的每个类都调用相同的包装类.我的包装类:

I'm trying to set up separate log files for different packages. I'm using a Wrapper class for a log4j logger. Every class in my application calls same wrapper class. My wrapper class:

public class MyLogger
{
    private static Logger logger = Logger.getLogger(MyLogger.class.getName());
    ....
    ....
}

它是这样调用的:

MyLogger.write(, , );

有没有办法配置 log4j 以便将不同包的日志输出到不同的文件?

谢谢!

这是我的 log4j.properties 文件:

log4j.rootLogger=DEBUG, infoout, aar
log4j.logger.com.businessservice.datapopulation=DEBUG, aar
log4j.additivity.com.businessservice.datapopulation=false

log4j.appender.infoout = org.apache.log4j.RollingFileAppender
log4j.appender.infoout.file=/app/aar_frontend.log
log4j.appender.infoout.append=true
log4j.appender.infoout.Threshold=DEBUG
log4j.appender.infoout.MaxFileSize=2MB
log4j.appender.infoout.MaxBackupIndex=10
log4j.appender.infoout.layout = org.apache.log4j.PatternLayout
log4j.appender.infoout.layout.ConversionPattern = %m%n

log4j.appender.aar = org.apache.log4j.RollingFileAppender
log4j.appender.aar.file=/app/aar/aar_backend.log
log4j.appender.aar.append=true
log4j.appender.aar.Threshold=DEBUG
log4j.appender.aar.MaxFileSize=2MB
log4j.appender.aar.MaxBackupIndex=10
log4j.appender.aar.layout = org.apache.log4j.PatternLayout
log4j.appender.aar.layout.ConversionPattern = %m%n

推荐答案

如果您在 MyLogger 类中创建一个静态 Logger,那么您就有一个 Logger 实例,名称设置为 MyLogger.当您从其他包调用该记录器时,Log4j 无法确定这些调用的来源,因为它们都使用相同的记录器.

If you create a static Logger within MyLogger class, then you have one Logger instance, with the name set to MyLogger. When you call that logger from other packages, Log4j is not able to determine the origin of those calls, as they all use the same Logger.

处理它的最好方法,是在每个类中定义一个单独的 Logger,但是如果您想使用一个类作为与 Log4j 的联系点,那么您可以这样做:

The best way to handle it, is to define a separate Logger within each class, but if you want to use one class as a point of contact with Log4j, then you can do this:

package com.daniel.logger;
import org.apache.log4j.Logger;

import com.daniel.package1.ClassA;
import com.daniel.package2.ClassB;

public class MyLogger{

    public static void write(String message, Class<?> clazz){
        Logger.getLogger(clazz).info(message);
    }

    public static void main(String[] args){
        ClassA.log();
        ClassB.log();
    }
}

然后,使用它的类之一可能如下所示:

Then, one of the class using it could look like:

package com.daniel.package1;

import com.daniel.logger.MyLogger;

public class ClassA {

    public static void log(){
        MyLogger.write("ClassA",ClassA.class);
    }
}

log4j.properties 文件看起来像:

log4j.appender.package1=org.apache.log4j.FileAppender
log4j.appender.package1.File=package1.log
log4j.appender.package1.layout=org.apache.log4j.PatternLayout

log4j.appender.package2=org.apache.log4j.FileAppender
log4j.appender.package2.File=package2.log
log4j.appender.package2.layout=org.apache.log4j.PatternLayout

log4j.logger.com.daniel.package1=DEBUG,package1
log4j.logger.com.daniel.package2=DEBUG,package2

如果您不想从 ClassA 传递 Class,您可以使用带有反射的讨厌技巧,即获取调用类的名称,但由于性能下降,我不建议这样做:

If you don't want to pass the Class from ClassA, you could use a nasty trick with reflection, that gets the calling class' name, but I wouldn't recommend that due to a performance hit:

public class MyLogger
{

    public static void write(String message){
        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
        Logger.getLogger(stackTraceElements[2].getClassName()).info(message);
    }

    public static void main(String[] args){
        ClassA.log();
        ClassB.log();
    }
}

这篇关于如何使用相同的 log4j 记录器为不同的包创建不同的日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 00:50