-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (50 loc) · 1.36 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"context"
"fmt"
"log"
"cloud.google.com/go/logging"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
func main() {
ctx := context.Background()
// Sets your Google Cloud Platform project ID.
projectID := ""
// Creates a client.
client, err := logging.NewClient(ctx, projectID)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
defer client.Close()
// Sets the name of the log to write to.
logName := "my-log"
logger := client.Logger(logName).StandardLogger(logging.Info)
var log *zap.Logger
config := zap.NewProductionConfig()
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.TimeKey = "timestamp"
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
config.EncoderConfig = encoderConfig
w := zapcore.AddSync(&lumberjack.Logger{
Filename: "logging.log",
MaxSize: 500, // megabytes
MaxBackups: 3,
MaxAge: 28, // days
})
writeStdout := zapcore.AddSync(CustomWriter{})
gcpLogger := zapcore.AddSync(logger.Writer())
core := zapcore.NewCore(
zapcore.NewJSONEncoder(encoderConfig),
zapcore.NewMultiWriteSyncer(w, writeStdout, gcpLogger),
zap.InfoLevel)
log = zap.New(core,
zap.AddCaller(),
zap.AddStacktrace(zap.ErrorLevel),
)
fmt.Println(111)
log.Info("Hello Zap!")
log.Warn("Beware of getting Zapped! (Pun)")
log.Error("I'm out of Zap joke!")
}