forked from kelseyhightower/confd
-
Notifications
You must be signed in to change notification settings - Fork 2
/
log.go
74 lines (59 loc) · 1.77 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
/*
Package log provides support for logging to stdout and stderr.
Log entries will be logged in the following format:
timestamp hostname tag[pid]: SEVERITY Message
*/
package log
import (
"fmt"
"os"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
type ConfdFormatter struct {
}
func (c *ConfdFormatter) Format(entry *log.Entry) ([]byte, error) {
timestamp := time.Now().Format(time.RFC3339)
hostname, _ := os.Hostname()
return []byte(fmt.Sprintf("%s %s %s[%d]: %s %s\n", timestamp, hostname, tag, os.Getpid(), strings.ToUpper(entry.Level.String()), entry.Message)), nil
}
// tag represents the application name generating the log message. The tag
// string will appear in all log entires.
var tag string
func init() {
tag = os.Args[0]
log.SetFormatter(&ConfdFormatter{})
}
// SetTag sets the tag.
func SetTag(t string) {
tag = t
}
// SetLevel sets the log level. Valid levels are panic, fatal, error, warn, info and debug.
func SetLevel(level string) {
lvl, err := log.ParseLevel(level)
if err != nil {
Fatal(fmt.Sprintf(`not a valid level: "%s"`, level))
}
log.SetLevel(lvl)
}
// Debug logs a message with severity DEBUG.
func Debug(format string, v ...interface{}) {
log.Debug(fmt.Sprintf(format, v...))
}
// Error logs a message with severity ERROR.
func Error(format string, v ...interface{}) {
log.Error(fmt.Sprintf(format, v...))
}
// Fatal logs a message with severity ERROR followed by a call to os.Exit().
func Fatal(format string, v ...interface{}) {
log.Fatal(fmt.Sprintf(format, v...))
}
// Info logs a message with severity INFO.
func Info(format string, v ...interface{}) {
log.Info(fmt.Sprintf(format, v...))
}
// Warning logs a message with severity WARNING.
func Warning(format string, v ...interface{}) {
log.Warning(fmt.Sprintf(format, v...))
}