Logback 继承自 log4j。
Logback 的架构非常的通用,适用不同的使用场景。Logback 被分成三个不同的模块:logback-core,logback-classic,logback-access。
logback-core 是其它两个模块的基础。logback-classic 模块可以看作是 log4j 的一个优化版本,它天然的支持 SLF4J,所以你可以随意的从其它日志框架(例如:log4j 或者 java.util.logging)切回到 logack。
这里举例的是工程使用gradle构建,引入相关依赖
in build.gradle 添加如下依赖引用:
implementation 'org.slf4j:slf4j-api:1.7.25'
implementation 'com.github.tony19:logback-android:2.0.0'
或者引入jar文件放到libs目录下(略)。
testFile.log
true
true
%-4relative [%thread] %-5level %logger{35} - %msg%n
也可以用java代码配置:
private String PREFIX = "hgltest";
static String LOG_DIR;
static {
File sdfile = Environment.getExternalStorageDirectory();
LOG_DIR = sdfile.getAbsolutePath() + "/hgl_log";
}
//配置logback appender
private void configureLogbackDirectly() {
// reset the default context (which may already have been initialized)
// since we want to reconfigure it
LoggerContext lc = (LoggerContext)LoggerFactory.getILoggerFactory();
lc.stop();
// setup FileAppender
PatternLayoutEncoder encoder1 = new PatternLayoutEncoder();
encoder1.setContext(lc);
encoder1.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
encoder1.start();
RollingFileAppender fileAppender = new RollingFileAppender<>();
fileAppender.setContext(lc);
String outputPath = LOG_DIR + "/"+PREFIX+".log";
Log.d("hgl_debug", "outputpath = " + outputPath);
fileAppender.setFile(outputPath);
fileAppender.setEncoder(encoder1);
//设置rolling policy
TimeBasedRollingPolicy rollingPolicy = new TimeBasedRollingPolicy();
rollingPolicy.setFileNamePattern(LOG_DIR + "/" + PREFIX + "_%d{yyyyMMdd_HH-mm}.log"); //rollover action based on filepattern
rollingPolicy.setMaxHistory(60); //设置最大的存档文件个数
rollingPolicy.setParent(fileAppender);
rollingPolicy.setContext(lc);
rollingPolicy.start();
fileAppender.setRollingPolicy(rollingPolicy);
fileAppender.start();
// setup LogcatAppender
PatternLayoutEncoder encoder2 = new PatternLayoutEncoder();
encoder2.setContext(lc);
encoder2.setPattern("[%thread] %msg%n");
encoder2.start();
LogcatAppender logcatAppender = new LogcatAppender();
logcatAppender.setContext(lc);
logcatAppender.setEncoder(encoder2);
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.addAppender(fileAppender);
root.addAppender(logcatAppender);
}
logger = LoggerFactory.getLogger(this.getClass());
logger.info("in ActivityB_onCreate() sdfile = " + sdfile.getAbsolutePath());
Appender用来指定用户采用那种方式进行日志记录、输出,常用的方式如控制台、文件输出。一下列举了几个使用频率较高的appender,均为logback提供,大家在使用时只需要做好配置工作即可。
3.1 ConSoleAppender
3.2 FileAppender
在指定使用FileAppender时,可以通过配置timestamp标签实现在每次应用启动创建一个包含时间戳信息的log file,
log-${bySecond}.txt
%logger{35} - %msg%n
3.3 RollingFileAppender
可以实现滚动生成日志文件的需要,该appender需要配合RollingPolicy和TriggeringPolicy来实现日志文件的滚动生成,两这的作用分别如下:
RollingPolicy: 决定文件以哪种形式命名和移动(旧文件的删除) 常用的RollingPolicy包括
TriggeringPolicy: 负责指示appender什么时候执行rollover。
3.4 SocketAppender & SSL SocketAppender
略
3.5 SMTPAppender
该appender同样需要设置trigger,配合使用的triggeringPolicy可以是
NOTIFY_ADMIN
TRANSACTION_FAILURE
${smtpHost}
${to}
${from}
logback documentation Five stars recommend !
logback快速上手指导
logback中文手册 http://www.logback.cn/