forked from skynetservices/skynet-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_logger.go
74 lines (63 loc) · 2.06 KB
/
file_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
package skynet
import (
"fmt"
"log"
"os"
)
// FileSemanticLogger logs logging data to files... semantically!
type FileSemanticLogger struct {
log *log.Logger
}
// NewFileSemanticLogger creates a new logger with the given name that
// logs to the given filename
func NewFileSemanticLogger(name, filename string) (*FileSemanticLogger, error) {
// Open file with append permissions
flags := os.O_APPEND | os.O_WRONLY | os.O_CREATE
file, err := os.OpenFile(filename, flags, 0666)
if err != nil {
return nil, fmt.Errorf("Error opening '%v': %v", filename, err)
}
// Oddity: `file.Close()` never gets called. Everything seems to work.
fl := FileSemanticLogger{
log: log.New(file, fmt.Sprintf("%s: ", name), log.LstdFlags),
}
return &fl, nil
}
// Log uses select parts of the given payload and logs to fl.log
func (fl *FileSemanticLogger) Log(payload *LogPayload) {
// TODO: Consider using more payload fields
fl.log.Printf("%v: %s\n", payload.Level, payload.Message)
}
func (fl *FileSemanticLogger) Trace(msg string) {
// TODO: Consider using more payload fields
fl.log.Printf("%v: %s\n", TRACE, msg)
}
func (fl *FileSemanticLogger) Debug(msg string) {
// TODO: Consider using more payload fields
fl.log.Printf("%v: %s\n", DEBUG, msg)
}
func (fl *FileSemanticLogger) Info(msg string) {
// TODO: Consider using more payload fields
fl.log.Printf("%v: %s\n", INFO, msg)
}
func (fl *FileSemanticLogger) Warn(msg string) {
// TODO: Consider using more payload fields
fl.log.Printf("%v: %s\n", WARN, msg)
}
func (fl *FileSemanticLogger) Error(msg string) {
// TODO: Consider using more payload fields
fl.log.Printf("%v: %s\n", ERROR, msg)
}
// Fatal populates payload.StackTrace then panics
func (fl *FileSemanticLogger) Fatal(msg string) {
payload := NewLogPayload(FATAL, msg)
payload.SetException()
fl.Log(payload)
panic(payload)
}
// BenchmarkInfo currently does nothing but should measure the time
// it takes to execute `f` based on the log level
func (fl *FileSemanticLogger) BenchmarkInfo(level LogLevel, msg string,
f func(logger SemanticLogger)) {
// TODO: Implement
}