Skip to content

Commit 8a5c49b

Browse files
committed
first commit
first commit
1 parent 3153166 commit 8a5c49b

File tree

5 files changed

+159
-1
lines changed

5 files changed

+159
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010

1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
13+
.idea/

README.md

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,34 @@
11
# lg
2-
zap日志项目级别的封装
2+
3+
Project logger packaging
4+
5+
## Getting Started
6+
7+
Suitable for project log in the container
8+
9+
```
10+
package main
11+
12+
import (
13+
"github.com/qclaogui/lg"
14+
)
15+
16+
func main() {
17+
//lg.TimeFormat = time.RFC3339Nano
18+
19+
// lvl - global log level: Debug(-1), Info(0), Warn(1), Error(2), DPanic(3), Panic(4), Fatal(5)
20+
_ = lg.Init(-1, "demo-project")
21+
22+
lg.APPLog.Info("Happy Goding!")
23+
}
24+
25+
```
26+
27+
## Versioning
28+
29+
Using [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/qclaogui/lg/tags).
30+
31+
32+
## License
33+
34+
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details

go.mod

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module github.com/qclaogui/lg
2+
3+
go 1.12
4+
5+
require (
6+
github.com/davecgh/go-spew v1.1.1 // indirect
7+
github.com/pkg/errors v0.8.1 // indirect
8+
github.com/stretchr/testify v1.3.0 // indirect
9+
go.uber.org/atomic v1.4.0 // indirect
10+
go.uber.org/multierr v1.1.0 // indirect
11+
go.uber.org/zap v1.10.0
12+
)

go.sum

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
6+
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
7+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
8+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
9+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
10+
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
11+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
12+
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
13+
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
14+
go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=
15+
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
16+
go.uber.org/zap v1.10.0 h1:ORx85nbTijNz8ljznvCMR1ZBIPKFn3jQrag10X2AsuM=
17+
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=

logger.go

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package lg
2+
3+
import (
4+
"os"
5+
"sync"
6+
"time"
7+
8+
"go.uber.org/zap"
9+
"go.uber.org/zap/zapcore"
10+
)
11+
12+
var (
13+
// APPLog is global logger
14+
APPLog *zap.Logger
15+
16+
// TimeFormat is custom Time format
17+
// example: "2006-01-02T15:04:05.999999999Z07:00"
18+
// 推荐不要设置, 使用默认时间戳
19+
TimeFormat string
20+
21+
// onceInit guarantee initialize logger only once
22+
onceInit sync.Once
23+
)
24+
25+
type commonInfo struct {
26+
Project string `json:"project"`
27+
Hostname string `json:"hostname"`
28+
}
29+
30+
func (i *commonInfo) MarshalLogObject(enc zapcore.ObjectEncoder) error {
31+
enc.AddString("project", i.Project)
32+
enc.AddString("hostname", i.Hostname)
33+
return nil
34+
}
35+
36+
// customTimeEncoder encode Time to our custom format
37+
// This example how we can customize zap default functionality
38+
func customTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
39+
enc.AppendString(t.Format(TimeFormat))
40+
}
41+
42+
// Init initializes log by input parameters
43+
// lvl - global log level: Debug(-1), Info(0), Warn(1), Error(2), DPanic(3), Panic(4), Fatal(5)
44+
// timeFormat - custom time format for logger of empty string to use default
45+
func Init(lvl int, project string) (err error) {
46+
onceInit.Do(func() {
47+
// First, define our level-handling logic.
48+
globalLevel := zapcore.Level(lvl)
49+
// High-priority output should also go to standard error, and low-priority
50+
// output should also go to standard out.
51+
// It is usefull for Kubernetes deployment.
52+
// Kubernetes interprets os.Stdout log items as INFO and os.Stderr log items
53+
// as ERROR by default.
54+
highPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
55+
return lvl >= zapcore.ErrorLevel
56+
})
57+
lowPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
58+
return lvl >= globalLevel && lvl < zapcore.ErrorLevel
59+
})
60+
61+
cfg := zap.NewProductionEncoderConfig()
62+
if len(TimeFormat) > 0 {
63+
// 默认的TimeKey为(ts) float64类型. 自定义会将TimeKey修改为string,防止es中出现问题,所以换个新的key叫tsp
64+
cfg.TimeKey = "tsp"
65+
cfg.EncodeTime = customTimeEncoder
66+
}
67+
// Optimize the Kafka output for machine consumption and the console output
68+
// for human operators.
69+
consoleEncoder := zapcore.NewJSONEncoder(cfg)
70+
// Join the outputs, encoders, and level-handling functions into
71+
// zapcore.Cores, then tee the four cores together.
72+
core := zapcore.NewTee(
73+
zapcore.NewCore(consoleEncoder, zapcore.Lock(os.Stdout), lowPriority),
74+
zapcore.NewCore(consoleEncoder, zapcore.Lock(os.Stderr), highPriority),
75+
)
76+
77+
// ErrorLevel 堆栈跟踪
78+
stackTrace := zap.AddStacktrace(zap.ErrorLevel)
79+
// 设置初始化字段
80+
fields := zap.Fields(zap.Object("info", &commonInfo{project, getHostName()}))
81+
82+
// From a zapcore.Core, it's easy to construct a Logger.
83+
APPLog = zap.New(core, fields, stackTrace)
84+
zap.RedirectStdLog(APPLog)
85+
})
86+
87+
return err
88+
}
89+
90+
// getHostName 获取当前主机的Hostname
91+
func getHostName() string {
92+
if host, err := os.Hostname(); err == nil {
93+
return host
94+
}
95+
return "unknown"
96+
}

0 commit comments

Comments
 (0)