forked from Juniper/jtimon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs.go
58 lines (48 loc) · 1.05 KB
/
logs.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
/*
* Copyright (c) 2018, Juniper Networks, Inc.
* All rights reserved.
*/
package main
import (
"fmt"
"io"
"log"
"os"
)
func jLog(jctx *JCtx, msg string) {
if *logMux {
log.Print(fmt.Sprintf("[%s]:%s", jctx.config.Host, msg))
return
}
if jctx.config.Log.Logger != nil {
jctx.config.Log.Logger.Printf(msg)
}
}
func logInit(jctx *JCtx) {
if *logMux {
return
}
file := jctx.config.Log.File
var out io.Writer
if *print {
out = os.Stdout
if file != "" {
log.Println("Both print and log options are used, ignoring log")
}
} else if file != "" {
var err error
out, err = os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
if err != nil {
log.Printf("Could not create log file(%s): %v\n", file, err)
}
}
if out != nil {
flags := 0
if !jctx.config.Log.CSVStats {
flags = log.LstdFlags
}
jctx.config.Log.Logger = log.New(out, "", flags)
log.Printf("logging in %s for %s:%d [periodic stats every %d seconds]\n",
jctx.config.Log.File, jctx.config.Host, jctx.config.Port, jctx.config.Log.PeriodicStats)
}
}