Blame view

main.go 3.99 KB
a99c98307   Marko Tikvić   first commit
1
2
3
4
5
  package gologger
  
  import (
  	"fmt"
  	"os"
e025a8738   Marko Tikvić   platform agnostic...
6
  	"path/filepath"
98708cdaf   Marko Tikvić   new version, does...
7
  	"runtime"
3ba7b5695   Marko Tikvić   added mutex locks...
8
  	"sync"
baa4468b7   Marko Tikvić   Comments go befor...
9
  	"time"
a99c98307   Marko Tikvić   first commit
10
  )
3ba7b5695   Marko Tikvić   added mutex locks...
11
12
  var muEv = &sync.Mutex{}
  var muEr = &sync.Mutex{}
baa4468b7   Marko Tikvić   Comments go befor...
13
14
15
16
17
  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   Marko Tikvić   first commit
18
19
20
21
22
23
24
25
26
27
  
  const ErrDirName = "error-logs"
  const EvtDirName = "event-logs"
  
  type Logger struct {
  	eventf        *os.File
  	eventFileName string
  	errorf        *os.File
  	errorFileName string
  }
98708cdaf   Marko Tikvić   new version, does...
28
  var logger Logger
a99c98307   Marko Tikvić   first commit
29

98708cdaf   Marko Tikvić   new version, does...
30
  func Init(eventFileName, errorFileName string) error {
ce403bd83   Marko Tikvić   changed timestamp...
31
  	timestamp := "_" + time.Now().Format(time.RFC3339)
a99c98307   Marko Tikvić   first commit
32
33
  
  	// create directories to keep log files
e025a8738   Marko Tikvić   platform agnostic...
34
  	err := os.Mkdir(EvtDirName, os.ModePerm)
a99c98307   Marko Tikvić   first commit
35
36
37
38
  	if err != nil {
  		if !os.IsExist(err) {
  			fmt.Printf("logger: mkdir: couldn't create event log directory
  ")
98708cdaf   Marko Tikvić   new version, does...
39
  			return err
a99c98307   Marko Tikvić   first commit
40
41
42
43
44
45
46
47
  		}
  	}
  
  	err = os.Mkdir(ErrDirName, os.ModePerm)
  	if err != nil {
  		if !os.IsExist(err) {
  			fmt.Printf("logger: new: couldn't create error log directory
  ")
98708cdaf   Marko Tikvić   new version, does...
48
  			return err
a99c98307   Marko Tikvić   first commit
49
50
  		}
  	}
e025a8738   Marko Tikvić   platform agnostic...
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  	// create files
  	logger.eventFileName = eventFileName
  	logger.errorFileName = errorFileName
  
  	path := filepath.Join(EvtDirName, eventFileName+timestamp)
  	logger.eventf, err = os.Create(path)
  
  	if err != nil {
  		fmt.Printf("logger: new: couldn't create event log file
  ")
  		return err
  	}
  
  	path = filepath.Join(ErrDirName, errorFileName+timestamp)
  	logger.errorf, err = os.Create(path)
  
  	if err != nil {
  		fmt.Printf("logger: new: couldn't create error log file
  ")
  		return err
  	}
98708cdaf   Marko Tikvić   new version, does...
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
119
120
  	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   Marko Tikvić   first commit
121
  }
98708cdaf   Marko Tikvić   new version, does...
122
  func Close() {
a99c98307   Marko Tikvić   first commit
123
  	// close files
98708cdaf   Marko Tikvić   new version, does...
124
  	err := logger.eventf.Close()
a99c98307   Marko Tikvić   first commit
125
126
127
128
  	if err != nil {
  		fmt.Printf("logger: on exit: couldn't close event log file
  ")
  	}
98708cdaf   Marko Tikvić   new version, does...
129
  	err = logger.errorf.Close()
a99c98307   Marko Tikvić   first commit
130
131
132
133
134
135
136
  	if err != nil {
  		fmt.Printf("logger: on exit: couldn't close error log file
  ")
  	}
  }
  
  func (l *Logger) splitEventLog() {
ce403bd83   Marko Tikvić   changed timestamp...
137
  	timestamp := "_" + time.Now().Format(time.RFC3339)
a99c98307   Marko Tikvić   first commit
138

a99c98307   Marko Tikvić   first commit
139
140
141
142
143
144
145
146
147
148
149
  	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
e025a8738   Marko Tikvić   platform agnostic...
150
151
  		path := filepath.Join(EvtDirName, l.eventFileName+timestamp)
  		l.eventf, errnew = os.Create(path)
a99c98307   Marko Tikvić   first commit
152
153
154
155
156
157
158
159
160
  
  		if errnew != nil {
  			fmt.Printf("logger: split: couldn't create event log file
  ")
  		}
  	}
  }
  
  func (l *Logger) splitErrorLog() {
ce403bd83   Marko Tikvić   changed timestamp...
161
  	timestamp := "_" + time.Now().Format(time.RFC3339)
a99c98307   Marko Tikvić   first commit
162
163
164
165
166
167
168
169
170
171
172
173
  
  	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
e025a8738   Marko Tikvić   platform agnostic...
174
175
  		path := filepath.Join(ErrDirName, l.errorFileName+timestamp)
  		l.errorf, errnew = os.Create(path)
a99c98307   Marko Tikvić   first commit
176
177
178
179
180
181
182
  
  		if errnew != nil {
  			fmt.Printf("logger: split: couldn't create error log file
  ")
  		}
  	}
  }