简体   繁体   English

无法关闭特定附加程序的日志记录

[英]Can't turn off logging for specific appender

Here log4j.xml这里 log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration debug="true"
    xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="Console" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <!-- l, L, M - is extremely slow. It's use should be avoided unless execution 
                speed is not an issue. -->
            <param name="ConversionPattern" value="%d{[dd.MM.yyyy HH:mm:ss.SSS]} %l %p:%n    %m%n" />
        </layout>
    </appender>

    <appender name="File" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="Encoding" value="UTF-8" />
        <param name="File" value="logs/trace.log" />
        <param name="Append" value="true" />
        <layout class="org.apache.log4j.PatternLayout">
            <!-- l, L, M - is extremely slow. It's use should be avoided unless execution 
                speed is not an issue. -->
            <param name="ConversionPattern" value="%d{[dd.MM.yyyy HH:mm:ss.SSS]} %l %p:%n    %m%n" />
        </layout>
    </appender>

    <root>
        <level value="ALL" />
        <appender-ref ref="Console" />
        <appender-ref ref="File" />
    </root>

</log4j:configuration>

Use org.apache.log4j使用org.apache.log4j

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

private static Logger logger = Logger.getLogger(UserOptions.class);;

AppenderSkeleton consoleAppender = (AppenderSkeleton) Logger.getRootLogger().getAppender("Console");
        AppenderSkeleton fileAppender = (AppenderSkeleton) Logger.getRootLogger().getAppender("File");
        if (!getOptionsAdditionalLoggingIsToConsole()) {
            consoleAppender.setThreshold(Level.OFF);
        }
        if (!getOptionsAdditionalLoggingIsToFile()) {
            fileAppender.setThreshold(Level.OFF);
}

So as result I can turn off logging for specific appender ( Console, File )因此,我可以关闭特定附加程序(控制台、文件)的日志记录

Nice.好的。

Here my log4j2.xml这是我的log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration package="log4j.test" status="WARN">
    <Properties>
        <Property name="baseDir">logs</Property>
        <Property name="patterLayout">%d{[dd.MM.yyyy HH:mm:ss.SSS]} [%t] %l %p:%n
            %m%n</Property>
    </Properties>

    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="${patterLayout}" />
        </Console>
        <RollingFile name="File" fileName="${baseDir}/application.log"
            filePattern="${baseDir}/application.%d{yyyy-MM-dd}.log.gz"
            ignoreExceptions="false">
            <PatternLayout>
                <Pattern>${patterLayout}</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" />
            </Policies>
            <DefaultRolloverStrategy max="5" />
        </RollingFile>
    </Appenders>

    <Loggers>
        <!-- Levels from lowest to highest are: ALL, TRACE, DEBUG, INFO, WARN, 
            ERROR, FATAL, OFF. The root category is used for all loggers unless a more 
            specific logger matches. If none of the loggers are assigned a level, then 
            all loggers inherit the level of the root logger which is set to DEBUG by 
            default -->
        <Root level="trace">
            <AppenderRef ref="Console" />
            <AppenderRef ref="File" />
        </Root>
    
    </Loggers>
</Configuration>

Now I want to use org.slf4j现在我想使用org.slf4j

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;

private static Logger logger = LoggerFactory.getLogger(UserOptions.class);;

LoggerContext loggerContext = (LoggerContext) LogManager.getContext();
        Configuration configuration = loggerContext.getConfiguration();

        LoggerConfig rootLoggerConfig = configuration.getLoggerConfig("");
        Appender consoleAppender = rootLoggerConfig.getAppenders().get("Console");
        Appender fileAppender = rootLoggerConfig.getAppenders().get("File");

        if (!getOptionsAdditionalLoggingIsToConsole()) {
            consoleAppender.setThreshold(org.apache.log4j.Level.OFF);
}

But I get error:但我得到错误:

error: cannot find symbol method setThreshold(Level)

"Now I want to use org.slf4j " : if you just want to use SLF4j, you can use its Log4j 1.2 binding. “现在我想使用org.slf4j :如果你只想使用 SLF4j,你可以使用它的 Log4j 1.2 绑定。 I assume you also want to upgrade the logging backend from Log4j 1.2 to Log4j 2.x.我假设您还想将日志记录后端从 Log4j 1.2 升级到 Log4j 2.x。

You obtain a compilation error, since LoggerConfig does not have a setThreshold method.您会收到编译错误,因为LoggerConfig没有setThreshold方法。 Therefore you must use the other methods at your disposal.因此,您必须使用您可以使用的其他方法。

If you just want to remove the console appender use:如果您只想删除控制台附加程序,请使用:

        LoggerConfig rootLoggerConfig = configuration.getRootLogger();
        rootLoggerConfig.removeAppender("Console");

If you want to keep it, but silence it, you need to remove it and add it with a different level:如果要保留它,但要使其静音,则需要将其删除并以不同的级别添加:

        LoggerConfig rootLoggerConfig = configuration.getRootLogger();
        Appender consoleAppender = rootLoggerConfig.getAppenders().get("Console");
        rootLoggerConfig.removeAppender("Console");
        rootLoggerConfig.addAppender(consoleAppender, Level.OFF, null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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