-
Notifications
You must be signed in to change notification settings - Fork 6
/
logger.go
170 lines (149 loc) · 2.78 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package banjo
import (
"bytes"
"fmt"
"os"
"strings"
"time"
)
// Logger struct
//
// Provides logging with timestamps and different logging levels
// With saving all messages to log file
//
type Logger struct {
filePath string
}
// CreateLogger function
//
// Returns Logger with default filePath
//
func CreateLogger() Logger {
path := os.Getenv("BANJO_LOG")
if path == "" {
path = "banjo_out.log"
}
return Logger{filePath: path}
}
// Info function
//
// Log message with log level INFO
//
// Params:
// - message {string}
//
// Response:
// - None
//
func (logger Logger) Info(message string) {
logger.log("INFO", message)
}
// Critical function
//
// Log message with log level CRITICAL
//
// Params:
// - message {string}
//
// Response:
// - None
//
func (logger Logger) Critical(message string) {
logger.log("CRITICAL", message)
}
// Warning function
//
// Log message with log level WARNING
//
// Params:
// - message {string}
//
// Response:
// - None
//
func (logger Logger) Warning(message string) {
logger.log("WARNING", message)
}
// Error function
//
// Log message with log level ERROR
//
// Params:
// - message {string}
//
// Response:
// - None
//
func (logger Logger) Error(message string) {
logger.log("ERROR", message)
}
// log function
//
// Puts logging information to file and to io
//
// Params:
// - logLevel {string} Message Log Level
// - message {string} Message to log
//
// Response:
// - None
//
func (logger Logger) log(level string, message string) {
logLine := formatMessage(level, message)
err := logger.writeToLogFile(logLine + "\n")
if err != nil {
str := fmt.Sprintf("Error while trying save logs to file:\nError: %v", err)
fmt.Println(formatMessage("ERROR", str))
}
fmt.Println(logLine)
}
// writeToLogFile function
//
// Saving log message to file
//
// Params:
// - line {string}
//
// Response:
// - err {Error} returns error if something went wrong
//
func (logger Logger) writeToLogFile(line string) error {
_, err := os.Stat(logger.filePath)
if os.IsNotExist(err) {
_, err := os.Create(logger.filePath)
if err != nil {
return err
}
}
f, err := os.OpenFile(logger.filePath, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
return err
}
defer f.Close()
if _, err := f.WriteString(line); err != nil {
return err
}
return nil
}
// formatMessage function
//
// Create Log line with timestampz, level and message
//
// Params:
// - level {string}
// - message {string}
//
// Response:
// - response {string}
//
func formatMessage(level string, message string) string {
var buffer bytes.Buffer
t := time.Now()
buffer.WriteString("[BANjO] ")
buffer.WriteString(t.Format(time.RFC3339))
buffer.WriteString(" | ")
buffer.WriteString(strings.ToUpper(level))
buffer.WriteString(" | ")
buffer.WriteString(message)
return buffer.String()
}