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