Skip to content
Advertisement

LOG4J2 RollingFile Appender not writing to file

I’m trying to configure log4j2 by code but my RollingFile and my custom plugin”JTxtLogAppender” not working. The problem with the RollingFile is that the log file is created but it not writing to the file, neither to my custom plugin, but output to console is working.

Here my code:

        String logFile = logPath + "dxc.log";
        String logFilePattern = logPath + "dxc-%d{yyyy-MM-dd}.log";

        PluginManager.addPackage("dxc");

        ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();

        LayoutComponentBuilder layout = builder.newLayout("PatternLayout");
        layout.addAttribute("pattern", "%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c: %m%n");

        AppenderComponentBuilder console = builder.newAppender("console", "Console")
                .add(layout);
        builder.add(console);

        ComponentBuilder triggeringPolicy = builder.newComponent("Policies")
                .addComponent(builder.newComponent("TimeBasedTriggeringPolicy"))
                .addComponent(builder.newComponent("SizeBasedTriggeringPolicy").addAttribute("size", "100M"));
        AppenderComponentBuilder rollingFile = builder.newAppender("rollingFile", "RollingFile")
                .addAttribute("fileName", logFile)
                .addAttribute("filePattern", logFilePattern)
                .addAttribute("immediateFlush", true)
                .addAttribute("append", true)
                .addComponent(triggeringPolicy)
                .add(layout);
        builder.add(rollingFile);

        AppenderComponentBuilder jTxtAppender = builder.newAppender("jTxtLogAppender", "JTxtLogAppender")
                .add(layout);
        builder.add(jTxtAppender);

        RootLoggerComponentBuilder rootLogger = builder.newRootLogger(Level.INFO)
                .add(builder.newAppenderRef("console"))
                .add(builder.newAppenderRef("rollingFile"))
                .add(builder.newAppenderRef("jTxtLogAppender"));
        builder.add(rootLogger);

        Configurator.initialize(builder.build());
        Configurator.setRootLevel(Level.INFO);

Advertisement

Answer

Before all of the above code I execute

LogManager.shutdown();

and it works now.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement