0 Star 2 Fork 0

灰灰手记 / XLog

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

分支说明

V2.X 的aar包均基于xlog2分支,其它分支均已废弃!

初始化

  1. logcat初始化(可选)
XLog.logcatConfig()
.setEnable(enable)          // 允许打印logcat
.setTag("XShawn")           // 使用自定义TAG
.setLevel(XLog.DEBUG)       // logcat 输出等级,小于此等级的log不会打印
.setBorderEnable(true);     // logcat显示边界 
  1. logfile初始化

logfile初始化主要是为了设置文件输出引擎、文件名、文件路径等关键参数。

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 输出示例

有边界效果(默认关闭)

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<x.shawn.core.test.ABC{
E/XLog: ┃                 abc = abcabc
E/XLog: ┃                 inner = x.shawn.core.test.ABC$Inner{
E/XLog: ┃                     a = x.shawn.core.test.A{
E/XLog: ┃                         a = aaa
E/XLog: ┃                         map = {k1={}, k2=22222, k3=v3}	// 超出递归层数,直接toString的效果
E/XLog: ┃                         set = []
E/XLog: ┃                     }
E/XLog: ┃                     ab = x.shawn.core.test.AB{
E/XLog: ┃                         ab = ababab
E/XLog: ┃                     }
E/XLog: ┃                     iabc = iabc
E/XLog: ┃                 }
E/XLog: ┃             }>
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)] 部分)

[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<x.shawn.core.test.ABC{
                abc = abcabc
                inner = x.shawn.core.test.ABC$Inner{
                    a = x.shawn.core.test.A{
                        a = aaa
                        map = {k1={}, k2=22222, k3=v3}
                        set = []
                    }
                    ab = x.shawn.core.test.AB{
                        ab = ababab
                    }
                    iabc = iabc
                }
            }>
            data = android.os.Bundle(0){}
            target = Handler (android.os.Handler) {c8e4113}
            isAsynchronous = false
        }
    }
}

二次封装

如需经常切换 Log 工具,或者需要降低 XLog 与其它代码的耦合,建议再封装一次。 封装时注意修改 method 偏移量,以免 StackTrace 信息输出错误:

XLog.logcatConfig()
    .setEnable(enable)
    .setMethodOffset(1)     // 校准显示的方法名
    .setBorderEnable(false);

致谢

此 library 基于如下开源项目衍生而来:

在此对 pengwei1024 大佬表示感谢!

License

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
微信公号

空文件

简介

基于apkfuns.logutil整理优化的日志库。 请使用xlog2分支,其它分支已废弃! 展开 收起
Android
取消

发行版 (5)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
Android
1
https://gitee.com/XShawnNotes/XLog.git
git@gitee.com:XShawnNotes/XLog.git
XShawnNotes
XLog
XLog
xlog2

搜索帮助