啃完这个,就只有SLF4J和TINY LOG了。
一,代码
logback.xml
<?xml version="1.0" encoding="UTF-8" ?>
<configuration >
<timestamp key="stampPerSecond" datePattern="yyyyMMdd'T'HHmmss" />
<appender name="sizeAndTimeRollingFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>myLogFileSizeRolling.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>myLogFile.%d{yyyy-MM-dd-HH-mm}.%i.log</fileNamePattern>
<maxFileSize>1KB</maxFileSize>
<maxHistory>30</maxHistory>
<totalSizeCap>15GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{55} %msg%n</pattern>
</encoder>
</appender>
<appender name="rollingFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>myLogFileRolling.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>myLogFile.%d{yyyy-MM-dd-HH-mm}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{55} %msg%n</pattern>
</encoder>
</appender>
<appender name="fileAppender" class="ch.qos.logback.core.FileAppender">
<file>mylogFile-${stampPerSecond}.log</file>
<append>true</append>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} %msg%n</pattern>
</encoder>
</appender>
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} %msg%n</pattern>
</encoder>
</appender>
<root>
<appender-ref ref="consoleAppender" />
<appender-ref ref="fileAppender" />
<!-- <appender-ref ref="sizeAndTimeRollingFileAppender" /> -->
</root>
</configuration>
test.java
package com.example.demo;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.core.ConsoleAppender;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
public class DemoApplication {
public static void main(String[] args) {
// System.setProperty("logback.configurationFile", "D:\\test\\demo\\src\\main\\resources\\logback.xml");
// Logger logger = LoggerFactory.getLogger(DemoApplication.class);
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
PatternLayoutEncoder patternLayoutEncoder = new PatternLayoutEncoder();
patternLayoutEncoder.setContext(loggerContext);
patternLayoutEncoder.setPattern("%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} %msg%n");
patternLayoutEncoder.start();
ConsoleAppender consoleAppender = new ConsoleAppender();
consoleAppender.setContext(loggerContext);
consoleAppender.setName("consoleAppender");
consoleAppender.setEncoder(patternLayoutEncoder);
consoleAppender.start();
Logger logger = loggerContext.getLogger("Main");
logger.setLevel(Level.DEBUG);
logger.setAdditive(false);
logger.addAppender(consoleAppender);
logger.debug("LOG Debug Message");
logger.info("LOG Info message");
logger.warn("LOG Warn Message");
logger.error("LOG error Message");
/*
for (int i = 0; i < 50; i++) {
logger.debug("Debug Message: " + i);
try {
Thread.sleep(500L);
} catch (final InterruptedException e) {
logger.error("An exception occured during processing.", e);
}
}
*/
}
}
二,输出
D:\Java\jdk17\bin\java.exe "-javaagent:D:\JetBrains\IntelliJ IDEA Community Edition 2024.3.1.1\lib\idea_rt.jar=6697:D:\JetBrains\IntelliJ IDEA Community Edition 2024.3.1.1\bin" -Dfile.encoding=UTF-8 -classpath D:\test\demo\target\classes;D:\Java\logback-classic-1.5.9.jar;D:\Java\logback-core-1.5.9.jar;D:\Java\slf4j-api-2.0.9.jar com.example.demo.DemoApplication
2025-01-02 15:48:29 [main] DEBUG Main LOG Debug Message
2025-01-02 15:48:29 [main] INFO Main LOG Info message
2025-01-02 15:48:29 [main] WARN Main LOG Warn Message
2025-01-02 15:48:29 [main] ERROR Main LOG error Message
Process finished with exit code 0