简体   繁体   中英

Configuring log4j2 to have different log levels for different packages

I want to configure log4j2 to log my application. I want to log my application with log level DEBUG, but hibernate with log level ERROR.

I looked into the documentation (here: https://logging.apache.org/log4j/2.x/manual/customconfig.html ) and read other sites too, but couldn't resolve my problem of rightly configuring log4j2.

    public static void init(String name) {

        ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
        builder.setStatusLevel(Level.ERROR);
        builder.setConfigurationName("BuilderTest");
        builder.add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT, Filter.Result.NEUTRAL)
                .addAttribute("level", Level.DEBUG));

        AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE")
                .addAttribute("target",ConsoleAppender.Target.SYSTEM_OUT);
        appenderBuilder.add(builder.newLayout("PatternLayout")
                .addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable"));
        appenderBuilder.add(builder.newFilter("MarkerFilter", Filter.Result.DENY, Filter.Result.NEUTRAL)
                .addAttribute("marker", "FLOW"));
        builder.add(appenderBuilder);

        builder.add(builder.newLogger("org.mypackage", Level.DEBUG)
                .add(builder.newAppenderRef("Stdout"))
                .addAttribute("additivity", false));

//        builder.add(builder.newLogger("org.hibernate", Level.ERROR)
//                .add(builder.newAppenderRef("Stdout"))
//                .addAttribute("additivity", false));

        builder.add(builder.newRootLogger(Level.ERROR).add(builder.newAppenderRef("Stdout")));

        LoggerContext ctx = Configurator.initialize(builder.build());
        logger = ctx.getLogger(name);
    }

What I tried is to use the packages names to set specific log levels for each one and to try different levels for the loggers/root logger but in the end I get always too much information (eg my debug messages and the debug messages from hibernate) or too less information.

How do I correctly configure log4j2 to fit my needs?

I've found the solution to my answer. I don't know if it is the correct way of doing it, but it works for me. Here is my log4j2.xml (done via the config file instead of doing it programatically):

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="ERROR">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="org.mypackage" level="DEBUG"/>
        <Logger name="org.hibernate" level="ERROR"/>

        <Root level="DEBUG">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

With this, the code in mypackage will be logged in the DEBUG level and hibernate will be logged in the ERROR level.

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