Commit 98708cdaf710163f6a048833a0409ba3ae6ae604
1 parent
132f8f2cba
Exists in
master
new version, doesn't require for user to create a gologger instance
Showing
1 changed file
with
58 additions
and
60 deletions
Show diff stats
gologger.go
1 | package gologger | 1 | package gologger |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "fmt" | 4 | "fmt" |
5 | "os" | 5 | "os" |
6 | "runtime" | ||
6 | "sync" | 7 | "sync" |
7 | "time" | 8 | "time" |
8 | "runtime" | ||
9 | ) | 9 | ) |
10 | 10 | ||
11 | var muEv = &sync.Mutex{} | 11 | var muEv = &sync.Mutex{} |
12 | var muEr = &sync.Mutex{} | 12 | var muEr = &sync.Mutex{} |
13 | 13 | ||
14 | const MaxLogFileSize5MB int64 = 5 * 1024 * 1024 | 14 | const MaxLogFileSize5MB int64 = 5 * 1024 * 1024 |
15 | const MaxLogFileSize1MB int64 = 1 * 1024 * 1024 | 15 | const MaxLogFileSize1MB int64 = 1 * 1024 * 1024 |
16 | const MaxLogFileSize500KB int64 = 500 * 1024 | 16 | const MaxLogFileSize500KB int64 = 500 * 1024 |
17 | const MaxLogFileSize100KB int64 = 100 * 1024 | 17 | const MaxLogFileSize100KB int64 = 100 * 1024 |
18 | const MaxLogFileSize512B int64 = 512 | 18 | const MaxLogFileSize512B int64 = 512 |
19 | 19 | ||
20 | const ErrDirName = "error-logs" | 20 | const ErrDirName = "error-logs" |
21 | const EvtDirName = "event-logs" | 21 | const EvtDirName = "event-logs" |
22 | 22 | ||
23 | type Logger struct { | 23 | type Logger struct { |
24 | eventf *os.File | 24 | eventf *os.File |
25 | eventFileName string | 25 | eventFileName string |
26 | errorf *os.File | 26 | errorf *os.File |
27 | errorFileName string | 27 | errorFileName string |
28 | } | 28 | } |
29 | 29 | ||
30 | func NewLogger(eventFileName, errorFileName string) (*Logger, error) { | 30 | var logger Logger |
31 | var l Logger | ||
32 | var err error | ||
33 | 31 | ||
32 | func Init(eventFileName, errorFileName string) error { | ||
34 | timestamp := "_" + time.Now().Format(time.RFC3339) | 33 | timestamp := "_" + time.Now().Format(time.RFC3339) |
34 | var err error | ||
35 | 35 | ||
36 | // create files | 36 | // create files |
37 | l.eventFileName = eventFileName | 37 | logger.eventFileName = eventFileName |
38 | l.errorFileName = errorFileName | 38 | logger.errorFileName = errorFileName |
39 | 39 | ||
40 | l.eventf, err = os.Create("./" + EvtDirName + "/" + | 40 | logger.eventf, err = os.Create("./" + EvtDirName + "/" + eventFileName + timestamp + ".txt") |
41 | eventFileName + timestamp + ".txt") | ||
42 | 41 | ||
43 | if err != nil { | 42 | if err != nil { |
44 | fmt.Printf("logger: new: couldn't create event log file\n") | 43 | fmt.Printf("logger: new: couldn't create event log file\n") |
45 | return &l, err | 44 | return err |
46 | } | 45 | } |
47 | 46 | ||
48 | l.errorf, err = os.Create("./" + ErrDirName + "/" + | 47 | logger.errorf, err = os.Create("./" + ErrDirName + "/" + errorFileName + timestamp + ".txt") |
49 | errorFileName + timestamp + ".txt") | ||
50 | 48 | ||
51 | if err != nil { | 49 | if err != nil { |
52 | fmt.Printf("logger: new: couldn't create error log file\n") | 50 | fmt.Printf("logger: new: couldn't create error log file\n") |
53 | return &l, err | 51 | return err |
54 | } | 52 | } |
55 | 53 | ||
56 | // create directories to keep log files | 54 | // create directories to keep log files |
57 | err = os.Mkdir(EvtDirName, os.ModePerm) | 55 | err = os.Mkdir(EvtDirName, os.ModePerm) |
58 | if err != nil { | 56 | if err != nil { |
59 | if !os.IsExist(err) { | 57 | if !os.IsExist(err) { |
60 | fmt.Printf("logger: mkdir: couldn't create event log directory\n") | 58 | fmt.Printf("logger: mkdir: couldn't create event log directory\n") |
61 | return &l, err | 59 | return err |
62 | } | 60 | } |
63 | } | 61 | } |
64 | 62 | ||
65 | err = os.Mkdir(ErrDirName, os.ModePerm) | 63 | err = os.Mkdir(ErrDirName, os.ModePerm) |
66 | if err != nil { | 64 | if err != nil { |
67 | if !os.IsExist(err) { | 65 | if !os.IsExist(err) { |
68 | fmt.Printf("logger: new: couldn't create error log directory\n") | 66 | fmt.Printf("logger: new: couldn't create error log directory\n") |
69 | return &l, err | 67 | return err |
70 | } | 68 | } |
71 | } | 69 | } |
72 | 70 | ||
73 | return &l, nil | 71 | return nil |
72 | } | ||
73 | |||
74 | func LogEvent(event string) { | ||
75 | muEv.Lock() | ||
76 | defer muEv.Unlock() | ||
77 | logger.eventf.WriteString(time.Now().Format(time.RFC3339) + ": " + event + "\n") | ||
78 | logger.splitEventLog() | ||
79 | } | ||
80 | |||
81 | func LogError(comment string, err error) { | ||
82 | muEr.Lock() | ||
83 | defer muEr.Unlock() | ||
84 | logger.errorf.WriteString(time.Now().Format(time.RFC3339) + ": " + comment + ": " + err.Error() + "\n") | ||
85 | logger.splitErrorLog() | ||
86 | } | ||
87 | |||
88 | func LogDetailedEvent(event string) { | ||
89 | muEv.Lock() | ||
90 | defer muEv.Unlock() | ||
91 | _, file, line, ok := runtime.Caller(1) | ||
92 | var s string | ||
93 | if ok { | ||
94 | s = fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, event) | ||
95 | } else { | ||
96 | s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + event + "\n") | ||
97 | } | ||
98 | logger.eventf.WriteString(s) | ||
99 | logger.splitEventLog() | ||
100 | } | ||
101 | |||
102 | func LogDetailedError(err error) { | ||
103 | muEr.Lock() | ||
104 | defer muEr.Unlock() | ||
105 | _, file, line, ok := runtime.Caller(1) | ||
106 | var s string | ||
107 | if ok { | ||
108 | s = fmt.Sprintf("%s %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, err.Error()) | ||
109 | } else { | ||
110 | s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + err.Error() + "\n") | ||
111 | } | ||
112 | logger.errorf.WriteString(s) | ||
113 | logger.splitErrorLog() | ||
74 | } | 114 | } |
75 | 115 | ||
76 | func (l *Logger) Close() { | 116 | func Close() { |
77 | // close files | 117 | // close files |
78 | err := l.eventf.Close() | 118 | err := logger.eventf.Close() |
79 | if err != nil { | 119 | if err != nil { |
80 | fmt.Printf("logger: on exit: couldn't close event log file\n") | 120 | fmt.Printf("logger: on exit: couldn't close event log file\n") |
81 | } | 121 | } |
82 | 122 | ||
83 | err = l.errorf.Close() | 123 | err = logger.errorf.Close() |
84 | if err != nil { | 124 | if err != nil { |
85 | fmt.Printf("logger: on exit: couldn't close error log file\n") | 125 | fmt.Printf("logger: on exit: couldn't close error log file\n") |
86 | } | 126 | } |
87 | } | 127 | } |
88 | 128 | ||
89 | func (l *Logger) splitEventLog() { | 129 | func (l *Logger) splitEventLog() { |
90 | timestamp := "_" + time.Now().Format(time.RFC3339) | 130 | timestamp := "_" + time.Now().Format(time.RFC3339) |
91 | 131 | ||
92 | evfstats, _ := l.eventf.Stat() | 132 | evfstats, _ := l.eventf.Stat() |
93 | if evfstats.Size() >= MaxLogFileSize100KB { | 133 | if evfstats.Size() >= MaxLogFileSize100KB { |
94 | // close old file | 134 | // close old file |
95 | err := l.eventf.Close() | 135 | err := l.eventf.Close() |
96 | if err != nil { | 136 | if err != nil { |
97 | fmt.Printf("logger: split: couldn't close event file\n") | 137 | fmt.Printf("logger: split: couldn't close event file\n") |
98 | return | 138 | return |
99 | } | 139 | } |
100 | // open new file | 140 | // open new file |
101 | var errnew error | 141 | var errnew error |
102 | l.eventf, errnew = os.Create("./" + EvtDirName + "/" + | 142 | l.eventf, errnew = os.Create("./" + EvtDirName + "/" + |
103 | l.eventFileName + timestamp + ".txt") | 143 | l.eventFileName + timestamp + ".txt") |
104 | 144 | ||
105 | if errnew != nil { | 145 | if errnew != nil { |
106 | fmt.Printf("logger: split: couldn't create event log file\n") | 146 | fmt.Printf("logger: split: couldn't create event log file\n") |
107 | } | 147 | } |
108 | } | 148 | } |
109 | } | 149 | } |
110 | 150 | ||
111 | func (l *Logger) splitErrorLog() { | 151 | func (l *Logger) splitErrorLog() { |
112 | timestamp := "_" + time.Now().Format(time.RFC3339) | 152 | timestamp := "_" + time.Now().Format(time.RFC3339) |
113 | 153 | ||
114 | erfstats, _ := l.errorf.Stat() | 154 | erfstats, _ := l.errorf.Stat() |
115 | if erfstats.Size() >= MaxLogFileSize100KB { | 155 | if erfstats.Size() >= MaxLogFileSize100KB { |
116 | // close old file | 156 | // close old file |
117 | err := l.errorf.Close() | 157 | err := l.errorf.Close() |
118 | if err != nil { | 158 | if err != nil { |
119 | fmt.Printf("logger: split: couldn't close error file\n") | 159 | fmt.Printf("logger: split: couldn't close error file\n") |
120 | return | 160 | return |
121 | } | 161 | } |
122 | // open new file | 162 | // open new file |
123 | var errnew error | 163 | var errnew error |
124 | l.errorf, errnew = os.Create("./" + ErrDirName + "/" + | 164 | l.errorf, errnew = os.Create("./" + ErrDirName + "/" + |
125 | l.errorFileName + timestamp + ".txt") | 165 | l.errorFileName + timestamp + ".txt") |
126 | 166 | ||
127 | if errnew != nil { | 167 | if errnew != nil { |
128 | fmt.Printf("logger: split: couldn't create error log file\n") | 168 | fmt.Printf("logger: split: couldn't create error log file\n") |
129 | } | 169 | } |