Skip to content

Commit 1bb1a32

Browse files
committed
Initial logger
1 parent 1c672c0 commit 1bb1a32

File tree

5 files changed

+95
-1
lines changed

5 files changed

+95
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# logger
2-
Go Logger
2+
3+
Golang Logger

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/packaged/logger
2+
3+
go 1.16
4+
5+
require go.uber.org/zap v1.19.0 // indirect

google.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package logger
2+
3+
import (
4+
"go.uber.org/zap"
5+
"go.uber.org/zap/zapcore"
6+
)
7+
8+
func WithGoogleEncoding(cfg *zap.Config) {
9+
// encoder to match GCP payloads
10+
// https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry
11+
12+
cfg.Encoding = "json"
13+
cfg.EncoderConfig = zapcore.EncoderConfig{
14+
TimeKey: "timestamp",
15+
LevelKey: "severity",
16+
NameKey: "logName",
17+
CallerKey: "caller",
18+
MessageKey: "textPayload",
19+
StacktraceKey: "trace",
20+
LineEnding: zapcore.DefaultLineEnding,
21+
EncodeLevel: func(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
22+
switch l {
23+
case zapcore.DebugLevel:
24+
enc.AppendString("DEBUG")
25+
case zapcore.InfoLevel:
26+
enc.AppendString("INFO")
27+
case zapcore.WarnLevel:
28+
enc.AppendString("WARNING")
29+
case zapcore.ErrorLevel:
30+
enc.AppendString("ERROR")
31+
case zapcore.DPanicLevel:
32+
enc.AppendString("CRITICAL")
33+
case zapcore.PanicLevel:
34+
enc.AppendString("ALERT")
35+
case zapcore.FatalLevel:
36+
enc.AppendString("EMERGENCY")
37+
}
38+
},
39+
EncodeTime: zapcore.ISO8601TimeEncoder,
40+
EncodeDuration: zapcore.SecondsDurationEncoder,
41+
EncodeCaller: zapcore.ShortCallerEncoder,
42+
}
43+
}

logger.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package logger
2+
3+
import (
4+
"go.uber.org/zap"
5+
"log"
6+
)
7+
8+
func Instance(options ...Option) *zap.Logger {
9+
return InstanceWithConfig(zap.NewProductionConfig(), options...)
10+
}
11+
func DevelopmentInstance(options ...Option) *zap.Logger {
12+
return InstanceWithConfig(zap.NewDevelopmentConfig(), options...)
13+
}
14+
15+
func InstanceWithConfig(cfg zap.Config, options ...Option) *zap.Logger {
16+
// Configure with options
17+
for _, opt := range options {
18+
opt(&cfg)
19+
}
20+
21+
logger, err := cfg.Build()
22+
if err != nil {
23+
log.Println("Unable to create logger", err)
24+
}
25+
return logger
26+
27+
}

options.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package logger
2+
3+
import "go.uber.org/zap"
4+
5+
type Option func(config *zap.Config)
6+
7+
func Debug(config *zap.Config) { config.Level = zap.NewAtomicLevelAt(zap.DebugLevel) }
8+
func Info(config *zap.Config) { config.Level = zap.NewAtomicLevelAt(zap.InfoLevel) }
9+
func Warn(config *zap.Config) { config.Level = zap.NewAtomicLevelAt(zap.WarnLevel) }
10+
func Error(config *zap.Config) { config.Level = zap.NewAtomicLevelAt(zap.ErrorLevel) }
11+
func DPanic(config *zap.Config) { config.Level = zap.NewAtomicLevelAt(zap.DPanicLevel) }
12+
func Panic(config *zap.Config) { config.Level = zap.NewAtomicLevelAt(zap.PanicLevel) }
13+
func Fatal(config *zap.Config) { config.Level = zap.NewAtomicLevelAt(zap.FatalLevel) }
14+
15+
func WithConsoleEncoding(config *zap.Config) {
16+
config.Encoding = "console"
17+
config.EncoderConfig = zap.NewDevelopmentEncoderConfig()
18+
}

0 commit comments

Comments
 (0)