简体   繁体   中英

log4j2 incorrectly initiates in web app

I'm using log4j2 in a web app to log user interaction.

This is a web 3.0 app running on tomcat 7.0.42

I am programmaticaly setting the settings using the instructions here

  String dir = System.getProperty("catalina.base");

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

             builder.setStatusLevel(Level.INFO);
             builder.setConfigurationName("RollingBuilder"); // create a rolling file appender
             LayoutComponentBuilder layoutBuilder = builder.newLayout("PatternLayout")
                     .addAttribute("pattern", "%d %-5level: %msg%n");
             ComponentBuilder triggeringPolicy = builder.newComponent("Policies")
                     .addComponent(builder.newComponent("SizeBasedTriggeringPolicy").addAttribute("size",
     "250K"));
             ComponentBuilder defaultStrat = builder.newComponent("DefaultRolloverStrategy")
                 .addAttribute("min", 2)
                 .addAttribute("max", 5);
             AppenderComponentBuilder appenderBuilder = builder.newAppender("rolling", "RollingFile")
                    .addAttribute("fileName", dir + "/logs/admin.log")
                     .addAttribute("filePattern", dir + "/logs/admin-%d{MM-dd-yy}_%i.log")
                     .add(layoutBuilder)
                     .addComponent(triggeringPolicy)
                     .addComponent(defaultStrat);
             builder.add(appenderBuilder);

     // create the new logger
             builder.add(builder.newLogger("AdminLogger", Level.INFO)
                     .add(builder.newAppenderRef("rolling"))
                     .addAttribute("additivity", false));

             builder.add(builder.newRootLogger(Level.DEBUG)
                     .add(builder.newAppenderRef("rolling")));
             LoggerContext ctx = Configurator.initialize(builder.build());
             LOGGER = LogManager.getLogger(UserLog.class.getName());

I have included log4j-web-2.7.jar for a web app.

I don't want auto initialize, so I have set the context-param IS_LOG4J_AUTO_INITIALIZATION_DISABLED to true

<context-param>
    <description>Turn off auto initialization for log4j2</description>
    <param-name>IS_LOG4J_AUTO_INITIALIZATION_DISABLED</param-name>
    <param-value>true</param-value>
 </context-param>

but when I deploy the app i get the following warning in the catalina log file

INFO | jvm 1 | 2016/11/25 20:48:55 | ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

All logging then goes to the console instead of the log file as set up in my initializer.

If I remove the log4j-web-2.7.jar log4j is not initialized when I deploy and the logger works as expected. No warning and the rolling log files are created and behave correctly.

Am I doing something wrong in the context parameter to turn off auto initialize? \\

Can I safely not include the log4j-web-2.7.jar? Or should I do something when the app is deployed and undeployed?

thanks scott

Based on what I read in the manual , I think your context parameter is incorrect. According to the subsection "The Long Story" it should be:

<context-param>
    <param-name>isLog4jAutoInitializationDisabled</param-name>
    <param-value>true</param-value>
</context-param>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM