protected Appender buildDailyMaxRollingAppender( String name, String fileName, String pattern, int maxBackupIndex) { RollingFileAppender appender = new RollingFileAppender(); appender.setContext(loggerContext); appender.setName(name); appender.setAppend(true); appender.setFile(new File(getLogPath(), fileName).getAbsolutePath()); TimeBasedRollingPolicy rolling = new TimeBasedRollingPolicy(); rolling.setContext(loggerContext); rolling.setFileNamePattern( new File(getLogPath(), fileName).getAbsolutePath() + ".%d{yyyy-MM-dd-HH}"); rolling.setMaxHistory(maxBackupIndex); rolling.setParent(appender); rolling.start(); appender.setRollingPolicy(rolling); PatternLayout layout = new PatternLayout(); layout.setContext(loggerContext); layout.setPattern(pattern); layout.start(); appender.setLayout(layout); // 启动 appender.start(); return appender; }
private static void initLogging() { final File logDir = new File( System.getProperty( "user.home")); // new File("."); //getDir("log", Constants.TEST ? // Context.MODE_WORLD_READABLE : MODE_PRIVATE); final File logFile = new File(logDir, "wowdoge.log"); final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); final PatternLayoutEncoder filePattern = new PatternLayoutEncoder(); filePattern.setContext(context); filePattern.setPattern("%d{HH:mm:ss.SSS} [%thread] %logger{0} - %msg%n"); filePattern.start(); final RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>(); fileAppender.setContext(context); fileAppender.setFile(logFile.getAbsolutePath()); final TimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = new TimeBasedRollingPolicy<ILoggingEvent>(); rollingPolicy.setContext(context); rollingPolicy.setParent(fileAppender); rollingPolicy.setFileNamePattern(logDir.getAbsolutePath() + "/wallet.%d.log.gz"); rollingPolicy.setMaxHistory(7); rollingPolicy.start(); fileAppender.setEncoder(filePattern); fileAppender.setRollingPolicy(rollingPolicy); fileAppender.start(); final PatternLayoutEncoder logcatTagPattern = new PatternLayoutEncoder(); logcatTagPattern.setContext(context); logcatTagPattern.setPattern("%logger{0}"); logcatTagPattern.start(); final PatternLayoutEncoder logcatPattern = new PatternLayoutEncoder(); logcatPattern.setContext(context); logcatPattern.setPattern("[%thread] %msg%n"); logcatPattern.start(); // final LogcatAppender logcatAppender = new LogcatAppender(); // logcatAppender.setContext(context); // logcatAppender.setTagEncoder(logcatTagPattern); // logcatAppender.setEncoder(logcatPattern); // logcatAppender.start(); final ch.qos.logback.classic.Logger log = context.getLogger(Logger.ROOT_LOGGER_NAME); log.addAppender(fileAppender); // log.addAppender(logcatAppender); log.setLevel(Level.INFO); // Level.INFO); SysOutOverSLF4J.sendSystemOutAndErrToSLF4J(); }
private void initLogging() { // We can't log into the wallet specific directories because // logging is initialized well before we've selected one. final File logDir = getDir("log", MODE_PRIVATE); // Context.MODE_WORLD_READABLE final File logFile = new File(logDir, "wallet32.log"); final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); final PatternLayoutEncoder filePattern = new PatternLayoutEncoder(); filePattern.setContext(context); filePattern.setPattern("%d{HH:mm:ss.SSS} [%thread] %logger{0} - %msg%n"); filePattern.start(); final RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>(); fileAppender.setContext(context); fileAppender.setFile(logFile.getAbsolutePath()); final TimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = new TimeBasedRollingPolicy<ILoggingEvent>(); rollingPolicy.setContext(context); rollingPolicy.setParent(fileAppender); rollingPolicy.setFileNamePattern(logDir.getAbsolutePath() + "/wallet32.%d.log.gz"); rollingPolicy.setMaxHistory(7); rollingPolicy.start(); fileAppender.setEncoder(filePattern); fileAppender.setRollingPolicy(rollingPolicy); fileAppender.start(); final PatternLayoutEncoder logcatTagPattern = new PatternLayoutEncoder(); logcatTagPattern.setContext(context); logcatTagPattern.setPattern("%logger{0}"); logcatTagPattern.start(); final PatternLayoutEncoder logcatPattern = new PatternLayoutEncoder(); logcatPattern.setContext(context); logcatPattern.setPattern("[%thread] %msg%n"); logcatPattern.start(); final LogcatAppender logcatAppender = new LogcatAppender(); logcatAppender.setContext(context); logcatAppender.setTagEncoder(logcatTagPattern); logcatAppender.setEncoder(logcatPattern); logcatAppender.start(); final ch.qos.logback.classic.Logger log = context.getLogger(Logger.ROOT_LOGGER_NAME); log.addAppender(fileAppender); log.addAppender(logcatAppender); log.setLevel(Level.INFO); }
private void configureFileLogger() { final File logFile = new File(logsDir, DEFAULT_LOG_FILENAME); final RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<>(); fileAppender.setContext((LoggerContext) LoggerFactory.getILoggerFactory()); fileAppender.setName(LOG_APPENDER); fileAppender.setFile(logFile.getAbsolutePath()); fileAppender.setEncoder(createEncoder()); fileAppender.setRollingPolicy(createRollingPolicy(fileAppender)); fileAppender.setTriggeringPolicy(createTriggeringPolicy()); fileAppender.start(); final ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("ROOT"); logger.addAppender(fileAppender); StatusPrinter.print((LoggerContext) LoggerFactory.getILoggerFactory()); }
private static void configureLogging(final Level consoleLevel, final Level fileLevel) { final LoggerContext loggerContext = LOG.getLoggerContext(); loggerContext.reset(); // set root log to the most permissive filter (affects performance big time (JAVACLI-90)) final int lowestLogLevel = Math.min(consoleLevel.toInt(), fileLevel.toInt()); LOG.setLevel(Level.toLevel(lowestLogLevel)); if (!consoleLevel.equals(Level.OFF)) { // create and add console appender final PatternLayoutEncoder consoleEncoder = new PatternLayoutEncoder(); consoleEncoder.setContext(loggerContext); consoleEncoder.setPattern(LOG_FORMAT_PATTERN); consoleEncoder.start(); final ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<ILoggingEvent>(); consoleAppender.setContext(loggerContext); consoleAppender.setName("STDOUT"); consoleAppender.setEncoder(consoleEncoder); final ThresholdFilter consoleFilter = new ThresholdFilter(); consoleFilter.setLevel(consoleLevel.levelStr); consoleFilter.setName(consoleLevel.levelStr); consoleFilter.start(); consoleAppender.addFilter(consoleFilter); consoleAppender.start(); LOG.addAppender(consoleAppender); } if (!fileLevel.equals(Level.OFF)) { // create file appender only if needed. // if done in the xml, it will create an empty file final RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>(); final FixedWindowRollingPolicy sizeBasedRollingPolicy = new FixedWindowRollingPolicy(); final SizeBasedTriggeringPolicy<Object> sizeBasedTriggeringPolicy = new SizeBasedTriggeringPolicy<Object>(); fileAppender.setContext(loggerContext); sizeBasedTriggeringPolicy.setContext(loggerContext); sizeBasedRollingPolicy.setContext(loggerContext); fileAppender.setRollingPolicy(sizeBasedRollingPolicy); sizeBasedRollingPolicy.setParent(fileAppender); sizeBasedRollingPolicy.setMinIndex(0); sizeBasedRollingPolicy.setMaxIndex(99); final Path logFilePath = FileSystems.getDefault().getPath(LOG_DIR, LOG_FILE_NAME); fileAppender.setFile(logFilePath.toString()); sizeBasedRollingPolicy.setFileNamePattern(LOG_DIR + LOG_ARCHIVE_FILE_PATTERN); sizeBasedRollingPolicy.start(); sizeBasedTriggeringPolicy.setMaxFileSize("10MB"); sizeBasedTriggeringPolicy.start(); final PatternLayoutEncoder fileEncoder = new PatternLayoutEncoder(); fileEncoder.setContext(loggerContext); fileEncoder.setPattern(LOG_FORMAT_PATTERN); fileEncoder.start(); fileAppender.setTriggeringPolicy((TriggeringPolicy) sizeBasedTriggeringPolicy); fileAppender.setRollingPolicy(sizeBasedRollingPolicy); fileAppender.setEncoder(fileEncoder); fileAppender.setName("LOGFILE"); sizeBasedRollingPolicy.start(); final ThresholdFilter fileFilter = new ThresholdFilter(); fileFilter.setLevel(fileLevel.levelStr); fileFilter.setName(fileLevel.levelStr); fileFilter.start(); fileAppender.addFilter(fileFilter); LOG.addAppender((Appender) fileAppender); fileAppender.start(); } }