-
Notifications
You must be signed in to change notification settings - Fork 14
/
log.go
95 lines (82 loc) · 2.68 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
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
/*
* skogul, logging utilities
*
* Copyright (c) 2019 Telenor Norge AS
* Author(s):
* - hakon.solbjorg@telenor.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
package skogul
import (
"io/ioutil"
"strings"
"github.com/sirupsen/logrus"
)
var skogulLogger = logrus.New()
// ConfigureLogger sets up the logger based on calling parameters
func ConfigureLogger(requestedLoglevel string, logtimestamp bool, logFormat string) {
// Disable output from the root logger; all logging is delegated through hooks
logrus.SetLevel(logrus.TraceLevel)
logrus.SetOutput(ioutil.Discard)
loglevel := GetLogLevelFromString(requestedLoglevel)
skogulLogger.SetLevel(loglevel)
if strings.ToLower(logFormat) == "json" {
skogulLogger.SetFormatter(&logrus.JSONFormatter{})
} else {
skogulLogger.SetFormatter(&logrus.TextFormatter{
DisableTimestamp: !logtimestamp,
})
}
copyHook := LoggerCopyHook{
Writer: skogulLogger,
}
// Add a hook to a new logger which outputs the logs to stdout
logrus.AddHook(©Hook)
}
// GetLogLevelFromString returns the matching logrus.Level from a string
func GetLogLevelFromString(requestedLevel string) logrus.Level {
switch strings.ToLower(requestedLevel) {
case "e", "error":
return logrus.ErrorLevel
case "w", "warn":
return logrus.WarnLevel
case "i", "info":
return logrus.InfoLevel
case "d", "debug":
return logrus.DebugLevel
case "v", "verbose", "t", "trace":
return logrus.TraceLevel
default:
{
skogulLogger.Warnf("Invalid loglevel '%s', defaulting to 'warn'", requestedLevel)
return logrus.WarnLevel
}
}
}
// LoggerCopyHook is simply a wrapper around a logrus logger
type LoggerCopyHook struct {
Writer *logrus.Logger
}
// Fire logs the log entry onto a copied logger to stdout
func (l *LoggerCopyHook) Fire(entry *logrus.Entry) error {
skogulLogger.WithFields(entry.Data).Log(entry.Level, entry.Message)
return nil
}
// Levels returns the levels this hook will support
func (l *LoggerCopyHook) Levels() []logrus.Level {
return logrus.AllLevels
}