Last active
May 2, 2017 10:47
-
-
Save melnikovdv/306f4ef5892e41498d35252c89d7a6db to your computer and use it in GitHub Desktop.
Android logging with slf4j and logback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
compile "org.slf4j:slf4j-api:1.7.21" | |
compile "com.github.tony19:logback-android-core:1.1.1-5" | |
compile("com.github.tony19:logback-android-classic:1.1.1-5") { | |
// workaround issue #73 | |
exclude group: 'com.google.android', module: 'android' | |
} | |
// ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.mlayer.utils; | |
import android.support.annotation.NonNull; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import ch.qos.logback.classic.AsyncAppender; | |
import ch.qos.logback.classic.Level; | |
import ch.qos.logback.classic.LoggerContext; | |
import ch.qos.logback.classic.android.LogcatAppender; | |
import ch.qos.logback.classic.encoder.PatternLayoutEncoder; | |
import ch.qos.logback.classic.spi.ILoggingEvent; | |
import ch.qos.logback.core.encoder.Encoder; | |
import ch.qos.logback.core.rolling.FixedWindowRollingPolicy; | |
import ch.qos.logback.core.rolling.RollingFileAppender; | |
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy; | |
import ch.qos.logback.core.rolling.TriggeringPolicy; | |
/** | |
* Log wrapper. Built around slf4j + logback | |
*/ | |
public final class Log { | |
public static final String TAG = Log.class.getName(); | |
public static final String LOG_FILE_PRIMARY = Files.logsPath() + "/log.txt"; | |
public static final String LOG_FILE_SECONDARY = Files.logsPath() + "/log.1.txt"; | |
private static final String LOG_FILE_HISTORY_PATTERN = Files.logsPath() + "/log.%i.txt"; | |
private static final String LOG_MESSAGE_PATTERN = "%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n"; | |
private static final String LOG_SIZE_ROLLING_TRIGGER_DEBUG = "1MB"; | |
private static final String LOG_SIZE_ROLLING_TRIGGER_PROD = "80KB"; | |
public static void configureLogbackDirectly() { | |
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); | |
lc.reset(); | |
RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<>(); | |
rollingFileAppender.setContext(lc); | |
rollingFileAppender.setFile(LOG_FILE_PRIMARY); | |
rollingFileAppender.setEncoder(createEncoder(lc)); | |
rollingFileAppender.setTriggeringPolicy(getTriggeringPolicy()); | |
rollingFileAppender.setRollingPolicy(getRollingPolicy(lc, rollingFileAppender)); | |
rollingFileAppender.start(); | |
AsyncAppender asyncAppender = new AsyncAppender(); | |
asyncAppender.setContext(lc); | |
asyncAppender.addAppender(rollingFileAppender); | |
asyncAppender.start(); | |
ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); | |
root.setLevel(Level.DEBUG); | |
root.addAppender(asyncAppender); | |
if (Device.isDebug()) { | |
PatternLayoutEncoder logcatEncoder = new PatternLayoutEncoder(); | |
logcatEncoder.setContext(lc); | |
logcatEncoder.setPattern("%msg"); | |
logcatEncoder.start(); | |
LogcatAppender logcatAppender = new LogcatAppender(); | |
logcatAppender.setContext(lc); | |
logcatAppender.setEncoder(logcatEncoder); | |
logcatAppender.start(); | |
root.addAppender(logcatAppender); | |
} | |
Log.d(TAG, "configureLogbackDirectly: finished"); | |
} | |
@NonNull | |
private static TriggeringPolicy<ILoggingEvent> getTriggeringPolicy() { | |
String size = Device.isDebug() || Device.isBeta() ? LOG_SIZE_ROLLING_TRIGGER_DEBUG : LOG_SIZE_ROLLING_TRIGGER_PROD; | |
return new SizeBasedTriggeringPolicy<>(size); | |
} | |
@NonNull | |
private static FixedWindowRollingPolicy getRollingPolicy(LoggerContext lc, RollingFileAppender<ILoggingEvent> rollingFileAppender) { | |
FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy(); | |
rollingPolicy.setContext(lc); | |
rollingPolicy.setParent(rollingFileAppender); | |
rollingPolicy.setFileNamePattern(LOG_FILE_HISTORY_PATTERN); | |
rollingPolicy.setMinIndex(1); | |
rollingPolicy.setMaxIndex(1); | |
rollingPolicy.start(); | |
return rollingPolicy; | |
} | |
@NonNull | |
private static Encoder<ILoggingEvent> createEncoder(LoggerContext lc) { | |
PatternLayoutEncoder encoder = new PatternLayoutEncoder(); | |
encoder.setContext(lc); | |
encoder.setPattern(LOG_MESSAGE_PATTERN); | |
encoder.start(); | |
return encoder; | |
} | |
private static boolean needLog() { | |
// return Device.isDebug() || Device.isBeta() || Device.isProd(); | |
return true; | |
} | |
public static void d(String tag, String msg) { | |
if (needLog()) { | |
LoggerFactory.getLogger(tag).debug(msg); | |
} | |
} | |
public static void e(String tag, String msg) { | |
if (needLog()) { | |
LoggerFactory.getLogger(tag).error(msg); | |
} | |
} | |
public static void i(String tag, String msg) { | |
if (needLog()) { | |
LoggerFactory.getLogger(tag).info(msg); | |
} | |
} | |
public static void v(String tag, String msg) { | |
if (needLog()) { | |
LoggerFactory.getLogger(tag).trace(msg); | |
} | |
} | |
public static void w(String tag, String msg) { | |
if (needLog()) { | |
LoggerFactory.getLogger(tag).warn(msg); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I am using the same configuration. I am getting file logs too. But, not all logs are being appended to file while they appears in logcat.
What can be the reason for this?