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