forked from DataDog/gohai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
50 lines (38 loc) · 1.04 KB
/
log.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
package main
import (
"bytes"
"fmt"
"os"
log "github.com/cihub/seelog"
)
const baseStdErrLogConfig = `<seelog minlevel="loglevel">
<outputs formatid="common">
<custom name="stderr" />
</outputs>
<formats>
<format id="common" format="%Msg%n"/>
</formats>
</seelog>`
type StdErrReceiver struct{}
// Implement seelog.CustomReceiver to log to stderr instead of stdout
func (sr *StdErrReceiver) ReceiveMessage(message string, level log.LogLevel, context log.LogContextInterface) error {
fmt.Fprintf(os.Stderr, message)
return nil
}
func (sr *StdErrReceiver) AfterParse(initArgs log.CustomReceiverInitArgs) error {
return nil
}
func (sr *StdErrReceiver) Flush() {}
func (sr *StdErrReceiver) Close() error {
return nil
}
func initLogging(logLevel string) error {
log.RegisterReceiver("stderr", &StdErrReceiver{})
logConfig := bytes.Replace([]byte(baseStdErrLogConfig), []byte("loglevel"), []byte(logLevel), 1)
logger, err := log.LoggerFromConfigAsBytes(logConfig)
if err != nil {
return err
}
log.ReplaceLogger(logger)
return nil
}