├── examples ├── README.md ├── custom │ └── main.go └── default │ └── main.go ├── go.mod ├── .gitignore ├── go.sum ├── LICENSE ├── init.go ├── sugar.go ├── logger.go ├── README.md └── README_zh.md /examples/README.md: -------------------------------------------------------------------------------- 1 | # Description 2 | 3 | ### default 4 | use sugar directly with default "zap logger". 5 | 6 | ### custom 7 | custom logger. -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/chalvern/sugar 2 | 3 | go 1.12 4 | 5 | require ( 6 | go.uber.org/atomic v1.4.0 // indirect 7 | go.uber.org/multierr v1.1.0 // indirect 8 | go.uber.org/zap v1.10.0 9 | ) 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, build with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | 14 | # IDE 15 | .vscode 16 | 17 | # log 18 | *.log -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU= 2 | go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= 3 | go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI= 4 | go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= 5 | go.uber.org/zap v1.10.0 h1:ORx85nbTijNz8ljznvCMR1ZBIPKFn3jQrag10X2AsuM= 6 | go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= 7 | -------------------------------------------------------------------------------- /examples/custom/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/chalvern/sugar" 5 | "go.uber.org/zap" 6 | ) 7 | 8 | func main() { 9 | config := zap.NewProductionConfig() 10 | config.OutputPaths = []string{"./production.log"} 11 | config.ErrorOutputPaths = []string{"./production_err.log"} 12 | sugar.SetSugar(&config) 13 | 14 | sugar.Info("main info") 15 | 16 | myCustomLogger() 17 | } 18 | 19 | func myCustomLogger() { 20 | myLogger := sugar.NewLoggerOf("my_custom_logger") 21 | myLogger.Info("log of myCustomLogger info") 22 | myLogger.Warn("log of myCustomLogger warn") 23 | } 24 | -------------------------------------------------------------------------------- /examples/default/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "github.com/chalvern/sugar" 4 | 5 | func main() { 6 | 7 | sugar.Debug("default development sugar of chalvern") 8 | myCustomLogger() 9 | myCustomLogger2() 10 | 11 | sugar.InitProductionSugar() 12 | sugar.Debug("should not be printed") 13 | sugar.Info("default production sugar of chalvern") 14 | myCustomLogger() 15 | myCustomLogger2() 16 | } 17 | 18 | func myCustomLogger() { 19 | myLogger := sugar.NewLoggerOf("my_custom_logger") 20 | myLogger.Info("log of myCustomLogger info") 21 | myLogger.Warn("log of myCustomLogger warn") 22 | } 23 | 24 | func myCustomLogger2() { 25 | myLogger := sugar.NewLoggerOf("my_custom_logger_2") 26 | myLogger.Info("log of myCustomLogger2 info") 27 | myLogger.Warn("log of myCustomLogger2 warn") 28 | } 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Jing维 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /init.go: -------------------------------------------------------------------------------- 1 | package sugar 2 | 3 | import ( 4 | "go.uber.org/zap" 5 | ) 6 | 7 | var ( 8 | zapLogger *zap.Logger 9 | zapSugar *zap.SugaredLogger 10 | // mainLogger default logger, with unit filed valued "main" 11 | mainLogger *Logger 12 | ) 13 | 14 | // GetZapLogger get zap's logger for special using 15 | func GetZapLogger() *zap.Logger { 16 | return zapLogger 17 | } 18 | 19 | func init() { 20 | config := zap.NewDevelopmentConfig() 21 | config.OutputPaths = []string{"stderr"} 22 | config.ErrorOutputPaths = []string{"stderr"} 23 | SetSugar(&config) 24 | } 25 | 26 | // InitProductionSugar for production 27 | func InitProductionSugar(opts ...zap.Option) { 28 | config := zap.NewProductionConfig() 29 | config.OutputPaths = []string{"stderr"} 30 | config.ErrorOutputPaths = []string{"stderr"} 31 | SetSugar(&config, opts...) 32 | } 33 | 34 | // InitDevelopmentSugar for development 35 | // encoding being json instead of console, 36 | // which is practically better for docker's json logger, 37 | func InitDevelopmentSugar(opts ...zap.Option) { 38 | config := zap.NewDevelopmentConfig() 39 | config.Encoding = "json" 40 | config.OutputPaths = []string{"stderr"} 41 | config.ErrorOutputPaths = []string{"stderr"} 42 | SetSugar(&config, opts...) 43 | } 44 | 45 | // SetSugar set sugar's logger 46 | func SetSugar(config *zap.Config, opts ...zap.Option) { 47 | opts = append(opts, zap.AddCallerSkip(1)) 48 | var err error 49 | zapLogger, err = config.Build(opts...) 50 | if err != nil { 51 | panic(err) 52 | } 53 | zapSugar = zapLogger.Sugar() 54 | mainLogger = NewLoggerOf("main") 55 | } 56 | -------------------------------------------------------------------------------- /sugar.go: -------------------------------------------------------------------------------- 1 | package sugar 2 | 3 | // Debug package mainLogger 4 | func Debug(args ...interface{}) { 5 | mainLogger.zapSugar.Debug(args...) 6 | } 7 | 8 | // Debugf package mainLogger 9 | func Debugf(template string, args ...interface{}) { 10 | mainLogger.zapSugar.Debugf(template, args...) 11 | } 12 | 13 | // Debugw package mainLogger 14 | func Debugw(msg string, keysAndValues ...interface{}) { 15 | mainLogger.zapSugar.Debugw(msg, keysAndValues...) 16 | } 17 | 18 | // Info package mainLogger 19 | func Info(args ...interface{}) { 20 | mainLogger.zapSugar.Info(args...) 21 | } 22 | 23 | // Infof package mainLogger 24 | func Infof(template string, args ...interface{}) { 25 | mainLogger.zapSugar.Infof(template, args...) 26 | } 27 | 28 | // Infow package mainLogger 29 | func Infow(msg string, keysAndValues ...interface{}) { 30 | mainLogger.zapSugar.Infow(msg, keysAndValues...) 31 | } 32 | 33 | // Warn package mainLogger 34 | func Warn(args ...interface{}) { 35 | mainLogger.zapSugar.Warn(args...) 36 | } 37 | 38 | // Warnf package mainLogger 39 | func Warnf(template string, args ...interface{}) { 40 | mainLogger.zapSugar.Warnf(template, args...) 41 | } 42 | 43 | // Warnw package mainLogger 44 | func Warnw(msg string, keysAndValues ...interface{}) { 45 | mainLogger.zapSugar.Warnw(msg, keysAndValues...) 46 | } 47 | 48 | // Error package mainLogger 49 | func Error(args ...interface{}) { 50 | mainLogger.zapSugar.Error(args...) 51 | } 52 | 53 | // Errorf package mainLogger 54 | func Errorf(template string, args ...interface{}) { 55 | mainLogger.zapSugar.Errorf(template, args...) 56 | } 57 | 58 | // Errorw package mainLogger 59 | func Errorw(msg string, keysAndValues ...interface{}) { 60 | mainLogger.zapSugar.Errorw(msg, keysAndValues...) 61 | } 62 | 63 | // Fatal package mainLogger 64 | func Fatal(args ...interface{}) { 65 | mainLogger.zapSugar.Fatal(args...) 66 | } 67 | 68 | // Fatalf package mainLogger 69 | func Fatalf(template string, args ...interface{}) { 70 | mainLogger.zapSugar.Fatalf(template, args...) 71 | } 72 | 73 | // Fatalw package mainLogger 74 | func Fatalw(msg string, keysAndValues ...interface{}) { 75 | mainLogger.zapSugar.Fatalw(msg, keysAndValues...) 76 | } 77 | 78 | // Panic package mainLogger 79 | func Panic(args ...interface{}) { 80 | mainLogger.zapSugar.Panic(args...) 81 | } 82 | 83 | // Panicf package mainLogger 84 | func Panicf(template string, args ...interface{}) { 85 | mainLogger.zapSugar.Panicf(template, args...) 86 | } 87 | 88 | // Panicw package mainLogger 89 | func Panicw(msg string, keysAndValues ...interface{}) { 90 | mainLogger.zapSugar.Panicw(msg, keysAndValues...) 91 | } 92 | -------------------------------------------------------------------------------- /logger.go: -------------------------------------------------------------------------------- 1 | package sugar 2 | 3 | import ( 4 | "go.uber.org/zap" 5 | ) 6 | 7 | const ( 8 | unitString = "unit" 9 | ) 10 | 11 | // Logger 定制化 sugar 12 | type Logger struct { 13 | zapSugar *zap.SugaredLogger 14 | } 15 | 16 | // NewLoggerOf logger with component field 17 | func NewLoggerOf(component string) *Logger { 18 | return &Logger{ 19 | zapSugar: zapSugar.With(unitString, component), 20 | } 21 | } 22 | 23 | // With adds a variadic number of fields to the logging context. 24 | // see https://github.com/uber-go/zap/blob/v1.10.0/sugar.go#L91 25 | func (l *Logger) With(args ...interface{}) *zap.SugaredLogger { 26 | return l.zapSugar.With(args...) 27 | } 28 | 29 | // Debug package sugar of zap 30 | func (l *Logger) Debug(args ...interface{}) { 31 | l.zapSugar.Debug(args...) 32 | } 33 | 34 | // Debugf package sugar of zap 35 | func (l *Logger) Debugf(template string, args ...interface{}) { 36 | l.zapSugar.Debugf(template, args...) 37 | } 38 | 39 | // Debugw package sugar of zap 40 | func (l *Logger) Debugw(msg string, keysAndValues ...interface{}) { 41 | l.zapSugar.Debugw(msg, keysAndValues...) 42 | } 43 | 44 | // Info package sugar of zap 45 | func (l *Logger) Info(args ...interface{}) { 46 | l.zapSugar.Info(args...) 47 | } 48 | 49 | // Infof package sugar of zap 50 | func (l *Logger) Infof(template string, args ...interface{}) { 51 | l.zapSugar.Infof(template, args...) 52 | } 53 | 54 | // Infow package sugar of zap 55 | func (l *Logger) Infow(msg string, keysAndValues ...interface{}) { 56 | l.zapSugar.Infow(msg, keysAndValues...) 57 | } 58 | 59 | // Warn package sugar of zap 60 | func (l *Logger) Warn(args ...interface{}) { 61 | l.zapSugar.Warn(args...) 62 | } 63 | 64 | // Warnf package sugar of zap 65 | func (l *Logger) Warnf(template string, args ...interface{}) { 66 | l.zapSugar.Warnf(template, args...) 67 | } 68 | 69 | // Warnw package sugar of zap 70 | func (l *Logger) Warnw(msg string, keysAndValues ...interface{}) { 71 | l.zapSugar.Warnw(msg, keysAndValues...) 72 | } 73 | 74 | // Error package sugar of zap 75 | func (l *Logger) Error(args ...interface{}) { 76 | l.zapSugar.Error(args...) 77 | } 78 | 79 | // Errorf package sugar of zap 80 | func (l *Logger) Errorf(template string, args ...interface{}) { 81 | l.zapSugar.Errorf(template, args...) 82 | } 83 | 84 | // Errorw package sugar of zap 85 | func (l *Logger) Errorw(msg string, keysAndValues ...interface{}) { 86 | l.zapSugar.Errorw(msg, keysAndValues...) 87 | } 88 | 89 | // Fatal package sugar of zap 90 | func (l *Logger) Fatal(args ...interface{}) { 91 | l.zapSugar.Fatal(args...) 92 | } 93 | 94 | // Fatalf package sugar of zap 95 | func (l *Logger) Fatalf(template string, args ...interface{}) { 96 | l.zapSugar.Fatalf(template, args...) 97 | } 98 | 99 | // Fatalw package sugar of zap 100 | func (l *Logger) Fatalw(msg string, keysAndValues ...interface{}) { 101 | l.zapSugar.Fatalw(msg, keysAndValues...) 102 | } 103 | 104 | // Panic package sugar of zap 105 | func (l *Logger) Panic(args ...interface{}) { 106 | l.zapSugar.Panic(args...) 107 | } 108 | 109 | // Panicf package sugar of zap 110 | func (l *Logger) Panicf(template string, args ...interface{}) { 111 | l.zapSugar.Panicf(template, args...) 112 | } 113 | 114 | // Panicw package sugar of zap 115 | func (l *Logger) Panicw(msg string, keysAndValues ...interface{}) { 116 | l.zapSugar.Panicw(msg, keysAndValues...) 117 | } 118 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sugar 2 | 3 | 《[中文文档](./README_zh.md)》 4 | 5 | more simple logger which package sugared [zap](https://github.com/uber-go/zap). 6 | Please learn [zap](https://github.com/uber-go/zap) first if you want advanced using. 7 | 8 | 9 | ## something you should know 10 | 11 | * sugar use `zap.NewDevelopmentConfig()` as default config, which use console as encoding style, 12 | * `InitDevelopmentSugar()` set encoding being json instead of console, which is practically better for docker's json logger plugin. 13 | 14 | 15 | ## Leveled method 16 | 17 | all methods of zap: 18 | 19 | * Debug/Debugf 20 | * Info/Infof 21 | * Warn/Warnf 22 | * Error/Errorf 23 | * Fatal/Fatalf 24 | * Panic/Panicf 25 | 26 | ## logs looking 27 | I recommend zap mostly because of its beautiful logs looking. 28 | 29 | ### looking #1 30 | Development mode with console style printing. 31 | 32 | ```bash 33 | 2019-06-10T09:13:03.672+0800 DEBUG default/main.go:7 default development sugar of chalvern {"unit": "main"} 34 | 2019-06-10T09:13:03.672+0800 INFO default/main.go:20 log of myCustomLogger info {"unit": "my_custom_logger"} 35 | 2019-06-10T09:13:03.672+0800 WARN default/main.go:21 log of myCustomLogger warn {"unit": "my_custom_logger"} 36 | github.com/chalvern/sugar.(*Logger).Warn 37 | /Users/Chalvern/developer/golang/src/github.com/chalvern/sugar/logger.go:51 38 | main.myCustomLogger 39 | /Users/Chalvern/developer/golang/src/github.com/chalvern/sugar/examples/default/main.go:21 40 | main.main 41 | /Users/Chalvern/developer/golang/src/github.com/chalvern/sugar/examples/default/main.go:8 42 | runtime.main 43 | /usr/local/go/src/runtime/proc.go:200 44 | 2019-06-10T09:13:03.672+0800 INFO default/main.go:26 log of myCustomLogger2 info {"unit": "my_custom_logger_2"} 45 | 2019-06-10T09:13:03.672+0800 WARN default/main.go:27 log of myCustomLogger2 warn {"unit": "my_custom_logger_2"} 46 | github.com/chalvern/sugar.(*Logger).Warn 47 | /Users/Chalvern/developer/golang/src/github.com/chalvern/sugar/logger.go:51 48 | main.myCustomLogger2 49 | /Users/Chalvern/developer/golang/src/github.com/chalvern/sugar/examples/default/main.go:27 50 | main.main 51 | /Users/Chalvern/developer/golang/src/github.com/chalvern/sugar/examples/default/main.go:9 52 | runtime.main 53 | /usr/local/go/src/runtime/proc.go:200 54 | ``` 55 | 56 | ### looking #2 57 | 58 | Production mode with json style printing (Development mode can also has json stype printing, more see [something you should know](#something-you-should-know) ) 59 | 60 | ```json 61 | {"level":"info","ts":1560129183.672966,"caller":"default/main.go:13","msg":"default production sugar of chalvern","unit":"main"} 62 | {"level":"info","ts":1560129183.673015,"caller":"default/main.go:20","msg":"log of myCustomLogger info","unit":"my_custom_logger"} 63 | {"level":"warn","ts":1560129183.6730392,"caller":"default/main.go:21","msg":"log of myCustomLogger warn","unit":"my_custom_logger"} 64 | {"level":"info","ts":1560129183.673059,"caller":"default/main.go:26","msg":"log of myCustomLogger2 info","unit":"my_custom_logger_2"} 65 | {"level":"warn","ts":1560129183.673182,"caller":"default/main.go:27","msg":"log of myCustomLogger2 warn","unit":"my_custom_logger_2"} 66 | ``` 67 | 68 | ## example 69 | 70 | you can find example in [examples](./examples). 71 | 72 | 73 | ### default sugar 74 | ```go 75 | // cat ./examples/default/main.go 76 | package main 77 | 78 | import "github.com/chalvern/sugar" 79 | 80 | func main() { 81 | 82 | sugar.Debug("default development sugar of chalvern") 83 | myCustomLogger() 84 | myCustomLogger2() 85 | 86 | sugar.InitProductionSugar() 87 | sugar.Debug("should not be printed") 88 | sugar.Info("default production sugar of chalvern") 89 | myCustomLogger() 90 | myCustomLogger2() 91 | } 92 | 93 | func myCustomLogger() { 94 | myLogger := sugar.NewLoggerOf("my_custom_logger") 95 | myLogger.Info("log of myCustomLogger info") 96 | myLogger.Warn("log of myCustomLogger warn") 97 | } 98 | 99 | func myCustomLogger2() { 100 | myLogger := sugar.NewLoggerOf("my_custom_logger_2") 101 | myLogger.Info("log of myCustomLogger2 info") 102 | myLogger.Warn("log of myCustomLogger2 warn") 103 | } 104 | ``` 105 | 106 | ### custom sugar 107 | 108 | ```go 109 | package main 110 | 111 | import ( 112 | "github.com/chalvern/sugar" 113 | "go.uber.org/zap" 114 | ) 115 | 116 | func main() { 117 | config := zap.NewProductionConfig() 118 | config.OutputPaths = []string{"./production.log"} 119 | config.ErrorOutputPaths = []string{"./production_err.log"} 120 | sugar.SetSugar(&config) 121 | 122 | sugar.Info("main info") 123 | 124 | myCustomLogger() 125 | } 126 | 127 | func myCustomLogger() { 128 | myLogger := sugar.NewLoggerOf("my_custom_logger") 129 | myLogger.Info("log of myCustomLogger info") 130 | myLogger.Warn("log of myCustomLogger warn") 131 | } 132 | 133 | ``` -------------------------------------------------------------------------------- /README_zh.md: -------------------------------------------------------------------------------- 1 | # sugar 2 | 3 | 《[English README](./README.md)》 4 | 5 | Sugar 封装了 [zap](https://github.com/uber-go/zap) 日志库,让开发者能够更方便、快捷地使用这个插件。 6 | 本库只是简单的封装,如果使用过程中遇到问题,推荐阅读 [zap](https://github.com/uber-go/zap) 的相关文档寻找答案。 7 | 8 | 当然,欢迎大家提 issue 来一起完善这个仓库。 9 | 10 | 11 | ## 一些注意点 12 | 13 | * Sugar 使用 zap 仓库的 `zap.NewDevelopmentConfig()` 方法返回的配置作为默认配置(开发环境),这个配置使用 console 类型的输出格式(平铺的那种,区别于 json 类型的日志) 14 | * 我曾经的工作阅历,认为服务容器化会是未来的趋势,因此觉得开发环境也有必要配置 json 格式的输出,通过 `InitDevelopmentSugar()` 可以达到目的。这样大家写容器化的服务时可以直接使用这个库而不需要定制日志路径。 15 | * 生产环境默认就是 json 格式,主要是为了方便地把日志收集到 ELK 中去。 16 | 17 | 18 | ## 方法 19 | 20 | Sugar 封装了 sugared-zap 的所有方法,如下(如果有疏漏,可以提 issue 通知我): 21 | 22 | * Debug/Debugf/Debugw 23 | * Info/Infof/Infow 24 | * Warn/Warnf/Warnw 25 | * Error/Errorf/Errorw 26 | * Fatal/Fatalf/Fatalw 27 | * Panic/Panicf/Panicw 28 | 29 | 30 | ## logs 格式预先看 31 | 32 | 我之所以喜欢使用 zap,主要因为它漂亮的输出格式(根据官方的描述,它的输出效率也很高,不过我还没有遇到这个瓶颈) 33 | 34 | ### 默认的开发模式 35 | 36 | 这种模式的输出是 console 样式,就是说日志平铺展示(开发模式也可以配置 json 格式的输出,见 [一些注意点](#一些注意点)。 37 | 38 | ```bash 39 | 2019-06-10T09:13:03.672+0800 DEBUG default/main.go:7 default development sugar of chalvern {"unit": "main"} 40 | 2019-06-10T09:13:03.672+0800 INFO default/main.go:20 log of myCustomLogger info {"unit": "my_custom_logger"} 41 | 2019-06-10T09:13:03.672+0800 WARN default/main.go:21 log of myCustomLogger warn {"unit": "my_custom_logger"} 42 | github.com/chalvern/sugar.(*Logger).Warn 43 | /Users/Chalvern/developer/golang/src/github.com/chalvern/sugar/logger.go:51 44 | main.myCustomLogger 45 | /Users/Chalvern/developer/golang/src/github.com/chalvern/sugar/examples/default/main.go:21 46 | main.main 47 | /Users/Chalvern/developer/golang/src/github.com/chalvern/sugar/examples/default/main.go:8 48 | runtime.main 49 | /usr/local/go/src/runtime/proc.go:200 50 | 2019-06-10T09:13:03.672+0800 INFO default/main.go:26 log of myCustomLogger2 info {"unit": "my_custom_logger_2"} 51 | 2019-06-10T09:13:03.672+0800 WARN default/main.go:27 log of myCustomLogger2 warn {"unit": "my_custom_logger_2"} 52 | github.com/chalvern/sugar.(*Logger).Warn 53 | /Users/Chalvern/developer/golang/src/github.com/chalvern/sugar/logger.go:51 54 | main.myCustomLogger2 55 | /Users/Chalvern/developer/golang/src/github.com/chalvern/sugar/examples/default/main.go:27 56 | main.main 57 | /Users/Chalvern/developer/golang/src/github.com/chalvern/sugar/examples/default/main.go:9 58 | runtime.main 59 | /usr/local/go/src/runtime/proc.go:200 60 | ``` 61 | 62 | ### looking #2 63 | 64 | 生产模式默认是 json 格式的输出,从而方便把日志收集起来集中处理。 65 | 66 | ```json 67 | {"level":"info","ts":1560129183.672966,"caller":"default/main.go:13","msg":"default production sugar of chalvern","unit":"main"} 68 | {"level":"info","ts":1560129183.673015,"caller":"default/main.go:20","msg":"log of myCustomLogger info","unit":"my_custom_logger"} 69 | {"level":"warn","ts":1560129183.6730392,"caller":"default/main.go:21","msg":"log of myCustomLogger warn","unit":"my_custom_logger"} 70 | {"level":"info","ts":1560129183.673059,"caller":"default/main.go:26","msg":"log of myCustomLogger2 info","unit":"my_custom_logger_2"} 71 | {"level":"warn","ts":1560129183.673182,"caller":"default/main.go:27","msg":"log of myCustomLogger2 warn","unit":"my_custom_logger_2"} 72 | ``` 73 | 74 | ## 例子 75 | 76 | 下面的例子也可以在 [examples](./examples) 目录找到。 77 | 78 | 79 | ### 默认配置 80 | ```go 81 | // cat ./examples/default/main.go 82 | package main 83 | 84 | import "github.com/chalvern/sugar" 85 | 86 | func main() { 87 | 88 | sugar.Debug("default development sugar of chalvern") 89 | myCustomLogger() 90 | myCustomLogger2() 91 | 92 | sugar.InitProductionSugar() 93 | sugar.Debug("should not be printed") 94 | sugar.Info("default production sugar of chalvern") 95 | myCustomLogger() 96 | myCustomLogger2() 97 | } 98 | 99 | func myCustomLogger() { 100 | myLogger := sugar.NewLoggerOf("my_custom_logger") 101 | myLogger.Info("log of myCustomLogger info") 102 | myLogger.Warn("log of myCustomLogger warn") 103 | } 104 | 105 | func myCustomLogger2() { 106 | myLogger := sugar.NewLoggerOf("my_custom_logger_2") 107 | myLogger.Info("log of myCustomLogger2 info") 108 | myLogger.Warn("log of myCustomLogger2 warn") 109 | } 110 | ``` 111 | 112 | ### 定制化配置 113 | 114 | ```go 115 | package main 116 | 117 | import ( 118 | "github.com/chalvern/sugar" 119 | "go.uber.org/zap" 120 | ) 121 | 122 | func main() { 123 | config := zap.NewProductionConfig() 124 | config.OutputPaths = []string{"./production.log"} 125 | config.ErrorOutputPaths = []string{"./production_err.log"} 126 | sugar.SetSugar(&config) 127 | 128 | sugar.Info("main info") 129 | 130 | myCustomLogger() 131 | } 132 | 133 | func myCustomLogger() { 134 | myLogger := sugar.NewLoggerOf("my_custom_logger") 135 | myLogger.Info("log of myCustomLogger info") 136 | myLogger.Warn("log of myCustomLogger warn") 137 | } 138 | 139 | ``` --------------------------------------------------------------------------------