# XLog **Repository Path**: XShawnNotes/XLog ## Basic Information - **Project Name**: XLog - **Description**: 基于apkfuns.logutil整理优化的日志库。 请使用xlog2分支,其它分支已废弃! - **Primary Language**: Android - **License**: Not specified - **Default Branch**: xlog2 - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 0 - **Created**: 2020-06-03 - **Last Updated**: 2025-05-26 ## Categories & Tags **Categories**: android-modules **Tags**: log ## README # 分支说明 V2.X 的aar包均基于xlog2分支,其它分支均已废弃! # 初始化 1. logcat初始化(可选) ```java XLog.logcatConfig() .setEnable(enable) // 允许打印logcat .setTag("XShawn") // 使用自定义TAG .setLevel(XLog.DEBUG) // logcat 输出等级,小于此等级的log不会打印 .setBorderEnable(true); // logcat显示边界 ``` 2. logfile初始化 logfile初始化主要是为了设置文件输出引擎、文件名、文件路径等关键参数。 ```java XLog.logfileConfig() .setEnable(true) // 允许输出到文件 .setLevel(XLog.DEBUG) // 输出等级,小于此等级的log不会输出 .setStackTraceEnable(true) // 输出Caller信息 .setLogfileName(fileName) // 文件名 .setLogfileDir(fileDir) // 文件路径 .setEngine(new LogfileEngineImpl(context)); // 输出引擎 // 3. 打印日志 // 3.1 打印格式化信息 XLog.d("启动NFC数据读取界面,当前进程:%s", "main"); // 3.2 打印各种对象 XLog.d(intent); XLog.e(new String[]{"1", "2", "3"}) ``` # Logcat 输出示例 有边界效果(默认关闭) ```shell E/XLog: ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ E/XLog: ┃ MainActivity2.onCreate(MainActivity2.kt:40) E/XLog: ┣━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ E/XLog: ┃ android.content.Intent{ E/XLog: ┃ Action = Action E/XLog: ┃ DataString = asdasdasd E/XLog: ┃ Type = uri E/XLog: ┃ ComponentInfo = {x.shawn.core.test/x.shawn.core.test.MainActivity2} E/XLog: ┃ Identifier = Identifier E/XLog: ┃ Flags = (1081344)|FLAG_ACTIVITY_CLEAR_TASK|FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY E/XLog: ┃ Extras = android.os.Bundle(4){ E/XLog: ┃ age = 30 E/XLog: ┃ data = [1, 2, 3, 4] E/XLog: ┃ name = XShawn E/XLog: ┃ message = android.os.Message{ E/XLog: ┃ what = 100 E/XLog: ┃ when = 1668482191081 E/XLog: ┃ arg1 = 111 E/XLog: ┃ arg2 = 222 E/XLog: ┃ obj = java.lang.ref.SoftReference E/XLog: ┃ data = android.os.Bundle(0){} E/XLog: ┃ target = Handler (android.os.Handler) {54ad477} E/XLog: ┃ isAsynchronous = false E/XLog: ┃ } E/XLog: ┃ } E/XLog: ┃ } E/XLog: ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ``` # Logfile 输出示例 有Caller信息(默认关闭显示,即下方 `[MainActivity2.onCreate(MainActivity2.kt:40)]` 部分) ```shell [2022.11.15 11:15:19:584][main][E:XLog][MainActivity2.onCreate(MainActivity2.kt:40)] android.content.Intent{ Action = Action DataString = asdasdasd Type = uri ComponentInfo = {x.shawn.core.test/x.shawn.core.test.MainActivity2} Identifier = Identifier Flags = (1081344)|FLAG_ACTIVITY_CLEAR_TASK|FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY Extras = android.os.Bundle(4){ age = 30 data = [1, 2, 3, 4] name = XShawn message = android.os.Message{ what = 100 when = 1668482119466 arg1 = 111 arg2 = 222 obj = java.lang.ref.SoftReference data = android.os.Bundle(0){} target = Handler (android.os.Handler) {c8e4113} isAsynchronous = false } } } ``` # 二次封装 如需经常切换 Log 工具,或者需要降低 XLog 与其它代码的耦合,建议再封装一次。 封装时注意修改 method 偏移量,以免 StackTrace 信息输出错误: ```java XLog.logcatConfig() .setEnable(enable) .setMethodOffset(1) // 校准显示的方法名 .setBorderEnable(false); ``` # 致谢 此 library 基于如下开源项目衍生而来: * [com.apkfuns.logutils](https://github.com/pengwei1024/LogUtils) * com.apkfuns.log2file * me.pqpo.librarylog4a 在此对 [pengwei1024](https://github.com/pengwei1024) 大佬表示感谢! # License ```text Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ``` # 博文公号 | 平台 | 地址 | | :------: | :----------------------------------------------------------: | | 语雀 | [www.yuque.com/xshawn](https://www.yuque.com/xshawn) | | 微信公号 | |