Commit e025a873800413e3698160cb81dae2edbc1daa6e

Authored by Marko Tikvić
1 parent 98708cdaf7
Exists in master

platform agnostic (path/filepath)

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