logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tarun Sharma <tarun.k.sha...@oracle.com>
Subject Unable to get the appender working if added dynamically.
Date Mon, 05 Dec 2016 10:41:20 GMT
Hi,

 

I am trying to get a appender working by adding it dynamically.

I have a logger config with one RollingFileAppender. Now, after the context has initialized,
I want to add another appender to the Logger, so that It now logs to both the appenders.

 

The reason for doing this is to achieve temporary log archives. I have jobs that run daily.
If a job fails, I want to pick all the relative logs for that particular job and make them
available for future auditing.

 

I have taken help from http://logging.apache.org/log4j/2.x/manual/customconfig.html#AddingToCurrent

But I cannot get it to work. Please help.

 

Here is my code.

 

 

name = PropertiesConfig

status=debug

packages=org.common.util.trace.plugins

 

appenders=rolling

appender.rolling.type=RollingFile

appender.rolling.name=RollingFile_TEST

appender.rolling.fileName=D:/rolling/test_${date:MMdd}.log

appender.rolling.filePattern=D:/rolling/test_%d{MMdd}.log

appender.rolling.layout.type=PatternLayout

appender.rolling.layout.pattern=%d{ABSOLUTE} %level{length=1} [%C{1}:%L] %m%n

appender.rolling.policies.type = Policies

appender.rolling.policies.time.type = TimeBasedTriggeringPolicy

appender.rolling.policies.time.interval = 1

appender.rolling.policies.time.modulate = true

appender.rolling.policies.size.type = SizeBasedTriggeringPolicy

appender.rolling.policies.size.size=100MB

appender.rolling.strategy.type = DefaultRolloverStrategy

appender.rolling.strategy.max = 5

 

rootLogger.level=info

rootLogger.appenderRefs=test

rootLogger.appenderRef.test.ref= RollingFile_TEST

 

loggers = util

 

logger.util.name = org.common.util

logger.util.level=info

logger.util.additivity=false

logger.util.appenderRefs=rolling

logger.util.appenderRef.rolling.ref=RollingFile_TEST

 

 

 

@Plugin(name="ReusableFile", category="Core", elementType = "appender", printObject = true)
public class ReusableFileAppender extends AbstractWriterAppender {

    private static final class AppendingWriterManager extends WriterManager {
        public AppendingWriterManager(StringLayout layout, String fileName) throws FileNotFoundException
{
            super(new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName,
true))),
                    true), fileName, layout, true);
        }

        @Override
        protected synchronized void write(String str) {
            super.write(str);
        }
    }

    private File logFile;

    private final FileAppender fileAppender;


    private ReusableFileAppender(String name, String fileName, StringLayout layout, WriterManager
manager) {
        super(name, layout, null, true, true, manager);
        logFile = new File(fileName);
        fileAppender = FileAppender.createAppender(fileName, null, null, name, null, null,
null, null, layout, null, null, null, null);
    }

    public void setFile(File file)
    {
        this.logFile = file;
    }



    @PluginFactory
    public static ReusableFileAppender createAppender(
            @PluginAttribute("name") String name,
            @PluginElement("Layout") PatternLayout layout,
            @PluginAttribute("fileName") String fileName
    ) {
        WriterManager manager = createWriterManager(layout, fileName);
        return new ReusableFileAppender(name, fileName, layout, manager);
    }

    private static WriterManager createWriterManager(@PluginElement("Layout") PatternLayout
layout, @PluginAttribute("fileName") String fileName) {
        WriterManager manager;
        try {
            manager = new AppendingWriterManager(layout, fileName);
        } catch (FileNotFoundException e) {
            throw new IllegalStateException("Failed to create writer.", e);
        }
        return manager;
    }

    @Override
    public AppendingWriterManager getManager() {
        return (AppendingWriterManager) super.getManager();
    }


    @Override
    public void start() {
        super.start();
        fileAppender.start();
    }

    @Override
    public void stop() {
        super.stop();
        fileAppender.stop();
    }

    /** Does nothing. */
    @Override
    public void append(LogEvent event) {
        fileAppender.append(event);
    }

}

 

Public class AdditionalLogger {

 

Logger logger = LogManager.getLogger("org.common.util");

 

Public static void main(Sting args[])

{

        LoggerContext context = LoggerContext.getContext(false);
       final Configuration config = context.getConfiguration();
       ReusableFileAppender.createAppender("reusable", PatternLayout.createDefaultLayout(),
"D:/rolling/reusable.log");
       appender.start();
      config.addAppender(appender);
      ((org.apache.logging.log4j.core.Logger)logger).get().addAppender(appender, null,null);

 
      Logger.info("additional Info");   // prints to the configuration in properties file
but not to new appender.
      appender.stop();

}

 

 

}

 

 

Regards,

Tarun

 

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message