Skip to content

Commit 8e3bd9b

Browse files
committed
[rk-logger] Combine zap & lumber config together [commit:dongxuny]
1 parent b5ce401 commit 8e3bd9b

File tree

5 files changed

+55
-97
lines changed

5 files changed

+55
-97
lines changed

README.md

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ Currently, support zap logger as default logger and lumberjack as log rotation
99
`go get -u rookie-ninja/rk-logger`
1010

1111
## Quick Start
12-
In order to init zap logger with full log rotation, rk-logger support three different utility functions
13-
- With zap config file path
14-
- With zap config as byte array
15-
- With zap config
12+
We combined zap config and lumberjack config in the same config file
13+
Both of the configs could keep same format as it
1614

17-
**Caution! Since zap and lumberjack both support log file path, we rk-logger define log file path in zap config file only.**
15+
In order to init zap logger with full log rotation, rk-logger support three different utility functions
16+
- With zap+lumberjack config file path
17+
- With zap+lumberjack config as byte array
18+
- With zap config and lumberjack config
1819

1920
### With Config file path
20-
zap config:
21+
config:
2122
```yaml
2223
---
2324
level: debug
@@ -47,27 +48,21 @@ encoderConfig:
4748
sampling:
4849
initial: '3'
4950
thereafter: '10'
50-
```
51-
lumberjack config:
52-
```yaml
53-
---
5451
maxsize: 1
5552
maxage: 7
5653
maxbackups: 3
5754
localtime: true
5855
compress: true
5956
```
57+
6058
Example:
6159
```go
6260
func NewZapLoggerWithConfPathExample() {
6361
// get current working directory
6462
dir, _ := os.Getwd()
65-
66-
// init lumberjack logger
67-
lumberjack, _ := rk_logger.NewLumberjackLoggerWithConfPath(path.Clean(path.Join(dir, "/assets/lumberjack.yaml")), rk_logger.YAML)
68-
69-
// init zap logger
70-
logger, _, _ := rk_logger.NewZapLoggerWithConfPath(path.Clean(path.Join(dir, "/assets/zap.yaml")), rk_logger.YAML, lumberjack)
63+
64+
// init logger
65+
logger, _, _ := rk_logger.NewZapLoggerWithConfPath(path.Clean(path.Join(dir, "/assets/zap.yaml")), rk_logger.YAML)
7166

7267
// use it
7368
logger.Info("NewZapLoggerWithConfPathExample")
@@ -77,7 +72,7 @@ func NewZapLoggerWithConfPathExample() {
7772
Example:
7873
```go
7974
func NewZapLoggerWithBytesExample() {
80-
zapBytes := []byte(`{
75+
bytes := []byte(`{
8176
"level": "debug",
8277
"encoding": "console",
8378
"outputPaths": ["stdout", "logs/rk-logger.log"],
@@ -102,20 +97,15 @@ func NewZapLoggerWithBytesExample() {
10297
"initial": "3",
10398
"thereafter": "10"
10499
}
105-
}
100+
},
101+
"maxsize": 1,
102+
"maxage": 7,
103+
"maxbackups": 3,
104+
"localtime": true,
105+
"compress": true
106106
}`)
107-
108-
lumberBytes := []byte(`{
109-
"maxsize": 1,
110-
"maxage": 7,
111-
"maxbackups": 3,
112-
"localtime": true,
113-
"compress": true
114-
}`)
115-
116-
lumber, _ := rk_logger.NewLumberjackLoggerWithBytes(lumberBytes, rk_logger.JSON)
117107

118-
logger, _, err := rk_logger.NewZapLoggerWithBytes(zapBytes, rk_logger.JSON, lumber)
108+
logger, _, err := rk_logger.NewZapLoggerWithBytes(bytes, rk_logger.JSON)
119109

120110
logger.Info("NewZapLoggerWithBytesExample")
121111
}

assets/zap.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@ encoderConfig:
2525
nameEncoder: full
2626
sampling:
2727
initial: '3'
28-
thereafter: '10'
28+
thereafter: '10'
29+
maxsize: 1
30+
maxage: 7
31+
maxbackups: 3
32+
localtime: true
33+
compress: true

example/example.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,8 @@ func NewZapLoggerWithConfExample() {
7272
func NewZapLoggerWithConfPathExample() {
7373
// get current working directory
7474
dir, _ := os.Getwd()
75-
// init lumberjack logger
76-
lumberjack, _ := rk_logger.NewLumberjackLoggerWithConfPath(path.Clean(path.Join(dir, "/assets/lumberjack.yaml")), rk_logger.YAML)
7775
// init zap logger
78-
logger, _, _ := rk_logger.NewZapLoggerWithConfPath(path.Clean(path.Join(dir, "/assets/zap.yaml")), rk_logger.YAML, lumberjack)
76+
logger, _, _ := rk_logger.NewZapLoggerWithConfPath(path.Clean(path.Join(dir, "/assets/zap.yaml")), rk_logger.YAML)
7977
// use it
8078
logger.Info("NewZapLoggerWithConfPathExample")
8179
}
@@ -106,23 +104,15 @@ func NewZapLoggerWithBytesExample() {
106104
"initial": "3",
107105
"thereafter": "10"
108106
}
109-
}
110-
}`)
111-
112-
lumberBytes := []byte(`{
107+
},
113108
"maxsize": 1,
114109
"maxage": 7,
115110
"maxbackups": 3,
116111
"localtime": true,
117112
"compress": true
118113
}`)
119114

120-
lumber, err := rk_logger.NewLumberjackLoggerWithBytes(lumberBytes, rk_logger.JSON)
121-
if err != nil {
122-
panic(err)
123-
}
124-
125-
logger, _, err := rk_logger.NewZapLoggerWithBytes(zapBytes, rk_logger.JSON, lumber)
115+
logger, _, err := rk_logger.NewZapLoggerWithBytes(zapBytes, rk_logger.JSON)
126116

127117
if err != nil {
128118
panic(err)

initializer.go

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ package rk_logger
66

77
import (
88
"encoding/json"
9-
"github.com/BurntSushi/toml"
10-
"github.com/hashicorp/hcl"
119
"github.com/pkg/errors"
1210
"go.uber.org/zap"
1311
"go.uber.org/zap/zapcore"
@@ -23,21 +21,17 @@ type FileType int
2321
// Config file type which support json, yaml, toml and hcl
2422
// JSON: https://www.json.org/
2523
// YAML: https://yaml.org/
26-
// TOML: https://github.com/toml-lang/toml
27-
// HCL : https://github.com/hashicorp/hcl
2824
const (
2925
JSON FileType = 0
3026
YAML FileType = 1
31-
TOML FileType = 2
32-
HCL FileType = 3
3327
)
3428

3529
// Stringfy above config file types.
3630
func (fileType FileType) String() string {
37-
names := [...]string{"JSON", "YAML", "TOML", "HCL"}
31+
names := [...]string{"JSON", "YAML"}
3832

3933
// Please do not forget to change the boundary while adding a new config file types
40-
if fileType < JSON || fileType > HCL {
34+
if fileType < JSON || fileType > YAML {
4135
return "UNKNOWN"
4236
}
4337

@@ -47,7 +41,7 @@ func (fileType FileType) String() string {
4741
// Init zap logger with byte array from content of config file
4842
// lumberjack.Logger could be empty, if not provided,
4943
// then, we will use default write sync
50-
func NewZapLoggerWithBytes(raw []byte, fileType FileType, lumber *lumberjack.Logger, opts ...zap.Option) (*zap.Logger, *zap.Config, error) {
44+
func NewZapLoggerWithBytes(raw []byte, fileType FileType, opts ...zap.Option) (*zap.Logger, *zap.Config, error) {
5145
if raw == nil {
5246
return nil, nil, errors.New("input byte array is nil")
5347
}
@@ -59,36 +53,33 @@ func NewZapLoggerWithBytes(raw []byte, fileType FileType, lumber *lumberjack.Log
5953
// Initialize zap logger from config file
6054
var logger *zap.Logger
6155
var err error
62-
config := &zap.Config{}
56+
zapConfig := &zap.Config{}
57+
lumberConfig := &lumberjack.Logger{}
6358

6459
if fileType == JSON {
65-
// parse json file
66-
if err := json.Unmarshal(raw, config); err != nil {
60+
// parse zap json file
61+
if err := json.Unmarshal(raw, zapConfig); err != nil {
6762
return nil, nil, err
6863
}
6964

70-
logger, err = NewZapLoggerWithConf(config, lumber, opts...)
71-
} else if fileType == YAML {
72-
// parse yaml file
73-
if err := yaml.Unmarshal(raw, config); err != nil {
65+
// parse lumberjack json file
66+
if err := json.Unmarshal(raw, lumberConfig); err != nil {
7467
return nil, nil, err
7568
}
7669

77-
logger, err = NewZapLoggerWithConf(config, lumber, opts...)
78-
} else if fileType == TOML {
79-
// parse toml file
80-
if err := toml.Unmarshal(raw, config); err != nil {
70+
logger, err = NewZapLoggerWithConf(zapConfig, lumberConfig, opts...)
71+
} else if fileType == YAML {
72+
// parse zap yaml file
73+
if err := yaml.Unmarshal(raw, zapConfig); err != nil {
8174
return nil, nil, err
8275
}
8376

84-
logger, err = NewZapLoggerWithConf(config, lumber, opts...)
85-
} else if fileType == HCL {
86-
// parse hcl file
87-
if err := hcl.Decode(config, string(raw)); err != nil {
77+
// parse lumberjack yaml file
78+
if err := yaml.Unmarshal(raw, lumberConfig); err != nil {
8879
return nil, nil, err
8980
}
9081

91-
logger, err = NewZapLoggerWithConf(config, lumber, opts...)
82+
logger, err = NewZapLoggerWithConf(zapConfig, lumberConfig, opts...)
9283
} else {
9384
logger, err = nil, errors.New("invalid config file")
9485
}
@@ -98,14 +89,14 @@ func NewZapLoggerWithBytes(raw []byte, fileType FileType, lumber *lumberjack.Log
9889
return nil, nil, err
9990
}
10091

101-
return logger, config, err
92+
return logger, zapConfig, err
10293
}
10394

10495
// Init zap logger with config file path
10596
// File path needs to be absolute path
10697
// lumberjack.Logger could be empty, if not provided,
10798
// then, we will use default write sync
108-
func NewZapLoggerWithConfPath(filePath string, fileType FileType, lumber *lumberjack.Logger, opts ...zap.Option) (*zap.Logger, *zap.Config, error) {
99+
func NewZapLoggerWithConfPath(filePath string, fileType FileType, opts ...zap.Option) (*zap.Logger, *zap.Config, error) {
109100
if len(filePath) == 0 {
110101
return nil, nil, errors.New("file path is empty")
111102
}
@@ -119,12 +110,11 @@ func NewZapLoggerWithConfPath(filePath string, fileType FileType, lumber *lumber
119110

120111
if err == nil {
121112
bytes, readErr := ioutil.ReadFile(filePath)
122-
123-
if readErr == nil {
124-
logger, config, err = NewZapLoggerWithBytes(bytes, fileType, lumber, opts...)
125-
} else {
126-
err = readErr
113+
if readErr != nil {
114+
return logger, config, readErr
127115
}
116+
117+
logger, config, err = NewZapLoggerWithBytes(bytes, fileType, opts...)
128118
}
129119

130120
return logger, config, err

initializer_test.go

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,11 @@ import (
1515
func TestConfigFileType_Indexing(t *testing.T) {
1616
assert.Equal(t, FileType(0), JSON)
1717
assert.Equal(t, FileType(1), YAML)
18-
assert.Equal(t, FileType(2), TOML)
19-
assert.Equal(t, FileType(3), HCL)
2018
}
2119

2220
func TestConfigFileType_String_HappyCase(t *testing.T) {
2321
assert.Equal(t, "JSON", JSON.String())
2422
assert.Equal(t, "YAML", YAML.String())
25-
assert.Equal(t, "TOML", TOML.String())
26-
assert.Equal(t, "HCL", HCL.String())
2723
}
2824

2925
func TestConfigFileType_String_Overflow_LeftBoundary(t *testing.T) {
@@ -68,24 +64,6 @@ func TestNewZapLoggerWithBytes_With_InvalidYaml(t *testing.T) {
6864
assert.NotNil(t, err)
6965
}
7066

71-
// With invalid toml
72-
func TestNewZapLoggerWithBytes_With_InvalidToml(t *testing.T) {
73-
invalidToml := `key: "value"`
74-
logger, config, err := NewZapLoggerWithBytes([]byte(invalidToml), TOML, nil)
75-
assert.Nil(t, logger)
76-
assert.Nil(t, config)
77-
assert.NotNil(t, err)
78-
}
79-
80-
// With invalid toml
81-
func TestNewZapLoggerWithBytes_With_InvalidHCL(t *testing.T) {
82-
invalidHCL := `"key" : "value"`
83-
logger, config, err := NewZapLoggerWithBytes([]byte(invalidHCL), HCL, nil)
84-
assert.Nil(t, logger)
85-
assert.Nil(t, config)
86-
assert.NotNil(t, err)
87-
}
88-
8967
// With unmatched type
9068
func TestNewZapLoggerWithBytes_With_InvalidType(t *testing.T) {
9169
json := `{"key":"value"}`
@@ -140,6 +118,11 @@ func TestNewZapLoggerWithBytes_HappyCase(t *testing.T) {
140118
"thereafter": "10"
141119
}
142120
}
121+
"maxsize": 1,
122+
"maxage": 7,
123+
"maxbackups": 3,
124+
"localtime": true,
125+
"compress": true
143126
}`)
144127
logger, config, err := NewZapLoggerWithBytes(bytes, JSON, nil)
145128
assert.NotNil(t, logger)

0 commit comments

Comments
 (0)