This repository has been archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
log.go
137 lines (118 loc) · 4.14 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
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 mock
import (
"fmt"
"log"
"os"
"github.com/18F/e-QIP-prototype/api"
)
// LogService mock implementation of logging.
type LogService struct {
Off bool
fields api.LogFields
}
// AddField will add a field using the name and value to the logger.
func (service *LogService) AddField(name string, value interface{}) {
if _, ok := service.fields[name]; !ok {
service.fields = service.mergeFields(api.LogFields{name: value})
}
}
// Print outputs a message to the log.
func (service *LogService) Print(message string, fields api.LogFields) {
if service.Off {
log.Println(withFields(message, service.mergeFields(fields)))
}
}
// PrintError outputs a message and error to the log.
func (service *LogService) PrintError(message string, err error, fields api.LogFields) {
if service.Off {
log.Println(withFieldsAndError(message, err, service.mergeFields(fields)))
}
}
// Debug outputs a debug message to the log.
func (service *LogService) Debug(message string, fields api.LogFields) {
if !service.Off && os.Getenv(api.LogLevel) == "debug" {
log.Println(withFields(message, service.mergeFields(fields)))
}
}
// DebugError outputs a debug message and error to the log.
func (service *LogService) DebugError(message string, err error, fields api.LogFields) {
if !service.Off && os.Getenv(api.LogLevel) == "debug" {
log.Println(withFieldsAndError(message, err, service.mergeFields(fields)))
}
}
// Warn outputs a warning message to the log.
func (service *LogService) Warn(message string, fields api.LogFields) {
if !service.Off && os.Getenv(api.LogLevel) == "warning" {
log.Println(withFields(message, service.mergeFields(fields)))
}
}
// WarnError outputs a warning message and error to the log.
func (service *LogService) WarnError(message string, err error, fields api.LogFields) {
if !service.Off && os.Getenv(api.LogLevel) == "warning" {
log.Println(withFieldsAndError(message, err, service.mergeFields(fields)))
}
}
// Info outputs a informative message to the log.
func (service *LogService) Info(message string, fields api.LogFields) {
if !service.Off && os.Getenv(api.LogLevel) == "info" {
log.Println(withFields(message, service.mergeFields(fields)))
}
}
// InfoError outputs a informative message and error to the log.
func (service *LogService) InfoError(message string, err error, fields api.LogFields) {
if !service.Off && os.Getenv(api.LogLevel) == "info" {
log.Println(withFieldsAndError(message, err, service.mergeFields(fields)))
}
}
// Fatal outputs a fatal message to the log.
func (service *LogService) Fatal(message string, fields api.LogFields) {
if !service.Off && os.Getenv(api.LogLevel) == "fatal" {
log.Println(withFields(message, service.mergeFields(fields)))
}
}
// FatalError outputs a fatal message and error to the log.
func (service *LogService) FatalError(message string, err error, fields api.LogFields) {
if !service.Off && os.Getenv(api.LogLevel) == "fatal" {
log.Println(withFieldsAndError(message, err, service.mergeFields(fields)))
}
}
// Panic outputs a panic message to the log.
func (service *LogService) Panic(message string, fields api.LogFields) {
if !service.Off && os.Getenv(api.LogLevel) == "panic" {
log.Println(withFields(message, service.mergeFields(fields)))
}
}
// PanicError outputs a panic message and error to the log.
func (service *LogService) PanicError(message string, err error, fields api.LogFields) {
if !service.Off && os.Getenv(api.LogLevel) == "panic" {
log.Println(withFieldsAndError(message, err, service.mergeFields(fields)))
}
}
func (service *LogService) mergeFields(fields api.LogFields) api.LogFields {
merged := api.LogFields{}
for k, v := range service.fields {
if _, ok := merged[k]; !ok {
merged[k] = v
}
}
for k, v := range fields {
if _, ok := merged[k]; !ok {
merged[k] = v
}
}
return merged
}
func withFields(message string, fields api.LogFields) string {
s := message
for k, v := range fields {
s += fmt.Sprintf(", %s: %v", k, v)
}
return s
}
func withFieldsAndError(message string, err error, fields api.LogFields) string {
s := fmt.Sprintf("%s, error: %s", message, err)
for k, v := range fields {
s += fmt.Sprintf(", %s: %v", k, v)
}
return s
}