為什么使用 slf4j + logback
logbak定位于log4j的替代者,logback同樣支持slf4j,方便被替換。在Android平臺(tái)上氮昧,我在使用log4中遇到tag混亂的問題。相比log4j浦楣,logback-android的jar更小巧袖肥。
下載jar包
前往 https://github.com/tony19/logback-android ,下載獲得 logback-android-1.1.1-4.jar slf4j-api-1.7.6.jar
導(dǎo)入到工程中
將上面的兩個(gè)jar文件放入項(xiàng)目中的libs中振劳,并添加項(xiàng)目引用椎组。
配置日志文件的位置
注意下面代碼中的 configureLogbackDirectly 方法,它接收一個(gè)文件路徑參數(shù),指示存放日志文件的目錄历恐,還有一個(gè)是生成的日志文件的前綴寸癌。我下面配置了“每天生成一個(gè)新的日志文件”。
package vir56k.logdemo;
import android.os.Environment;
import org.slf4j.LoggerFactory;
import java.io.File;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
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.rolling.RollingFileAppender;
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;
/**
* Created by zhangyunfei on 15/9/21.
*/
public class LogConfigurator {
public static void confifure() {
final String LOG_DIR = Environment.getExternalStorageDirectory() + File.separator + "logback";
final String PREFIX = "log";
configureLogbackDirectly(LOG_DIR, PREFIX);
}
private static void configureLogbackDirectly(String log_dir, String filePrefix) {
// reset the default context (which may already have been initialized)
// since we want to reconfigure it
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
context.reset();
RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<ILoggingEvent>();
rollingFileAppender.setAppend(true);
rollingFileAppender.setContext(context);
// OPTIONAL: Set an active log file (separate from the rollover files).
// If rollingPolicy.fileNamePattern already set, you don't need this.
//rollingFileAppender.setFile(LOG_DIR + "/log.txt");
TimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = new TimeBasedRollingPolicy<ILoggingEvent>();
rollingPolicy.setFileNamePattern(log_dir + "/" + filePrefix + "_%d{yyyyMMdd}.txt");
rollingPolicy.setMaxHistory(7);
rollingPolicy.setParent(rollingFileAppender); // parent and context required!
rollingPolicy.setContext(context);
rollingPolicy.start();
rollingFileAppender.setRollingPolicy(rollingPolicy);
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setPattern("%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
encoder.setContext(context);
encoder.start();
rollingFileAppender.setEncoder(encoder);
rollingFileAppender.start();
LogcatAppender logcatAppender = new LogcatAppender();
logcatAppender.setContext(context);
logcatAppender.setEncoder(encoder);
logcatAppender.setName("logcat1");
logcatAppender.start();
// add the newly created appenders to the root logger;
// qualify Logger to disambiguate from org.slf4j.Logger
ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
root.setLevel(Level.TRACE);
root.addAppender(rollingFileAppender);
root.addAppender(logcatAppender);
// print any status messages (warnings, etc) encountered in logback config
//StatusPrinter.print(context);
}
}
使用方法
先聲明和創(chuàng)建實(shí)例
private static final Logger logger = LoggerFactory.getLogger(MainActivity.class);
寫入日志
logger.debug("debug Some log message. Details: {}", "debug 輸出");
logger.info("info Some log message. Details: {}", "debug 輸出");
logger.error("error Some log message. Details: {}", "debug 輸出");
代碼混淆注意
請(qǐng)?jiān)谀愕幕煜O(shè)置里加入下面這些:
-keep class ch.qos.** { *; }
-keep class org.slf4j.** { *; }
-keepattributes *Annotation*
示例代碼下載地址
https://github.com/vir56k/demo/tree/master/logBackDemo
參考資料:
https://github.com/tony19/logback-android
https://github.com/tony19/logback-android/wiki/Appender-Notes