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