/** * Create an appender that will create a new log each day * * @param context * @param encoder * @return An appender that matches the set up of the logger builder */ private RollingFileAppender<ILoggingEvent> createDailyLogAppender(LoggerContext context, Encoder<ILoggingEvent> encoder){ RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>(); appender.setEncoder(encoder); appender.setFile(file); TimeBasedRollingPolicy<ILoggingEvent> rolling = new TimeBasedRollingPolicy<>(); rolling.setContext(context); rolling.setParent(appender); rolling.setFileNamePattern(getFileWithPattern("%d")); //Set the maximum number of logs, either to the user specified setting or default to 1 if (maxNumberLogs.isPresent() && maxNumberLogs.get() >= 0) { rolling.setMaxHistory(maxNumberLogs.get()); } else { rolling.setMaxHistory(1); } //Do we need to also split files by size? if (divideBasedOnSize()) { SizeAndTimeBasedFNATP<ILoggingEvent> sizeBased = new SizeAndTimeBasedFNATP<>(); sizeBased.setContext(context); sizeBased.setMaxFileSize(getMaxFileSize()); sizeBased.setTimeBasedRollingPolicy(rolling); rolling.setTimeBasedFileNamingAndTriggeringPolicy(sizeBased); } rolling.start(); if(rolling.getTimeBasedFileNamingAndTriggeringPolicy() != null){ rolling.getTimeBasedFileNamingAndTriggeringPolicy().start(); } appender.setRollingPolicy(rolling); return appender; }
/** * Creates Logger that will write logs in html file using slf4j and logback. * The logs will be included in Phial Attachment * * @param context application context */ public PhialLogger(Context context) { logDir = createLogDir(context); clearOldLogs(logDir); final String logDirectory = logDir.getAbsolutePath(); // reset the default context (which may already have been initialized) // since we want to reconfigure it LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); loggerContext.reset(); RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<>(); rollingFileAppender.setContext(loggerContext); rollingFileAppender.setAppend(true); rollingFileAppender.setFile(logDirectory + "/" + LOG_PREFIX + "-latest.html"); SizeAndTimeBasedFNATP<ILoggingEvent> fileNamingPolicy = new SizeAndTimeBasedFNATP<>(); fileNamingPolicy.setContext(loggerContext); fileNamingPolicy.setMaxFileSize(MAX_FILE_SIZE); TimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = new TimeBasedRollingPolicy<>(); rollingPolicy.setContext(loggerContext); rollingPolicy.setFileNamePattern(logDirectory + "/" + LOG_PREFIX + HISTORY_FILE_NAME_PATTERN); rollingPolicy.setMaxHistory(5); rollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(fileNamingPolicy); rollingPolicy.setParent(rollingFileAppender); // parent and context required! rollingPolicy.start(); HTMLLayout htmlLayout = new HTMLLayout(); htmlLayout.setContext(loggerContext); htmlLayout.setPattern(PATTERN); htmlLayout.start(); LayoutWrappingEncoder<ILoggingEvent> encoder = new LayoutWrappingEncoder<>(); encoder.setContext(loggerContext); encoder.setLayout(htmlLayout); encoder.start(); rollingFileAppender.setRollingPolicy(rollingPolicy); rollingFileAppender.setEncoder(encoder); rollingFileAppender.start(); // add the newly created appenders to the root logger; // qualify Logger to disambiguate from org.slf4j.Logger Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); root.setLevel(Level.DEBUG); root.addAppender(rollingFileAppender); // print any status messages (warnings, etc) encountered in logback config StatusPrinter.print(loggerContext); }
@Test public void testConfigureRollingFileAppender() { System.setProperty("config.file", "src/test/resources/rollingFileAppender.conf"); ConfigFactory.invalidateCaches(); LoggerContext context = new LoggerContext(); ConfigConfigurator configurator = new ConfigConfigurator(); configurator.configure(context); int errorCount = 0; int warningCount = 0; for (Status status : context.getStatusManager().getCopyOfStatusList()) { if (status.getLevel() == Status.ERROR) { System.out.println(String.format("ERROR : %s", status.getMessage())); errorCount++; } else if (status.getLevel() == Status.WARN) { System.out.println(String.format("WARN : %s", status.getMessage())); warningCount++; } else if (status.getLevel() == Status.INFO) { System.out.println(String.format("INFO : %s", status.getMessage())); } } assertEquals(0, errorCount); assertEquals(0, warningCount); Logger rootLogger = context.getLoggerList().get(0); Appender<?> appender = rootLogger.getAppender("rolling"); assertTrue(appender.isStarted()); assertTrue(appender instanceof RollingFileAppender); RollingFileAppender<?> rolling = (RollingFileAppender<?>) appender; assertTrue(rolling.getEncoder() instanceof PatternLayoutEncoder); assertTrue(rolling.isStarted()); PatternLayoutEncoder encoder = (PatternLayoutEncoder) rolling.getEncoder(); assertEquals(Charset.forName("UTF-8"), encoder.getCharset()); assertEquals("%date %level %logger %thread %msg%n", encoder.getPattern()); assertEquals("logs/test.log", rolling.getFile()); assertTrue(rolling.getRollingPolicy() instanceof TimeBasedRollingPolicy); TimeBasedRollingPolicy<?> rollingPolicy = (TimeBasedRollingPolicy<?>) rolling.getRollingPolicy(); assertEquals("logs/test%d{yyyy-MM-dd}.%i.log", rollingPolicy.getFileNamePattern()); assertEquals(30, rollingPolicy.getMaxHistory()); assertTrue(rollingPolicy.isStarted()); assertTrue(rollingPolicy.getTimeBasedFileNamingAndTriggeringPolicy() instanceof SizeAndTimeBasedFNATP); SizeAndTimeBasedFNATP<?> triggeringPolicy = (SizeAndTimeBasedFNATP<?>) rollingPolicy.getTimeBasedFileNamingAndTriggeringPolicy(); assertEquals("5MB", triggeringPolicy.getMaxFileSize()); assertTrue(triggeringPolicy.isStarted()); }
private static void initLogs() { Logger logbackLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME); LoggerContext lc = logbackLogger.getLoggerContext(); Logger rootLogger = lc.getLogger(Logger.ROOT_LOGGER_NAME); rootLogger.detachAndStopAllAppenders(); TimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = new TimeBasedRollingPolicy<ILoggingEvent>(); rollingPolicy.setMaxHistory(3); SizeAndTimeBasedFNATP<ILoggingEvent> sizeAndTimeBasedFNATP = new SizeAndTimeBasedFNATP<ILoggingEvent>(); sizeAndTimeBasedFNATP.setMaxFileSize("2MB"); rollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(sizeAndTimeBasedFNATP); rollingPolicy.setFileNamePattern(context.getFilesDir().getPath() + "/logs/old/flickruploader.%d{yyyy-MM-dd}.%i.log"); rollingPolicy.setContext(lc); RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>(); fileAppender.setContext(lc); fileAppender.setFile(getLogFilePath()); fileAppender.setRollingPolicy(rollingPolicy); fileAppender.setTriggeringPolicy(rollingPolicy); rollingPolicy.setParent(fileAppender); PatternLayoutEncoder pl = new PatternLayoutEncoder(); pl.setContext(lc); pl.setCharset(Charset.defaultCharset()); pl.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %class{0}.%method:%L > %msg%n"); pl.setImmediateFlush(false); pl.start(); fileAppender.setEncoder(pl); fileAppender.setName("file"); rollingPolicy.start(); fileAppender.start(); if (Config.isDebug()) { final PatternLayoutEncoder logcatTagPattern = new PatternLayoutEncoder(); logcatTagPattern.setContext(lc); logcatTagPattern.setPattern("%class{0}"); logcatTagPattern.start(); final PatternLayoutEncoder logcatPattern = new PatternLayoutEncoder(); logcatPattern.setContext(lc); logcatPattern.setPattern("[%thread] %method:%L > %msg%n"); logcatPattern.start(); final LogcatAppender logcatAppender = new LogcatAppender(); logcatAppender.setContext(lc); logcatAppender.setTagEncoder(logcatTagPattern); logcatAppender.setEncoder(logcatPattern); logcatAppender.start(); rootLogger.addAppender(logcatAppender); } rootLogger.addAppender(fileAppender); }