/
lib_with_sl_main.go
107 lines (96 loc) · 2.22 KB
/
lib_with_sl_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
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
101
102
103
104
105
106
107
// Copyright 2011 Cloud Instruments Co. Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
log "github.com/cihub/seelog"
library "github.com/cihub/seelog-examples/library"
"fmt"
)
func loadAppConfig() {
appConfig := `
<seelog type="sync">
<outputs formatid="app">
<console />
</outputs>
<formats>
<format id="app" format="app: [%LEV] %Msg%n" />
</formats>
</seelog>
`
logger, err := log.LoggerFromConfigAsBytes([]byte(appConfig))
if err != nil {
fmt.Println(err)
return
}
log.ReplaceLogger(logger)
}
func calcF2() {
x := 1
y := 2
log.Debug("Calculating F")
result := library.CalculateF(x,y)
log.Debugf("Got F = %d", result)
}
// Same config for both library and app
func sameOutputConfig() {
libConfig := `
<seelog type="sync">
<outputs formatid="library">
<console />
</outputs>
<formats>
<format id="library" format="library + app: [%LEV] %Msg%n" />
</formats>
</seelog>
`
logger, err := log.LoggerFromConfigAsBytes([]byte(libConfig))
if err != nil {
fmt.Println(err)
return
}
log.ReplaceLogger(logger)
library.UseLogger(logger)
}
// Special config for library (app config is not changed)
func specialOutputConfig() {
libConfig := `
<seelog type="sync">
<outputs formatid="library">
<console />
</outputs>
<formats>
<format id="library" format="library: %Msg [%LEV] %n" />
</formats>
</seelog>
`
logger, err := log.LoggerFromConfigAsBytes([]byte(libConfig))
if err != nil {
fmt.Println(err)
return
}
library.UseLogger(logger)
}
func libWithSealogMain() {
defer library.FlushLog()
defer log.Flush()
loadAppConfig()
log.Info("App started")
log.Info("Config loaded")
// Disable library log
log.Info("* Disabled library log test")
library.DisableLog();
calcF2();
log.Info("* Disabled library log tested")
// Use a special logger for library
log.Info("* Special output test")
specialOutputConfig()
calcF2();
log.Info("* Special output tested")
// Use the same logger for both app and library
log.Info("* Same output test")
sameOutputConfig()
calcF2();
log.Info("* Same output tested")
log.Info("App finished")
}