Search in sources :

Example 1 with LogcatAppender

use of ch.qos.logback.classic.android.LogcatAppender in project bitcoin-wallet by bitcoin-wallet.

the class Logging method init.

public static synchronized void init(final File filesDir) {
    if (logFile != null)
        return;
    // create log dir
    final File logDir = new File(filesDir, LOG_DIRECTORY_NAME);
    logDir.mkdir();
    logFile = new File(logDir, LOG_FILE_NAME);
    final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    final PatternLayoutEncoder filePattern = new PatternLayoutEncoder();
    filePattern.setContext(context);
    filePattern.setPattern("%d{HH:mm:ss,UTC} [%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() + "/" + LOG_ROLLING_FILE_NAME_PATTERN);
    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);
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) LogcatAppender(ch.qos.logback.classic.android.LogcatAppender) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) File(java.io.File) LoggerContext(ch.qos.logback.classic.LoggerContext) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy)

Aggregations

LoggerContext (ch.qos.logback.classic.LoggerContext)1 LogcatAppender (ch.qos.logback.classic.android.LogcatAppender)1 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)1 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)1 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)1 TimeBasedRollingPolicy (ch.qos.logback.core.rolling.TimeBasedRollingPolicy)1 File (java.io.File)1