A good program can solve problems from Log.
A good Log system is the key to a program.
This article introduces Logback-Android, a library that can write Log to File, to database, to Email, and to network.
Of course, the most basic Logcat window is perfect for output, which is the most basic;
Open source address: https://github.com/tony19/logback-android
slf4j address: http://www.slf4j.org/
slf4j API documentation: http://www.slf4j.org/api/
if you need to know more Many, please use Baidu (Google);
logback official WIKI: https://github.com/tony19/logback-android/wiki
logback API documentation: http://tony19.github.io/logback-android/doc/1.1.1-4/
Recommended reading: http://blog.csdn.net/haidage/article/details/6794529
1: Add the following dependencies to build.gradle in Module
dependencies {
...
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'
}
2: Create a logback.xml file in the following location, this path and file name are fixed by default
3: Configure logback file
<!--debug属性用来决定是否打印logback的日志信息-->
<configuration debug='false'>
<!--声明一个属性,用来指定log文件存放的路径-->
<property name="LOG_DIR" value="/sdcard/rlog"/>
<!--声明一个时间戳-->
<timestamp datePattern="yyyyMMdd" key="today"/>
<!--用于在控制台输出的Appender-->
<appender name="LOGCAT" class="ch.qos.logback.classic.android.LogcatAppender">
<encoder>
<pattern>%-5relative [%thread][%file:%M:%line] - %msg%n</pattern>
</encoder>
</appender>
<!--声明一个FileAppender-->
<appender name="BASE_FILE" class="ch.qos.logback.core.FileAppender">
<!--初始化的时候不创建文件,在第一次使用的时候创建文件-->
<lazy>true</lazy>
<!--log追加到文件,否则覆盖文件-->
<append>true</append>
<!--用来保存log的文件全路径-->
<file>${LOG_DIR}/base.log</file>
<!--输出log的格式-->
<encoder>
<!--<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} [%file:%line] - %msg%n</pattern>-->
<pattern>%date [%thread] %-5level %logger{36} [%file:%line] - %msg%n</pattern>
</encoder>
</appender>
<!--声明一个RollingFileAppender-->
<appender name="BASE_ROLL_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/base.roll.${today}.log</file>
<append>true</append>
<encoder>
<pattern>%date %-5relative [%thread] %-5level %logger{36} [%file:%M:%line] - %msg%n
</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_DIR}/base.roll.%d{yyyy-MM-dd}.log</fileNamePattern>
<!--最大保存7天的日志-->
<maxHistory>7</maxHistory>
</rollingPolicy>
<!--文件大于10mb,切换文件-->
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>10MB</maxFileSize>
</triggeringPolicy>
</appender>
<!--指定logtest采用BASE_ROLL_FILE声明的RollingFileAppender输出日志-->
<logger name="logtest">
<appender-ref ref="BASE_ROLL_FILE"/>
</logger>
<!-- Write INFO (and higher-level) messages to the log file -->
<root level="TRACE">
<appender-ref ref="LOGCAT"/>
</root>
<!--支持的level-->
<!--TRACE-->
<!--DEBUG-->
<!--INFO-->
<!--WARN-->
<!--ERROR-->
<!--<pattern>
%d{yyyy-MM-dd HH:mm:ss} [%level] - %msg%n
Logger: %logger
Class: %class
File: %file
Caller: %caller
Line: %line
Message: %m
Method: %M
Relative: %relative
Thread: %thread
Exception: %ex
xException: %xEx
nopException: %nopex
rException: %rEx
Marker: %marker
%n
</pattern>-->
<!--引用其他位置的配置信息-->
<!--<includes>-->
<!--<include file="/sdcard/foo.xml"/>-->
<!--<include resource="assets/config/test.xml"/>-->
<!--<include resource="AndroidManifest.xml"/>-->
<!--<include file="/sdcard/logback/logback-test.xml"/>-->
<!--<include file="/sdcard/logback/logback.xml"/>-->
<!--<include resource="AndroidManifest.xml"/>-->
<!--<include resource="assets/logback-test.xml"/>-->
<!--<include resource="assets/logback.xml"/>-->
<!--</includes>-->
</configuration>
There are other appenders available, detailed instructions for moving:
http://logback.qos.ch/manual/appenders.html
4: code test
Log information saved in the file:
Easter eggs: color settings of the console
Friendly reminder, you may need to add the following permissions:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Project address: https://github.com/angcyo/LogbackDemo