Create error log file

This commit is contained in:
preland 2024-09-17 11:44:20 -04:00 committed by GitHub
parent a4e43f1045
commit d4f1dc5b8e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -21,6 +21,7 @@ import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder; import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.filter.ThresholdFilter;
import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.rolling.FixedWindowRollingPolicy; import ch.qos.logback.core.rolling.FixedWindowRollingPolicy;
import ch.qos.logback.core.rolling.RollingFileAppender; import ch.qos.logback.core.rolling.RollingFileAppender;
@ -52,11 +53,12 @@ public class Log {
SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = new SizeBasedTriggeringPolicy<>(); SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = new SizeBasedTriggeringPolicy<>();
triggeringPolicy.setMaxFileSize(FileSize.valueOf("10MB")); triggeringPolicy.setMaxFileSize(FileSize.valueOf("10MB"));
triggeringPolicy.setContext(loggerContext);
triggeringPolicy.start(); triggeringPolicy.start();
PatternLayoutEncoder encoder = new PatternLayoutEncoder(); PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext); encoder.setContext(loggerContext);
encoder.setPattern("%d{MMM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{15}: %msg %xEx%n"); encoder.setPattern("%d{MMM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{15}: %msg%n");
encoder.start(); encoder.start();
appender.setEncoder(encoder); appender.setEncoder(encoder);
@ -64,25 +66,44 @@ public class Log {
appender.setTriggeringPolicy(triggeringPolicy); appender.setTriggeringPolicy(triggeringPolicy);
appender.start(); appender.start();
logbackLogger = loggerContext.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
logbackLogger.addAppender(appender);
logbackLogger.setLevel(Level.INFO);
// log errors in separate file // log errors in separate file
// not working as expected still.... damn logback... // not working as expected still.... damn logback...
/* FileAppender errorAppender = new FileAppender(); PatternLayoutEncoder errorEncoder = new PatternLayoutEncoder();
errorAppender.setEncoder(encoder); errorEncoder.setContext(loggerContext);
errorEncoder.setPattern("%d{MMM-dd HH:mm:ss.SSS} [%thread] %-5level %logger: %msg%n%ex");
errorEncoder.start();
RollingFileAppender<ILoggingEvent> errorAppender = new RollingFileAppender<>();
errorAppender.setEncoder(errorEncoder);
errorAppender.setName("Error"); errorAppender.setName("Error");
errorAppender.setContext(loggerContext); errorAppender.setContext(loggerContext);
errorAppender.setFile(fileName + "_error.log"); errorAppender.setFile(fileName + "_error.log");
LevelFilter levelFilter = new LevelFilter();
levelFilter.setLevel(Level.ERROR); FixedWindowRollingPolicy errorRollingPolicy = new FixedWindowRollingPolicy();
levelFilter.setOnMatch(FilterReply.ACCEPT); errorRollingPolicy.setContext(loggerContext);
levelFilter.setOnMismatch(FilterReply.DENY); errorRollingPolicy.setParent(errorAppender);
levelFilter.start(); errorRollingPolicy.setFileNamePattern(fileName + "_error_%i.log");
errorAppender.addFilter(levelFilter); errorRollingPolicy.setMinIndex(1);
errorRollingPolicy.setMaxIndex(20);
errorRollingPolicy.start();
SizeBasedTriggeringPolicy<ILoggingEvent> errorTriggeringPolicy = new SizeBasedTriggeringPolicy<>();
errorTriggeringPolicy.setMaxFileSize(FileSize.valueOf("10MB"));
errorTriggeringPolicy.start();
ThresholdFilter thresholdFilter = new ThresholdFilter();
thresholdFilter.setLevel("WARN");
thresholdFilter.start();
errorAppender.setRollingPolicy(errorRollingPolicy);
errorAppender.setTriggeringPolicy(errorTriggeringPolicy);
errorAppender.addFilter(thresholdFilter);
errorAppender.start(); errorAppender.start();
logbackLogger.addAppender(errorAppender);*/
logbackLogger = loggerContext.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
logbackLogger.addAppender(errorAppender);
logbackLogger.addAppender(appender);
logbackLogger.setLevel(Level.INFO);
} }
public static void setCustomLogLevel(String pattern, Level logLevel) { public static void setCustomLogLevel(String pattern, Level logLevel) {