Вопрос по java – Как создать разные файлы журнала для разных пакетов, используя один и тот же log4j logger?

12

Я пытаюсь настроить отдельные файлы журналов для разных пакетов. Я использую класс Wrapper дляlog4j регистратор. Каждый класс в моем приложении вызывает один и тот же класс-обертку. Мой класс обертки:

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

Это называется так:

MyLogger.write(, , );

Is there a way to configure log4j so that it outputs logging of different packages to different files?

Спасибо!

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

Ваш Ответ

4   ответа
0

Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededLogger? HashimR
0

Error: User Rate Limit Exceeded

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); 
}
Error: User Rate Limit Exceeded
6

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

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

Error: User Rate Limit Exceeded

package com.daniel.package1;

import com.daniel.logger.MyLogger;

public class ClassA {

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

Error: User Rate Limit Exceededlog4j.propertiesError: User Rate Limit Exceeded

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

Error: User Rate Limit Exceeded

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();
    }
}
Error: User Rate Limit Exceeded HashimR
0

Error: User Rate Limit Exceeded

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

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded HashimR
Error: User Rate Limit Exceeded HashimR

Похожие вопросы