1
|
|
|
package zla_test |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"fmt" |
5
|
|
|
"time" |
6
|
|
|
|
7
|
|
|
"github.com/cdleo/go-e2h" |
8
|
|
|
"github.com/cdleo/go-zla" |
9
|
|
|
) |
10
|
|
|
|
11
|
|
|
func bar() error { |
12
|
|
|
return e2h.Trace(fmt.Errorf("foo")) |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
func Example_logger() { |
16
|
|
|
|
17
|
|
|
logger, _ := zla.NewLogger() |
18
|
|
|
|
19
|
|
|
//We've set this time func in order to always get the same time in the logger output |
20
|
|
|
mockedDateTime := time.Date(2021, 05, 21, 9, 00, 00, 000000000, time.UTC) |
21
|
|
|
logger.SetTimestampFunc(mockedDateTime.UTC) |
22
|
|
|
|
23
|
|
|
//By default, the logger in fully initialized with level Info and writes to StdOutput |
24
|
|
|
logger.Info("Log this!") |
25
|
|
|
|
26
|
|
|
reqId := "ad7ec2d7-d92d-4d02-a937-e0c477611ffd" |
27
|
|
|
logger.WithRefID(reqId).Error(bar(), "This is an error log!") |
28
|
|
|
|
29
|
|
|
/*logConfig := loggerImple.NewLogConfig() |
30
|
|
|
logConfig.LogLevel = "debug" |
31
|
|
|
logConfig.Rotation.Path = "./log" |
32
|
|
|
logConfig.Rotation.NamePattern = "%Y%m%d.log" |
33
|
|
|
logConfig.Rotation.RotationTimeInHours = 24 |
34
|
|
|
logConfig.Rotation.MaxAgeInDays = 30 |
35
|
|
|
writer, err := fs.NewFileRotationWriter(config.Rotation) |
36
|
|
|
if err != nil { |
37
|
|
|
return errors.TraceA(err) |
38
|
|
|
} |
39
|
|
|
l.SetLogOutput(writer)*/ |
40
|
|
|
|
41
|
|
|
// Output: |
42
|
|
|
// {"time":"2021-05-21T09:00:00Z","level":"INFO","message":"Log this!","where":"zla_example_test.go:24"} |
43
|
|
|
// {"time":"2021-05-21T09:00:00Z","ref":"ad7ec2d7-d92d-4d02-a937-e0c477611ffd","level":"ERROR","message":"This is an error log!","where":"zla_example_test.go:27","details":{"error":"foo","stack_trace":[{"func":"github.com/cdleo/go-zla_test.bar","caller":"zla_example_test.go:12"}]}} |
44
|
|
|
} |
45
|
|
|
|