{"id":18565050,"url":"https://github.com/jenly1314/logx","last_synced_at":"2025-04-10T04:32:13.905Z","repository":{"id":236795897,"uuid":"793164368","full_name":"jenly1314/LogX","owner":"jenly1314","description":":triangular_flag_on_post: LogX 一个小而美的日志记录框架；好用不解释。","archived":false,"fork":false,"pushed_at":"2024-12-10T16:13:25.000Z","size":3908,"stargazers_count":17,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-24T16:21:11.522Z","etag":null,"topics":["android","kotlin","log","log-printer","logcat","logger","timber"],"latest_commit_sha":null,"homepage":"https://jenly1314.github.io/LogX/","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jenly1314.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-04-28T15:58:28.000Z","updated_at":"2025-03-05T11:25:02.000Z","dependencies_parsed_at":"2024-04-28T17:24:56.938Z","dependency_job_id":"224b3e4c-e8ca-494a-9d42-00b8f8f79c33","html_url":"https://github.com/jenly1314/LogX","commit_stats":null,"previous_names":["jenly1314/logx"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jenly1314%2FLogX","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jenly1314%2FLogX/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jenly1314%2FLogX/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jenly1314%2FLogX/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jenly1314","download_url":"https://codeload.github.com/jenly1314/LogX/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248157550,"owners_count":21057034,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","kotlin","log","log-printer","logcat","logger","timber"],"created_at":"2024-11-06T22:17:31.105Z","updated_at":"2025-04-10T04:32:13.898Z","avatar_url":"https://github.com/jenly1314.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LogX\n\n[![Download](https://img.shields.io/badge/download-App-blue.svg)](https://raw.githubusercontent.com/jenly1314/LogX/master/app/release/app-release.apk)\n[![MavenCentral](https://img.shields.io/maven-central/v/com.github.jenly1314/logx)](https://repo1.maven.org/maven2/com/github/jenly1314/logx)\n[![JitPack](https://jitpack.io/v/jenly1314/LogX.svg)](https://jitpack.io/#jenly1314/LogX)\n[![CircleCI](https://circleci.com/gh/jenly1314/LogX.svg?style=svg)](https://circleci.com/gh/jenly1314/LogX)\n[![API](https://img.shields.io/badge/API-21%2B-blue.svg?style=flat)](https://android-arsenal.com/api?level=21)\n[![License](https://img.shields.io/badge/license-Apche%202.0-blue.svg)](http://www.apache.org/licenses/LICENSE-2.0)\n\n**LogX** 一个小而美的日志记录框架；既有 **Timber** 的易用性与可扩展性，又具备 **Logger** 的日志格式美观性。\n\n\u003e写这个日志框架的主要原因是为了简化维护流程。在我个人的GitHub开源项目中，有一些需要使用日志功能的库。\n\u003e最初，我使用的是一个自维护日志工具类：LogUtils，当开源项目数量较少时，这种方法还比较有效。\n\u003e然而，随着开源项目数量的增加，我不得不频繁地复制和维护LogUtils，随着时间的推移，不同开源项目中的LogUtils可能会\n\u003e出现一些差异，这大大增加了维护的难度。因此，我开始考虑更加优雅的解决方案。在经过一段时间的思考和研究后，我决定\n\u003e结合平时使用的 [Timber](https://github.com/JakeWharton/timber) 和 [Logger](https://github.com/orhanobut/logger) 这\n\u003e两个成熟的开源库，取其精华，编写一个新的日志框架，即：**LogX** 。\n\n## 类图\n\n![Image](art/logx_uml.png)\n\n\u003e 从上面的类图可以明确的看出 **LogX** 内部之间的关系与结构。\n\n## 引入\n\n### Gradle:\n\n1. 在Project的 **build.gradle** 或 **setting.gradle** 中添加远程仓库\n\n    ```gradle\n    repositories {\n        //...\n        mavenCentral()\n    }\n    ```\n\n2. 在Module的 **build.gradle** 里面添加引入依赖项\n\n    ```gradle\n    implementation 'com.github.jenly1314:logx:1.1.0'\n    ```\n\n## 使用\n\n### 基本用法\n\n如果没有特殊的要求，直接使用即可。\n\n\u003e **LogX** 无需初始化，因为 **LogX** 的默认配置就是个人最推荐的配置。（这也是我写 **LogX** 的原因之一）\n\n主要的一些方法调用示例如下：\n\n```java\nLogX.v(\"verbose\");\nLogX.d(\"debug\");\nLogX.i(\"info\");\nLogX.w(\"warn\");\nLogX.e(\"error\");\nLogX.wtf(\"assert\");\n\n```\n\n占位符格式化示例如下:\n\n```java\nLogX.d(\"hello %s\", \"world\");\n```\n\n\u003e 当未指定tag时，内部会自动通过线程栈获取对应的简单类名来作为tag，上面的示例皆是如此；不过你也可以自己指定tag；\n\n指定tag示例如下：\n\n```java\n// 指定tag（指定的tag是一次性的）\nLogX.tag(\"MyTag\").d(\"debug\");\n```\n\n\u003e 看了上面的基本用法，相信你已经会使用 **LogX** 了；如果还想了解更多，你可以继续往下看高级用法。\n\n### 高级用法\n\n如果你有特别的需求，你可以自己按照喜好进行配置`DefaultLogger`或自定义实现`Logger`。\n\n```java\n\n// 参数1：是否显示线程信息； 参数1：显示多少方法行，；参数3：方法栈偏移量\nLogger logger = new DefaultLogger(true, 2, 0);\nLogX.setLogger(logger);\n\n```\n\n全局配置是否记录日志示例如下：\n\n```java\nLogX.setLogger(new DefaultLogger() {\n   @Override\n   protected boolean isLoggable(int priority, @Nullable String tag) {\n//       return super.isLoggable(priority, tag);\n        // 例如：只在开发模式才记录日志\n       return BuildConfig.DEBUG;\n   }\n});\n```\n\u003e **LogX** 默认配置就是在debug包下才开启日志记录的，release包默认是关闭日志记录的。\n\n使用`CompositeLogger`管理`Logger`示例如下：\n\n```java\nCompositeLogger compositeLogger = new CompositeLogger();\n// 添加任意的Logger实现即可；例如：DefaultLogger\ncompositeLogger.addLogger(new DefaultLogger());\nLogX.setLogger(compositeLogger);\n```\n\n\u003e 如以上内置的功能都不满足你的需求，你还可以自定义实现一个`Logger`。\n\n### 日志效果\n\n日志的默认输出格式如下：\n\n```\n ┌──────────────────────────────\n │ Thread information\n ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄\n │ Method stack history\n ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄\n │ Log message\n └──────────────────────────────\n```\n在Logcat中的实际效果如下：\n\n![Image](art/logx_output.png)\n\n### 关于`FormatUtils`（v1.1.0新增）\n\n格式化`json`和`xml`本不属于日志打印的范畴，但为了提升可读性和美观性，决定提供一个工具类`FormatUtils`来支持此功能。至于是否需要格式化，由开发者根据实际需求自行决定。\n\n#### 格式化json示例：\n```java\nString json = \"{\\\"key\\\": \\\"value\\\", \\\"array\\\":[\\\"item1\\\",\\\"item2\\\"]}\";\n// 打印格式化后的json\nLogX.d(FormatUtils.formatJson(json));\n\n```\n\n#### 格式化xml示例：\n```java\nString xml = \"\u003croot\u003e\u003ckey\u003evalue\u003c/key\u003e\u003carray\u003e\u003citem\u003eitem1\u003c/item\u003e\u003citem\u003eitem2\u003c/item\u003e\u003c/array\u003e\u003c/root\u003e\";\n// 打印格式化后的xml\nLogX.d(FormatUtils.formatXml(xml));\n```\n\n### 特别说明\n\n既然 **LogX** 的实现主要是参考了：**Timber** 和 **Logger**，那么二者的诸多优点，**LogX** 自然也是支持的。\n\n比如：如果你之前是集成使用的Timber，那么现在你也可以通过如下方式，将最终的日志输出转到 **LogX**，让日志输出格式更具美观性。\n```java\nTimber.plant(new Timber.Tree() {\n   @Override\n   protected void log(int priority, @Nullable String tag, @NonNull String message, @Nullable Throwable throwable) {\n//       if(tag != null) { // 这里的tag可要可不要\n//           LogX.tag(tag);\n//       }\n       // 在Timber目前层级没发生变化的情况下，方法栈偏移4 即可准确定位到原始Timber调用的代码所在行。（这里不需要throwable，因为message里面已经包含了）\n       LogX.offset(4).log(priority, message);\n   }\n});\n\n```\n\n\u003e 通过使用`LogX.offset(offset)`进行方法栈的偏移，就算多库混用也互不影响，都可以轻松的定位到日志具体的代码行。\n\n更多使用详情，请查看[app](app)中的源码使用示例或直接查看[API帮助文档](https://jenly1314.github.io/LogX/api/)\n\n\u003c!-- end --\u003e\n\n## 版本日志\n\n#### v1.1.0：2024-12-10\n* 新增格式化工具类：`FormatUtils`\n\n#### [查看更多版本日志](CHANGELOG.md)\n\n## 赞赏\n\n如果你喜欢LogX，或感觉LogX帮助到了你，可以点右上角“Star”支持一下，你的支持就是我的动力，谢谢 :smiley:\n\u003cp\u003e您也可以扫描下面的二维码，请作者喝杯咖啡 :coffee:\n\n\u003cdiv\u003e\n   \u003cimg src=\"https://jenly1314.github.io/image/page/rewardcode.png\"\u003e\n\u003c/div\u003e\n\n## 关于我\n\n| 我的博客                                                                                | GitHub                                                                                  | Gitee                                                                                 | CSDN                                                                                | 博客园                                                                           |\n|:------------------------------------------------------------------------------------|:----------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------|:------------------------------------------------------------------------------------|:------------------------------------------------------------------------------|\n| \u003ca title=\"我的博客\" href=\"https://jenly1314.github.io\" target=\"_blank\"\u003eJenly's Blog\u003c/a\u003e | \u003ca title=\"GitHub开源项目\" href=\"https://github.com/jenly1314\" target=\"_blank\"\u003ejenly1314\u003c/a\u003e | \u003ca title=\"Gitee开源项目\" href=\"https://gitee.com/jenly1314\" target=\"_blank\"\u003ejenly1314\u003c/a\u003e | \u003ca title=\"CSDN博客\" href=\"http://blog.csdn.net/jenly121\" target=\"_blank\"\u003ejenly121\u003c/a\u003e | \u003ca title=\"博客园\" href=\"https://www.cnblogs.com/jenly\" target=\"_blank\"\u003ejenly\u003c/a\u003e |\n\n## 联系我\n\n| 微信公众号                                                   | Gmail邮箱                                                                          | QQ邮箱                                                                              | QQ群                                                                                                                       | QQ群                                                                                                                       |\n|:--------------------------------------------------------|:---------------------------------------------------------------------------------|:----------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------------------|\n| [Jenly666](http://weixin.qq.com/r/wzpWTuPEQL4-ract92-R) | \u003ca title=\"给我发邮件\" href=\"mailto:jenly1314@gmail.com\" target=\"_blank\"\u003ejenly1314\u003c/a\u003e | \u003ca title=\"给我发邮件\" href=\"mailto:jenly1314@vip.qq.com\" target=\"_blank\"\u003ejenly1314\u003c/a\u003e | \u003ca title=\"点击加入QQ群\" href=\"https://qm.qq.com/cgi-bin/qm/qr?k=6_RukjAhwjAdDHEk2G7nph-o8fBFFzZz\" target=\"_blank\"\u003e20867961\u003c/a\u003e | \u003ca title=\"点击加入QQ群\" href=\"https://qm.qq.com/cgi-bin/qm/qr?k=Z9pobM8bzAW7tM_8xC31W8IcbIl0A-zT\" target=\"_blank\"\u003e64020761\u003c/a\u003e |\n\n\u003cdiv\u003e\n   \u003cimg src=\"https://jenly1314.github.io/image/page/footer.png\"\u003e\n\u003c/div\u003e\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjenly1314%2Flogx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjenly1314%2Flogx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjenly1314%2Flogx/lists"}