{"id":21018691,"url":"https://github.com/tal-tech/loggerx","last_synced_at":"2025-06-19T07:42:01.002Z","repository":{"id":57537853,"uuid":"284695185","full_name":"tal-tech/loggerX","owner":"tal-tech","description":"Log Factory. mix together log4go,logrus and zap, not only that,loggerX will also give you more creativity","archived":false,"fork":false,"pushed_at":"2020-09-27T02:39:45.000Z","size":59,"stargazers_count":15,"open_issues_count":0,"forks_count":9,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-03T05:12:07.971Z","etag":null,"topics":["go","log-service","log4go","logger","logrus","zap"],"latest_commit_sha":null,"homepage":"https://www.yuque.com/tal-tech/loggerx","language":"Go","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/tal-tech.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-08-03T12:33:47.000Z","updated_at":"2024-10-03T05:14:53.000Z","dependencies_parsed_at":"2022-09-07T17:24:21.171Z","dependency_job_id":null,"html_url":"https://github.com/tal-tech/loggerX","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tal-tech%2FloggerX","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tal-tech%2FloggerX/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tal-tech%2FloggerX/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tal-tech%2FloggerX/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tal-tech","download_url":"https://codeload.github.com/tal-tech/loggerX/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254288224,"owners_count":22045862,"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":["go","log-service","log4go","logger","logrus","zap"],"created_at":"2024-11-19T10:27:07.728Z","updated_at":"2025-05-15T06:31:39.790Z","avatar_url":"https://github.com/tal-tech.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"## loggerX 日志组件库\n\n[[开发文档]](https://www.yuque.com/tal-tech/loggerx)\n\n​\t日志作为整个代码行为的记录，是程序执行逻辑和异常最直接的反馈 ，xesLogger日志组件，插件化支持，支持标准输出和高性能磁盘写入,多样化的配置方式，使用起来简单方便，7个日志级别满足项目中各种需求。\n\n#### 1.下载安装  \n\n```shell\ngo get -u github.com/tal-tech/loggerX \n```\n\n\n#### 2.main函数中初始化logger：  \n* ##### 使用xml配置文件：  \n\n   ```golang\n   logger.InitLogger(\"conf/log.xml\")  //xml配置地址  \n   defer logger.Close() \n   ```\n\n   - log.xml配置文件示例：\n\n     ```xml\n      xxxxxxxxxx \u003clogging\u003e    \u003cfilter enabled=\"false\"\u003e         \u003ctag\u003estdout\u003c/tag\u003e           \u003c!-- 控制台输出日志 --\u003e        \u003ctype\u003econsole\u003c/type\u003e             \u003c!-- level is (:?FINEST|FINE|DEBUG|TRACE|INFO|WARNING|ERROR) --\u003e        \u003clevel\u003eDEBUG\u003c/level\u003e        \u003c!-- 日志级别 --\u003e    \u003c/filter\u003e    \u003cfilter enabled=\"true\"\u003e        \u003c!-- 文件输出日志 --\u003e        \u003ctag\u003egoentry\u003c/tag\u003e                \u003ctype\u003efile_trace\u003c/type\u003e          \u003clevel\u003eINFO\u003c/level\u003e           \u003c!-- 日志存储路径 --\u003e         \u003cproperty name=\"filename\"\u003e/home/logs/xeslog/teacherpanel/teacherpanel.log\u003c/property\u003e         \u003c!--        %T - Time (15:04:05 MST) 时间格式        %t - Time (15:04)        %D - Date (2006/01/02)   日期格式        %d - Date (01/02/06)        %L - Level (FNST, FINE, DEBG, TRAC, WARN, EROR, CRIT)  打印级别        %S - Source          %M - Message        It ignores unknown format strings (and removes them)        Recommended: \"[%D %T] [%L] (%S) %M\"        --\u003e        \u003c!-- 格式化输出日志 --\u003e         \u003cproperty name=\"format\"\u003e%G %L %S %M\u003c/property\u003e        \u003cproperty name=\"rotate\"\u003etrue\u003c/property\u003e \u003c!-- true enables log rotation, otherwise append --\u003e \u003cproperty name=\"maxsize\"\u003e0M\u003c/property\u003e \u003c!-- \\d+[KMG]? Suffixes are in terms of 2**10 --\u003e        \u003cproperty name=\"maxlines\"\u003e0K\u003c/property\u003e \u003c!-- \\d+[KMG]? Suffixes are in terms of thousands --\u003e        \u003cproperty name=\"daily\"\u003etrue\u003c/property\u003e \u003c!-- Automatically rotates when a log message is written after midnight --\u003e    \u003c/filter\u003e\u003c/logging\u003e\n     ```\n\n     \n\n* ##### 使用自定义配置：  \n\n   ```xml\n      config := logger.NewLogConfig()\n      config.Level=\"WARNING\"  //更新其他配置     \n      logger.InitLogWithConfig(config)  \n      defer logger.Close() \n   ```\n   * 配置说明：\n\n     ```golang\n        config格式及默认值 \n        //存储路径  默认值 /home/logs/xeslog/default/default.log\n        LogPath string  \n        //日志级别  默认值 INFO   \n        Level string  \n        //日志标签 多日志时使用  默认值 default  \n        Tag string    \n        //日志格式  默认值  \"%G %L %S %M\"  G=\u003eisoTime L=\u003elevel  S=\u003esource  M=\u003emsg    \n        Format string  \n        //最大行数切割  默认值  0K  支持K\\M\\G\\k\\m\\g 单位1000  \n        RotateLines string  \n        //最大容量切割  默认值  0M  支持K\\M\\G\\k\\m\\g 单位1024  \n        RotateSize string  \n        //按日期切割   默认值 true  \n        RotateDaily bool   \n     ```\n\n   * ##### 使用ini配置：   \n\n     ```golang\n       logMap := confutil.GetConfStringMap(\"log\")  //通过配置文件转为map[string]string     \n        config := logger.NewLogConfig()\n        config.SetConfigMap(logMap)    \n        logger.InitLogWithConfig(config)   \n        defer logger.Close() \n     ```\n\n     * ini文件配置内容:\n\n       ```golang\n          //指定字段更新，不指定使用默认值    \n          [log]      \n          LogPath=/home/logs/xeslog/logpath/log.log   \n          Level=DEBUG   \n          RotateSize=2G   \n          RotateDaily=false \n       ```\n\n#### 3.打印日志方法\n\n* 支持不同级别打印日志的方法：  \n\n```golang\nlogger.I(tag string, args interface{}, v ...interface{})  \nlogger.T(tag string, args interface{}, v ...interface{})  \nlogger.D(tag string, args interface{}, v ...interface{})  \nlogger.W(tag string, args interface{}, v ...interface{})  \nlogger.E(tag string, args interface{}, v ...interface{})  \nlogger.C(tag string, args interface{}, v ...interface{})  \nlogger.F(tag string, args interface{}, v ...interface{})  //级别同CRITICAL，但触发panic  \nlogger.Ix(ctx context.Context, tag string, args interface{}, v ...interface{})  \nlogger.Tx(ctx context.Context, tag string, args interface{}, v ...interface{})  \nlogger.Dx(ctx context.Context, tag string, args interface{}, v ...interface{})  \nlogger.Wx(ctx context.Context, tag string, args interface{}, v ...interface{})   \nlogger.Ex(ctx context.Context, tag string, args interface{}, v ...interface{})  \n```\n* 使用用例:   \n\n```golang\nlogger.I(\"TESTFUNCTAG\",\"data save to mysql, uid:%d ,name:%s\",100,\"学生1\")   \nlogger.Ix(ctx,\"TESTFUNCTAG\",\"data save to mysql, uid:%d ,name:%s\",100,\"学生1\")  \nlogger.E(\"TESTFUNCTAG\",\"get redis error：%v, uid:%d ,name:%s\",err,100,\"学生1\")   \nlogger.Ex(ctx,\"TESTFUNCTAG\",\"get redis error:%v, uid:%d ,name:%s\",err,100,\"学生1\")\n```\n* 支持携带ctx的打印方法:   \n\n```golang\n每次调用携带全局变量，支持log特殊需求，如链路追踪等     \n默认支持两个特性：  //一次请求开始时写入   \nctx = context.WithValue(ctx, \"logid\", id)   //id类型string，通过id区分一次完整请求的所有日志      \nctx = context.WithValue(ctx, \"start\", time.Now())   //每条日志会计算出相对接口开始时间耗时     \n```\n* 其他   \n\n```\n日志支持接入网校链路追踪系统,使用参考example目录\n```\n####  4.生成error \n```golang  \n调用：    \nlogger.NewError(\"error\",SYSTEM_DEFAULT)\nfunc NewError(err interface{}, ext ...XesError)       \n//err传参支持string，error类型(会自动解析rpc server错误)，表示错误根本原因    \n//ext XesError，Xes错误码，对外输出错误信息，不传默认系统异常    \ntype XesError struct {        \n    Code int    \n    Msg  string    \n} \n```\n* 其他参考    \n\n```\nerror_test.go\n```\n#### 5.日志插件\n\n​     在我们其它的日志组件中，内部打印日志的方式为 `logger.X` 的方式，默认引入xesLogger日志库实例进行打印，若您的项目中采用其它的日志库，可以使用以下方式引入您的日志库。\n\n* 支持引入logrus实例\n\n  ```golang\n  //支持logrus日志库使用LoggerX接口格式打印日志 log为logrus全局实例\n  \tlogger.AccessLogLib(\"logrus\", log)\n  ```\n\n* 支持引入zap实例\n\n  ```\n  //支持Zap日志库使用LoggerX接口格式打印日志 log为zap全局实例\n  \tlogger.AccessLogLib(\"zap\", log)\n  ```\n\n* 若现有日志格式不符合预期\n\n  ```\n  参考builders目录下builder文件，实现打印日志接口中LoggerX与Build方法即可。\n  ```\n\n* 使用参考\n\n  ```\n  日志支持接入其他日志库实例,请参考example目录 accessLogLibDemo.go中的demo\n  ```\n\n#### 6.注意事项：\n\n * logger库是并发不安全的，所以全局只能有一个实例。在写单元测时，有可能会多次初始化，此时一定要在包测试完之后进行logger.Close()操作。否则可能会出现如下错误\n ```shell\n    FileLogTraceWriter(\"/xxx/xxx.log\"): Rotate: rename /xxx/xxx.log.1: no such file or dircotry\\n\n ```\n* logger库初始化的时候会先进行一次关闭操作，如果在init方法中使用的logger日志打印，数据写入channel，main函数初始化时进行关闭channel操作时会造成panic。\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftal-tech%2Floggerx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftal-tech%2Floggerx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftal-tech%2Floggerx/lists"}