-
Notifications
You must be signed in to change notification settings - Fork 1
/
doc.go
100 lines (75 loc) · 3.29 KB
/
doc.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// This file is part of graze/golang-service
//
// Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
//
// license: https://github.com/graze/golang-service/blob/master/LICENSE
// link: https://github.com/graze/golang-service
/*
Package log provides some helpers for structured contextual logging
Handle global logging with context. Based on [logrus](https://github.com/Sirupsen/logrus)
with an option to create a global context
package (
"github.com/graze/golang-service/log"
"github.com/Sirupsen/logrus"
)
Global properties that are used whenever `log.<xxx>` is called can be set as such:
log.SetFormatter(&logrus.TextFormatter{})
log.SetOutput(os.Stderr)
log.SetLevel(log.InfoLevel)
log.Add(log.KV{"service":"super_service"}) // apply `service=super_service` to each log message
You can then log messages using the `log.` commands which will use the above configuration
log.Add(log.KV{
"app": "http-service"
})
log.With(log.KV{
"module": "request_handler",
"tag": "received_request"
"method": "GET",
"path": "/path"
}).Info("Received request");
// app="http-service" module=request_handler tag=received_request method=GET path="/path" level=info
// msg="Received request"
It is also possible to create a new logger ignoring the global configuration set above. Calling `log.New` will
create a new instance of a logger which can be passed around and used with other methods
// create a fresh context using defaults (ignores the global logger properties set above)
logger := log.New()
logger.Add(log.KV{
"module": "request_handler"
})
logger.With(log.KV{
"tag": "received_request",
"method": "GET",
"path": "/path"
}).Info("Recieved GET /path")
logger.Err(err).Error("Failed to handle input request")
// module=request_handler method=GET tag=received_request path="/path" level=error err="some error"
// msg="Failed to handler input request"
When a new logger is created, the format and output can be modified to change the how messages passed to this logger
are logged
logger := log.New()
logger.SetFormatter(&logrus.JSONFormatter{})
logger.SetLevel(log.DebugLevel)
logger.SetOutput(os.Stdout)
logger.Debug("some debug output printed")
// level=debug msg="some debug output printed"
This logger supports golang's `Context`. You can create a new context and use an existing context as such
logger := log.New()
logger.Add(log.KV{"key":"value"})
ctx := logger.NewContext(context.Background())
log.Ctx(ctx).Info("text")
// key=value level=info msg=text
You can use a logging context stored within a `context.Context` with a second local logger
logger := log.New()
logger.Add(log.KV{"key":"value"})
ctx := logger.NewContext(context.Background())
logger2 := log.New()
logger2.Add(log.KV{"key2":"value2"})
logger2.Ctx(ctx).Info("text")
// key=value key2=value2 level=info msg=text
As the logger is based on logrus you can add Hooks to each logger to send data to multiple outputs.
See: https://github.com/Sirupsen/logrus#hooks
*/
package log