Sitemap
mofcloud

mofcloud

Co-founder @ mofcloud.com

GoFrame 101: Log management

2 min readJan 18, 2022

--

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.

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/gf

Please 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.go

IV. 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.go

IV. 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:

Access ZapLoggerEntry from code

EventLoggerEntry

rk-boot treat RPC request as an Event, and record every RPC request into Event type in rk-query.

EventLogger fields

Configure EventLoggerEntry in boot.yaml

Access EventLoggerEntry from code

Usage of Event

Event is an interface contains a log of utility functions.

Commonly used functions:

--

--