Commit 327c98634f9c72315b9b79776193f0466037915c

Authored by Marko Tikvić
1 parent 2f4b177f08
Exists in master

added Print() for console output

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