Search in sources :

Example 16 with RollingFileAppender

use of ch.qos.logback.core.rolling.RollingFileAppender in project bitsquare by bitsquare.

the class Log method setup.

public static void setup(String fileName) {
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    RollingFileAppender appender = new RollingFileAppender();
    appender.setContext(loggerContext);
    appender.setFile(fileName + ".log");
    FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy();
    rollingPolicy.setContext(loggerContext);
    rollingPolicy.setParent(appender);
    rollingPolicy.setFileNamePattern(fileName + "_%i.log");
    rollingPolicy.setMinIndex(1);
    rollingPolicy.setMaxIndex(10);
    rollingPolicy.start();
    triggeringPolicy = new SizeBasedTriggeringPolicy();
    triggeringPolicy.setMaxFileSize(FileSize.valueOf("10MB"));
    triggeringPolicy.start();
    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setContext(loggerContext);
    encoder.setPattern("%d{MMM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{15}: %msg %xEx%n");
    encoder.start();
    appender.setEncoder(encoder);
    appender.setRollingPolicy(rollingPolicy);
    appender.setTriggeringPolicy(triggeringPolicy);
    appender.start();
    logbackLogger = loggerContext.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
    logbackLogger.addAppender(appender);
    logbackLogger.setLevel(Level.INFO);
// log errors in separate file
// not working as expected still.... damn logback...
/* FileAppender errorAppender = new FileAppender();
        errorAppender.setEncoder(encoder);
        errorAppender.setName("Error");
        errorAppender.setContext(loggerContext);
        errorAppender.setFile(fileName + "_error.log");
        LevelFilter levelFilter = new LevelFilter();
        levelFilter.setLevel(Level.ERROR);
        levelFilter.setOnMatch(FilterReply.ACCEPT);
        levelFilter.setOnMismatch(FilterReply.DENY);
        levelFilter.start();
        errorAppender.addFilter(levelFilter);
        errorAppender.start();
        logbackLogger.addAppender(errorAppender);*/
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) FixedWindowRollingPolicy(ch.qos.logback.core.rolling.FixedWindowRollingPolicy) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) SizeBasedTriggeringPolicy(ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy) LoggerContext(ch.qos.logback.classic.LoggerContext)

Example 17 with RollingFileAppender

use of ch.qos.logback.core.rolling.RollingFileAppender in project cdap-ingest by caskdata.

the class FileTailerMetricsProcessor method initAppender.

/**
   * Initializes the file appender.
   *
   * @param path the path to the directory for the metrics file
   * @param fileName the name of the metrics file
   * @return the initialized file appender
   */
private RollingFileAppender initAppender(String path, String fileName) {
    LOG.debug("Starting initialize rolling file appender");
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    RollingFileAppender fileAppender = new RollingFileAppender();
    fileAppender.setContext(loggerContext);
    fileAppender.setFile(path + "/" + fileName);
    fileAppender.setAppend(true);
    TimeBasedRollingPolicy rollingPolicy = new TimeBasedRollingPolicy();
    rollingPolicy.setContext(loggerContext);
    rollingPolicy.setParent(fileAppender);
    rollingPolicy.setFileNamePattern(path + "/" + fileName + ".%d");
    rollingPolicy.start();
    fileAppender.setRollingPolicy(rollingPolicy);
    PatternLayoutEncoder layoutEncoder = new PatternLayoutEncoder();
    layoutEncoder.setContext(loggerContext);
    layoutEncoder.setPattern("%msg%n");
    layoutEncoder.start();
    fileAppender.setEncoder(layoutEncoder);
    fileAppender.start();
    return fileAppender;
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) LoggerContext(ch.qos.logback.classic.LoggerContext) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy)

Example 18 with RollingFileAppender

use of ch.qos.logback.core.rolling.RollingFileAppender in project cdap-ingest by caskdata.

the class TailerLogUtils method getTimeLogger.

public static Logger getTimeLogger(String file) {
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    RollingFileAppender fileAppender = new RollingFileAppender();
    fileAppender.setContext(loggerContext);
    fileAppender.setFile(file);
    fileAppender.setAppend(true);
    TimeBasedRollingPolicy rollingPolicy = new TimeBasedRollingPolicy();
    rollingPolicy.setContext(loggerContext);
    rollingPolicy.setParent(fileAppender);
    rollingPolicy.setFileNamePattern(file + "%d{yyyy-MM-dd_HH-mm}");
    rollingPolicy.start();
    fileAppender.setRollingPolicy(rollingPolicy);
    PatternLayoutEncoder layoutEncoder = new PatternLayoutEncoder();
    layoutEncoder.setContext(loggerContext);
    layoutEncoder.setPattern("[%d  %-5p %c{1}] %msg%n");
    layoutEncoder.start();
    fileAppender.setEncoder(layoutEncoder);
    fileAppender.start();
    // configures the root logger
    Logger rootLogger = loggerContext.getLogger(BaseTailerTest.class.getName() + "time");
    rootLogger.setLevel(Level.DEBUG);
    rootLogger.addAppender(fileAppender);
    return rootLogger;
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) Logger(ch.qos.logback.classic.Logger) LoggerContext(ch.qos.logback.classic.LoggerContext) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy)

Example 19 with RollingFileAppender

use of ch.qos.logback.core.rolling.RollingFileAppender in project coffeenet-starter by coffeenet.

the class CoffeeNetLoggingAutoConfiguration method rollingFileAppender.

private RollingFileAppender<ILoggingEvent> rollingFileAppender(LoggerContext loggerContext) {
    RollingFileAppender<ILoggingEvent> rfAppender = new RollingFileAppender<>();
    rfAppender.setName(COFFEENET_FILE_APPENDER);
    rfAppender.setContext(loggerContext);
    rfAppender.setFile(coffeeNetLoggingFileProperties.getFile());
    // TimeBasedRollingPolicy
    TimeBasedRollingPolicy rollingPolicy = new TimeBasedRollingPolicy();
    rollingPolicy.setContext(loggerContext);
    rollingPolicy.setParent(rfAppender);
    rollingPolicy.setFileNamePattern(coffeeNetLoggingFileProperties.getFileNamePattern());
    rollingPolicy.setMaxHistory(coffeeNetLoggingFileProperties.getMaxHistory());
    rollingPolicy.start();
    rfAppender.setRollingPolicy(rollingPolicy);
    // PatternLayoutEncoder
    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setContext(loggerContext);
    encoder.setPattern(coffeeNetLoggingFileProperties.getPattern());
    encoder.start();
    rfAppender.setEncoder(encoder);
    rfAppender.start();
    return rfAppender;
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy)

Aggregations

RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)19 LoggerContext (ch.qos.logback.classic.LoggerContext)11 TimeBasedRollingPolicy (ch.qos.logback.core.rolling.TimeBasedRollingPolicy)10 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)9 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)6 Test (org.junit.Test)6 Logger (ch.qos.logback.classic.Logger)5 FixedWindowRollingPolicy (ch.qos.logback.core.rolling.FixedWindowRollingPolicy)4 SizeBasedTriggeringPolicy (ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy)4 FileAppender (ch.qos.logback.core.FileAppender)3 FileSize (ch.qos.logback.core.util.FileSize)3 RollingPolicy (ch.qos.logback.core.rolling.RollingPolicy)2 Field (java.lang.reflect.Field)2 Appender (ch.qos.logback.core.Appender)1 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)1 SizeAndTimeBasedFNATP (ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP)1 GelfAppender (de.appelgriepsch.logback.GelfAppender)1 File (java.io.File)1 FilenameFilter (java.io.FilenameFilter)1 SimpleDateFormat (java.text.SimpleDateFormat)1