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