Blame view

gologger.go 3.2 KB
a99c98307   Marko Tikvić   first commit
1
2
3
4
5
6
  package gologger
  
  import (
  	"fmt"
  	"os"
  	"time"
3ba7b5695   Marko Tikvić   added mutex locks...
7
  	"sync"
a99c98307   Marko Tikvić   first commit
8
  )
3ba7b5695   Marko Tikvić   added mutex locks...
9
10
  var muEv = &sync.Mutex{}
  var muEr = &sync.Mutex{}
a99c98307   Marko Tikvić   first commit
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  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
  
  const ErrDirName = "error-logs"
  const EvtDirName = "event-logs"
  
  type Logger struct {
  	eventf        *os.File
  	eventFileName string
  	errorf        *os.File
  	errorFileName string
  }
  
  func NewLogger(eventFileName, errorFileName string) (*Logger, error) {
  	var l Logger
  	var err error
ce403bd83   Marko Tikvić   changed timestamp...
30
  	timestamp := "_" + time.Now().Format(time.RFC3339)
a99c98307   Marko Tikvić   first commit
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
  
  	// create files
  	l.eventFileName = eventFileName
  	l.errorFileName = errorFileName
  
  	l.eventf, err = os.Create("./" + EvtDirName + "/" +
  		eventFileName + timestamp + ".txt")
  
  	if err != nil {
  		fmt.Printf("logger: new: couldn't create event log file
  ")
  		return &l, err
  	}
  
  	l.errorf, err = os.Create("./" + ErrDirName + "/" +
  		errorFileName + timestamp + ".txt")
  
  	if err != nil {
  		fmt.Printf("logger: new: couldn't create error log file
  ")
  		return &l, err
  	}
  
  	// 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
  ")
  			return &l, err
  		}
  	}
  
  	err = os.Mkdir(ErrDirName, os.ModePerm)
  	if err != nil {
  		if !os.IsExist(err) {
  			fmt.Printf("logger: new: couldn't create error log directory
  ")
  			return &l, err
  		}
  	}
  
  	return &l, nil
  }
  
  func (l *Logger) Close() {
  	// close files
  	err := l.eventf.Close()
  	if err != nil {
  		fmt.Printf("logger: on exit: couldn't close event log file
  ")
  	}
  
  	err = l.errorf.Close()
  	if err != nil {
  		fmt.Printf("logger: on exit: couldn't close error log file
  ")
  	}
  }
  
  func (l *Logger) splitEventLog() {
ce403bd83   Marko Tikvić   changed timestamp...
92
  	timestamp := "_" + time.Now().Format(time.RFC3339)
a99c98307   Marko Tikvić   first commit
93

a99c98307   Marko Tikvić   first commit
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  	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   Marko Tikvić   changed timestamp...
116
  	timestamp := "_" + time.Now().Format(time.RFC3339)
a99c98307   Marko Tikvić   first commit
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  
  	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
  ")
  		}
  	}
  }
  
  func (l *Logger) LogEvent(event string) {
3ba7b5695   Marko Tikvić   added mutex locks...
140
141
  	muEv.Lock()
  	defer muEv.Unlock()
ce403bd83   Marko Tikvić   changed timestamp...
142
143
  	l.eventf.WriteString(time.Now().Format(time.RFC3339) + ": " + event + "
  ")
a99c98307   Marko Tikvić   first commit
144
145
  	l.splitEventLog()
  }
7fa5b8af2   Marko Tikvić   Changed order of ...
146
  func (l *Logger) LogError(comment string, err error) {
3ba7b5695   Marko Tikvić   added mutex locks...
147
148
  	muEr.Lock()
  	defer muEr.Unlock()
7fa5b8af2   Marko Tikvić   Changed order of ...
149
150
  	l.errorf.WriteString(time.Now().Format(time.RFC3339) + ": " + err.Error() + " ::::: " + comment + "
  ")
a99c98307   Marko Tikvić   first commit
151
152
  	l.splitErrorLog()
  }