English 中文(简体)
如何利用同日志4j的日志,为不同的计划建立不同的日志?
原标题:How to create different log files for different packages using same log4j logger?

I m试图为不同包裹建立单独的记录文档。 用于<代码>log4j的每等舱一米。 在我的申请中,每个班级都使用相同的包装。 我的总结班:

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

这要求:

MyLogger.write(, , );

www.un.org/spanish/ecosoc 是否有混淆记录4j的方法,以便它能够将不同的包裹记录到不同的档案中?

感谢!

<><>Edit>:

我的<编码>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
最佳回答

如果你在我的记录记录仪中制造一个静态的Logger,那么你就有一个Logger案,名字被写给我的记录仪。 当你从其他包裹中称为“记录”时,Log4j无法确定这些电话的来源,因为它们都使用相同的Logger。

处理该问题的最佳办法是在每个类别中分别确定一个Logger,但如果你希望使用一个类别作为与Log4j的联系点,则你可以这样做:

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();
    }
}

之后,使用它的一个类别可以认为:

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

如果你不希望从A级中转过这一级,那么你可以采用一种新式的trick光,这种 reflection子能够打上呼声的班子,但我不建议,由于业绩受到打击:

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();
    }
}
问题回答

你可以这样做(com.myco.a 和 com.myco.b 是你们的2个不同的包件):

log4j.logger.com.myco.a=DEBUG, infoout 
log4j.logger.com.myco.b=DEBUG, aar 

Cheers.

创建2个投影器和2个记录仪将做你想要的事情。

从财产档案中读到所需习惯档案地点,或按此办理。 然后,您可使用以下特定方法更新原木4j推进剂档案中确定的原木4j文档位置:

private void updateLog4jConfiguration(String logFile) { 
    java.util.Properties properties = new Properties(); 
    try { 
        InputStream configStream = getClass().getResourceAsStream( "/log4j.properties");
        properties.load(configStream); 
        configStream.close();
        } 
    catch (IOException e) {
        System.out.println("Error: Cannot laod configuration file "); 
        } 
    properties.setProperty("log4j.appender.FILE.file", logFile); 
    org.apache.log4j.LogManager.resetConfiguration(); 
    org.apache.log4j.PropertyConfigurator.configure(properties); 
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签