-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
80 lines (65 loc) · 1.46 KB
/
logger.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
/* logger.go: provides an logging framework for nrd
*
* Authors: J. Lowell Wofford <[email protected]> & Brett Holman <[email protected]>
*
* This software is open source software available under the BSD-3 license.
* Copyright (c) 2020, Triad National Security, LLC
* See LICENSE file for details.
*/
package main
import (
"log"
"os"
)
// Logging
const logPrefix = ""
//const logFlags = log.Ldate | log.Ltime
const logFlags = 0
type logLevel uint8
// logging levels
const (
ERROR logLevel = iota
WARN
INFO
DEBUG
)
var logLevelStr = map[logLevel]string{
ERROR: "ERROR",
WARN: "WARN",
INFO: "INFO",
DEBUG: "DEBUG",
}
type logger struct {
lvl logLevel
f *os.File
l *log.Logger
}
// NewLogger returns an initialized logger
func NewLogger(f *os.File, lvl logLevel) *logger {
return &logger{
lvl: lvl,
f: f,
l: log.New(f, logPrefix, logFlags),
}
}
func (l *logger) Printf(lvl logLevel, fmt string, args ...interface{}) {
if l.lvl >= lvl {
l.l.Printf(logLevelStr[lvl]+":"+fmt+"\n", args...)
}
}
func (l *logger) ERROR(fmt string, args ...interface{}) {
l.Printf(ERROR, fmt, args...)
}
func (l *logger) WARN(fmt string, args ...interface{}) {
l.Printf(WARN, fmt, args...)
}
func (l *logger) INFO(fmt string, args ...interface{}) {
l.Printf(INFO, fmt, args...)
}
func (l *logger) DEBUG(fmt string, args ...interface{}) {
l.Printf(DEBUG, fmt, args...)
}
func (l *logger) FATAL(fmt string, args ...interface{}) {
l.ERROR(fmt, args...)
os.Exit(1)
}