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