GoFrame 101: Log management
We will use rk-boot to manage logger config in gogf/gf service including log rotation, multiple log files and etc.
Introduce rk-boot
We introduce rk-boot which is a library can be used to create goLang micro-service with gogf/gf in a convenient way.
Brief introduction of concept
rk-boot integrates bellow lib to manage logger instance.
- zap: Manage logger instance
- lumberjack: Manage rotation
rk-boot defines two types of logger, we will describe detail later.
- ZapLogger: Standard logger for Error, Info and etc.
- EventLogger: For Event like logging.
Install rk-boot
go get github.com/rookie-ninja/rk-boot/gfPlease visit rkdev.info for detailed document.
In this example, we will try to change zap logger output path and encoding type.
I. Create boot.yaml
II. Create main.go
Write to zap logger.
III. Start main.go
$ go run main.goIV. Validate
├── boot.yaml
├── go.mod
├── go.sum
├── logs
│ └── zap.log
└── main.go{"level":"INFO","ts":"2021-10-21T02:10:09.279+0800","msg":"This is zap-log"}
Configure EventLogger
In this part, let’s configure EventLogger in boot.yaml.
I. Create boot.yaml
II. Create main.go
Write to event logger.
III. Start main.go
$ go run main.goIV. Validate
├── boot.yaml
├── go.mod
├── go.sum
├── logs
│ └── event.log
└── main.go{
"endTime":"2021-11-27T01:56:56.001+0800",
"startTime":"2021-11-27T01:56:56.001+0800",
"elapsedNano":423,
"timezone":"CST",
"ids":{
"eventId":"70b034b8-27af-43ad-97a5-82c99292297d"
},
"app":{
"appName":"echo-demo",
"appVersion":"master-f948c90",
"entryName":"",
"entryType":""
},
"env":{
"arch":"amd64",
"az":"*",
"domain":"*",
"hostname":"lark.local",
"localIP":"10.8.0.2",
"os":"darwin",
"realm":"*",
"region":"*"
},
"payloads":{},
"error":{},
"counters":{},
"pairs":{
"key":"value"
},
"timing":{},
"remoteAddr":"localhost",
"operation":"demo-event",
"eventStatus":"Ended",
"resCode":"OK"
}
Concept
Now, let’s talk about the concept of ZapLogger and EventLogger and the usage of them.
Architecture
ZapLoggerEntry
ZapLoggerEntry wraps zap and lumberjack instance.
Configure ZapLoggerEntry in boot.yaml
ZapLoggerEntry is compatible with zap and lumberjack YAML hierarchy.
Get mofcloud’s stories in your inbox
Join Medium for free to get updates from this writer.
Complete configuration:










