-
Notifications
You must be signed in to change notification settings - Fork 70
/
logging.go
137 lines (113 loc) · 2.96 KB
/
logging.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package config
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
log "github.com/sirupsen/logrus"
)
const (
// DefaultLogLevel is the default log level
DefaultLogLevel = "info"
// DefaultLogFile is the default log file
DefaultLogFile = "newrelic-cli.log"
)
var (
fileHookConfigured = false
Logger = log.StandardLogger()
)
func InitLogger(logger *log.Logger, logLevel string) {
logger.SetFormatter(&log.TextFormatter{
DisableLevelTruncation: true,
DisableTimestamp: true,
EnvironmentOverrideColors: true,
})
level := getLevelFromString(logLevel, log.InfoLevel)
logger.SetLevel(level)
}
func GetDefaultLogFilePath() string {
return filepath.Join(BasePath, DefaultLogFile)
}
func InitFileLogger(terminalLogLevel string) {
Logger = log.New()
InitLogger(Logger, terminalLogLevel)
if fileHookConfigured {
log.Debug("file logger already configured")
return
}
_, err := os.Stat(BasePath)
if os.IsNotExist(err) {
errDir := os.MkdirAll(BasePath, 0750)
if errDir != nil {
log.Warnf("Could not create log file folder: %s", err)
}
}
fileLoggerLevel := log.DebugLevel
if l := os.Getenv("NEW_RELIC_CLI_FILE_LOG_LEVEL"); l != "" {
fileLoggerLevel = getLevelFromString(l, log.DebugLevel)
}
fileHook, err := NewLogrusFileHook(BasePath+"/"+DefaultLogFile, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0640)
if err == nil && !fileHookConfigured {
l := log.StandardLogger()
l.SetOutput(ioutil.Discard)
l.SetLevel(fileLoggerLevel)
l.Hooks.Add(fileHook)
fileHookConfigured = true
}
}
type LogrusFileHook struct {
file *os.File
flag int
chmod os.FileMode
formatter *log.JSONFormatter
}
func NewLogrusFileHook(file string, flag int, chmod os.FileMode) (*LogrusFileHook, error) {
formatter := &log.JSONFormatter{}
logFile, err := os.OpenFile(file, flag, chmod)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to write file on filehook %v", err)
return nil, err
}
return &LogrusFileHook{logFile, flag, chmod, formatter}, err
}
func (hook *LogrusFileHook) Fire(entry *log.Entry) error {
Logger.Log(entry.Level, entry.Message)
plainformat, err := hook.formatter.Format(entry)
if err != nil {
return err
}
line := string(plainformat)
_, err = hook.file.WriteString(line)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to write file on filehook(entry.String)%v", err)
return err
}
return nil
}
func (hook *LogrusFileHook) Levels() []log.Level {
return []log.Level{
log.PanicLevel,
log.FatalLevel,
log.ErrorLevel,
log.WarnLevel,
log.InfoLevel,
log.DebugLevel,
log.TraceLevel,
}
}
func getLevelFromString(logLevel string, defaultLevel log.Level) log.Level {
switch level := strings.ToUpper(logLevel); level {
case "TRACE":
return log.TraceLevel
case "DEBUG":
return log.DebugLevel
case "WARN":
return log.WarnLevel
case "ERROR":
return log.ErrorLevel
default:
log.Debugf("log level %s could not be parsed, using default level %s", level, defaultLevel)
return defaultLevel
}
}