程序化Logback设置和文件日志记录问题。

huangapple go评论74阅读模式
英文:

Programmatic Logback setup and file logging issue

问题

以下是翻译好的内容:

在一个基本应用程序中,我有3个依赖项:

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.10</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.3</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.29</version>
    </dependency>
</dependencies>

我的目标是使用Logback编写一个日志记录器。我的约束是以编程方式开发它。我为我的日志记录器创建了一个配置类:

public class LogbackConfig {
    // ...
}

我可以在任何地方调用这个类,例如在这里:

public class Main {
    public static void main(String[] args) {
        // ...
    }
}

我的测试类只是记录一些文本。我的问题是,我在控制台上正确显示信息。我也正确地创建了日志文件,但是没有写入任何内容。

英文:

In a base app i have 3 dependencies :

&lt;dependencies&gt;
    &lt;!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
        &lt;artifactId&gt;lombok&lt;/artifactId&gt;
        &lt;version&gt;1.18.10&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;ch.qos.logback&lt;/groupId&gt;
        &lt;artifactId&gt;logback-classic&lt;/artifactId&gt;
        &lt;version&gt;1.2.3&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
        &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
        &lt;version&gt;1.7.29&lt;/version&gt;
    &lt;/dependency&gt;

&lt;/dependencies&gt;

My objective is to code a logger using logback. My constraint was to develop this programmatically. I created a configuration class for my logger

public class LogbackConfig {
    public static Logger getLogger(LoggerContext logCtx, String name) {
        Logger log = logCtx.getLogger(name);
        log.setAdditive(false);
        log.setLevel(Level.INFO);
        log.addAppender(getLogConsoleAppender(logCtx, getLogEncoder(logCtx)));
        log.addAppender(getLogFileAppender(logCtx, getLogEncoder(logCtx)));

        return log;
    }
    public static LoggerContext loggerContext() {
        LoggerContext logCtx = (LoggerContext) LoggerFactory.getILoggerFactory();
        PatternLayoutEncoder logEncoder = getLogEncoder(logCtx);
        ConsoleAppender&lt;ILoggingEvent&gt; consoleAppender = getLogConsoleAppender(logCtx, logEncoder);
        RollingFileAppender&lt;ILoggingEvent&gt; logFileAppender = getLogFileAppender(logCtx, logEncoder);
        SizeBasedTriggeringPolicy&lt;ILoggingEvent&gt; triggeringPolicy = getTriggeringPolicy(logCtx, logFileAppender);
        FixedWindowRollingPolicy rollingPolicy = getRollingPolicy(logCtx, logFileAppender);

        return logCtx;
    }

    private static PatternLayoutEncoder getLogEncoder(LoggerContext loggerContext) {
        PatternLayoutEncoder logEncoder = new PatternLayoutEncoder();
        logEncoder.setContext(loggerContext);
        logEncoder.setPattern(&quot;%d{yyyy/MM/dd HH:mm:ss.SSS} | %-5logger{100} |  %-5level | %C.%M\\(%F:%L\\) | %msg %n&quot;);
        logEncoder.start();
        return logEncoder;
    }
    private static ConsoleAppender&lt;ILoggingEvent&gt; getLogConsoleAppender(LoggerContext loggerContext, PatternLayoutEncoder logEncoder) {
        ConsoleAppender&lt;ILoggingEvent&gt; logConsoleAppender = new ConsoleAppender&lt;&gt;();
        logConsoleAppender.setContext(loggerContext);
        logConsoleAppender.setName(&quot;console&quot;);
        logConsoleAppender.setEncoder(logEncoder);
        logConsoleAppender.start();

        return logConsoleAppender;
    }
    private static RollingFileAppender&lt;ILoggingEvent&gt; getLogFileAppender(LoggerContext loggerContext, PatternLayoutEncoder logEncoder) {
        RollingFileAppender&lt;ILoggingEvent&gt; logFileAppender = new RollingFileAppender&lt;ILoggingEvent&gt;();
        logFileAppender.setContext(loggerContext);
        logFileAppender.setName(&quot;logFile&quot;);
        logFileAppender.setEncoder(logEncoder);
        logFileAppender.setAppend(true);
        logFileAppender.setFile(&quot;logs/logfile.log&quot;);
        return logFileAppender;
    }
    private static SizeBasedTriggeringPolicy&lt;ILoggingEvent&gt; getTriggeringPolicy(LoggerContext loggerContext, RollingFileAppender&lt;ILoggingEvent&gt; logFileAppender) {
        SizeBasedTriggeringPolicy&lt;ILoggingEvent&gt; triggeringPolicy = new SizeBasedTriggeringPolicy&lt;&gt;();
        triggeringPolicy.setContext(loggerContext);
        triggeringPolicy.setMaxFileSize(FileSize.valueOf(&quot;1MB&quot;));
        triggeringPolicy.start();
        logFileAppender.setTriggeringPolicy(triggeringPolicy);

        return triggeringPolicy;
    }

    private static FixedWindowRollingPolicy getRollingPolicy(LoggerContext loggerContext, RollingFileAppender&lt;ILoggingEvent&gt; logFileAppender) {
        FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy();
        rollingPolicy.setContext(loggerContext);
        rollingPolicy.setParent(logFileAppender);
        rollingPolicy.setMinIndex(1);
        rollingPolicy.setMaxIndex(3);
        String rollingName = &quot;logs/log_%i.log&quot;;
        rollingPolicy.setFileNamePattern(rollingName);
        logFileAppender.setRollingPolicy(rollingPolicy);
        rollingPolicy.start();
        logFileAppender.start();
        return rollingPolicy;
    }
}

And i can call this class anyware for example here :

public class Main {
    public static void main(String[] args) {
        Logger log = LogbackConfig.getLogger(
                               LogbackConfig.loggerContext(), 
                               MethodHandles.lookup().lookupClass().toString());

        for(int i = 0; i &lt; 10; i++) {
            log.warn(&quot;write in file ...&quot;);
        }
        Test test = new Test();
        test.testLog();
    }
}

my test class just log some text. My issue is that i display correctly the information on the console. I also correctly create my log file but nothing is written.

答案1

得分: 0

我终于解决了这个问题

public static Logger getLogger(String name) {
    LoggerContext logCtx = (LoggerContext) LoggerFactory.getILoggerFactory();

    PatternLayoutEncoder logEncoder = getPatternLayoutEncoder(logCtx);
    ConsoleAppender<ILoggingEvent> logConsoleAppender = getConsoleAppender(logCtx, logEncoder);

    // 两种不同的模式布局
    logEncoder = new PatternLayoutEncoder();
    logEncoder.setContext(logCtx);
    logEncoder.setPattern("%d{yyyy/MM/dd HH:mm:ss.SSS} | %-5logger{100} |  %-5level | %C.%M\\(%F:%L\\) | %msg %n");
    logEncoder.start();

    RollingFileAppender<ILoggingEvent> logFileAppender = getRollingFileAppender(logCtx, logEncoder);
    SizeBasedTriggeringPolicy<ILoggingEvent> sizeBasedTriggeringPolicy = getSizeBasedTriggeringPolicy(logCtx);

    logFileAppender.setTriggeringPolicy(sizeBasedTriggeringPolicy);

    FixedWindowRollingPolicy logFilePolicy = getFixedWindowRollingPolicy(logCtx, logFileAppender);
    logFilePolicy.start();
    logFileAppender.start();

    Logger logger = logCtx.getLogger(name);
    logger.setAdditive(false);
    logger.setLevel(Level.INFO);
    logger.addAppender(logConsoleAppender);
    logger.addAppender(logFileAppender);
    return logger;
}

private static PatternLayoutEncoder getPatternLayoutEncoder(LoggerContext logCtx) {
    PatternLayoutEncoder logEncoder = new PatternLayoutEncoder();
    logEncoder.setContext(logCtx);
    logEncoder.setPattern("%d{yyyy/MM/dd HH:mm:ss.SSS} | %-5logger{100} |  %-5level | %C.%M\\(%F:%L\\) | %msg %n");
    logEncoder.start();
    return logEncoder;
}
private static ConsoleAppender<ILoggingEvent> getConsoleAppender(LoggerContext logCtx, PatternLayoutEncoder logEncoder) {
    ConsoleAppender<ILoggingEvent> logConsoleAppender = new ConsoleAppender<>();
    logConsoleAppender.setContext(logCtx);
    logConsoleAppender.setName("console");
    logConsoleAppender.setEncoder(logEncoder);
    logConsoleAppender.start();
    return logConsoleAppender;
}
private static RollingFileAppender<ILoggingEvent> getRollingFileAppender(LoggerContext logCtx, PatternLayoutEncoder logEncoder) {
    RollingFileAppender<ILoggingEvent> logFileAppender = new RollingFileAppender<>();
    logFileAppender.setContext(logCtx);
    logFileAppender.setName("logFile");
    logFileAppender.setEncoder(logEncoder);
    logFileAppender.setAppend(true);
    logFileAppender.setFile("logs/logfile.log");
    return logFileAppender;
}
private static SizeBasedTriggeringPolicy<ILoggingEvent> getSizeBasedTriggeringPolicy(LoggerContext logCtx) {
    SizeBasedTriggeringPolicy<ILoggingEvent> sizeBasedTriggeringPolicy = new SizeBasedTriggeringPolicy<>();
    sizeBasedTriggeringPolicy.setContext(logCtx);
    sizeBasedTriggeringPolicy.setMaxFileSize(FileSize.valueOf("1Mb"));
    sizeBasedTriggeringPolicy.start();
    return sizeBasedTriggeringPolicy;
}

private static FixedWindowRollingPolicy getFixedWindowRollingPolicy(LoggerContext logCtx, RollingFileAppender<ILoggingEvent> logFileAppender) {
    FixedWindowRollingPolicy logFilePolicy = new FixedWindowRollingPolicy();
    logFilePolicy.setContext(logCtx);
    logFilePolicy.setParent(logFileAppender);
    logFilePolicy.setMinIndex(1);
    logFilePolicy.setMaxIndex(3);
    String rollingName = "logs/log_%i.log";
    logFilePolicy.setFileNamePattern(rollingName);
    logFileAppender.setRollingPolicy(logFilePolicy);
    logFilePolicy.start();
    return logFilePolicy;
}

在主类中

public class Main {
    public static void main(String[] args) {
        Logger log = LogbackConfig.getLogger(
                        MethodHandles.lookup().lookupClass().toString());

        for(int i = 0; i < 10; i++) {
            log.warn("write in file ...");
        }
        Test test = new Test();
        test.testLog();
    }
}
英文:

I finally solved this :

public static Logger getLogger(String name) {
LoggerContext logCtx = (LoggerContext) LoggerFactory.getILoggerFactory();
PatternLayoutEncoder logEncoder = getPatternLayoutEncoder(logCtx);
ConsoleAppender&lt;ILoggingEvent&gt; logConsoleAppender = getConsoleAppender(logCtx, logEncoder);
// two different patterns layout
logEncoder = new PatternLayoutEncoder();
logEncoder.setContext(logCtx);
logEncoder.setPattern(&quot;%d{yyyy/MM/dd HH:mm:ss.SSS} | %-5logger{100} |  %-5level | %C.%M\\(%F:%L\\) | %msg %n&quot;);
logEncoder.start();
RollingFileAppender&lt;ILoggingEvent&gt; logFileAppender = getRollingFileAppender(logCtx, logEncoder);
SizeBasedTriggeringPolicy&lt;ILoggingEvent&gt; sizeBasedTriggeringPolicy = getSizeBasedTriggeringPolicy(logCtx);
logFileAppender.setTriggeringPolicy(sizeBasedTriggeringPolicy);
FixedWindowRollingPolicy logFilePolicy = getFixedWindowRollingPolicy(logCtx, logFileAppender);
logFilePolicy.start();
logFileAppender.start();
Logger logger = logCtx.getLogger(name);
logger.setAdditive(false);
logger.setLevel(Level.INFO);
logger.addAppender(logConsoleAppender);
logger.addAppender(logFileAppender);
return logger;
}
private static PatternLayoutEncoder getPatternLayoutEncoder(LoggerContext logCtx) {
PatternLayoutEncoder logEncoder = new PatternLayoutEncoder();
logEncoder.setContext(logCtx);
logEncoder.setPattern(&quot;%d{yyyy/MM/dd HH:mm:ss.SSS} | %-5logger{100} |  %-5level | %C.%M\\(%F:%L\\) | %msg %n&quot;);
logEncoder.start();
return logEncoder;
}
private static ConsoleAppender&lt;ILoggingEvent&gt; getConsoleAppender(LoggerContext logCtx, PatternLayoutEncoder logEncoder) {
ConsoleAppender&lt;ILoggingEvent&gt; logConsoleAppender = new ConsoleAppender&lt;&gt;();
logConsoleAppender.setContext(logCtx);
logConsoleAppender.setName(&quot;console&quot;);
logConsoleAppender.setEncoder(logEncoder);
logConsoleAppender.start();
return logConsoleAppender;
}
private static RollingFileAppender&lt;ILoggingEvent&gt; getRollingFileAppender(LoggerContext logCtx, PatternLayoutEncoder logEncoder) {
RollingFileAppender&lt;ILoggingEvent&gt; logFileAppender = new RollingFileAppender&lt;&gt;();
logFileAppender.setContext(logCtx);
logFileAppender.setName(&quot;logFile&quot;);
logFileAppender.setEncoder(logEncoder);
logFileAppender.setAppend(true);
logFileAppender.setFile(&quot;logs/logfile.log&quot;);
return logFileAppender;
}
private static SizeBasedTriggeringPolicy&lt;ILoggingEvent&gt; getSizeBasedTriggeringPolicy(LoggerContext logCtx) {
SizeBasedTriggeringPolicy&lt;ILoggingEvent&gt; sizeBasedTriggeringPolicy = new SizeBasedTriggeringPolicy&lt;&gt;();
sizeBasedTriggeringPolicy.setContext(logCtx);
sizeBasedTriggeringPolicy.setMaxFileSize(FileSize.valueOf(&quot;1Mb&quot;));
sizeBasedTriggeringPolicy.start();
return sizeBasedTriggeringPolicy;
}
private static FixedWindowRollingPolicy getFixedWindowRollingPolicy(LoggerContext logCtx, RollingFileAppender&lt;ILoggingEvent&gt; logFileAppender) {
FixedWindowRollingPolicy logFilePolicy = new FixedWindowRollingPolicy();
logFilePolicy.setContext(logCtx);
logFilePolicy.setParent(logFileAppender);
logFilePolicy.setMinIndex(1);
logFilePolicy.setMaxIndex(3);
String rollingName = &quot;logs/log_%i.log&quot;;
logFilePolicy.setFileNamePattern(rollingName);
logFileAppender.setRollingPolicy(logFilePolicy);
logFilePolicy.start();
return logFilePolicy;
}

And in the main :

public class Main {
public static void main(String[] args) {
Logger log = LogbackConfig.getLogger(
MethodHandles.lookup().lookupClass().toString());
for(int i = 0; i &lt; 10; i++) {
log.warn(&quot;write in file ...&quot;);
}
Test test = new Test();
test.testLog();
}
}

huangapple
  • 本文由 发表于 2020年10月6日 15:43:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64221359.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定