Commit 98708cdaf710163f6a048833a0409ba3ae6ae604
1 parent
132f8f2cba
Exists in
master
new version, doesn't require for user to create a gologger instance
Showing
1 changed file
with
58 additions
and
60 deletions
Show diff stats
gologger.go
... | ... | @@ -3,9 +3,9 @@ package gologger |
3 | 3 | import ( |
4 | 4 | "fmt" |
5 | 5 | "os" |
6 | + "runtime" | |
6 | 7 | "sync" |
7 | 8 | "time" |
8 | - "runtime" | |
9 | 9 | ) |
10 | 10 | |
11 | 11 | var muEv = &sync.Mutex{} |
... | ... | @@ -27,30 +27,28 @@ type Logger struct { |
27 | 27 | errorFileName string |
28 | 28 | } |
29 | 29 | |
30 | -func NewLogger(eventFileName, errorFileName string) (*Logger, error) { | |
31 | - var l Logger | |
32 | - var err error | |
30 | +var logger Logger | |
33 | 31 | |
32 | +func Init(eventFileName, errorFileName string) error { | |
34 | 33 | timestamp := "_" + time.Now().Format(time.RFC3339) |
34 | + var err error | |
35 | 35 | |
36 | 36 | // create files |
37 | - l.eventFileName = eventFileName | |
38 | - l.errorFileName = errorFileName | |
37 | + logger.eventFileName = eventFileName | |
38 | + logger.errorFileName = errorFileName | |
39 | 39 | |
40 | - l.eventf, err = os.Create("./" + EvtDirName + "/" + | |
41 | - eventFileName + timestamp + ".txt") | |
40 | + logger.eventf, err = os.Create("./" + EvtDirName + "/" + eventFileName + timestamp + ".txt") | |
42 | 41 | |
43 | 42 | if err != nil { |
44 | 43 | fmt.Printf("logger: new: couldn't create event log file\n") |
45 | - return &l, err | |
44 | + return err | |
46 | 45 | } |
47 | 46 | |
48 | - l.errorf, err = os.Create("./" + ErrDirName + "/" + | |
49 | - errorFileName + timestamp + ".txt") | |
47 | + logger.errorf, err = os.Create("./" + ErrDirName + "/" + errorFileName + timestamp + ".txt") | |
50 | 48 | |
51 | 49 | if err != nil { |
52 | 50 | fmt.Printf("logger: new: couldn't create error log file\n") |
53 | - return &l, err | |
51 | + return err | |
54 | 52 | } |
55 | 53 | |
56 | 54 | // create directories to keep log files |
... | ... | @@ -58,7 +56,7 @@ func NewLogger(eventFileName, errorFileName string) (*Logger, error) { |
58 | 56 | if err != nil { |
59 | 57 | if !os.IsExist(err) { |
60 | 58 | fmt.Printf("logger: mkdir: couldn't create event log directory\n") |
61 | - return &l, err | |
59 | + return err | |
62 | 60 | } |
63 | 61 | } |
64 | 62 | |
... | ... | @@ -66,21 +64,63 @@ func NewLogger(eventFileName, errorFileName string) (*Logger, error) { |
66 | 64 | if err != nil { |
67 | 65 | if !os.IsExist(err) { |
68 | 66 | fmt.Printf("logger: new: couldn't create error log directory\n") |
69 | - return &l, err | |
67 | + return err | |
70 | 68 | } |
71 | 69 | } |
72 | 70 | |
73 | - return &l, nil | |
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() | |
74 | 114 | } |
75 | 115 | |
76 | -func (l *Logger) Close() { | |
116 | +func Close() { | |
77 | 117 | // close files |
78 | - err := l.eventf.Close() | |
118 | + err := logger.eventf.Close() | |
79 | 119 | if err != nil { |
80 | 120 | fmt.Printf("logger: on exit: couldn't close event log file\n") |
81 | 121 | } |
82 | 122 | |
83 | - err = l.errorf.Close() | |
123 | + err = logger.errorf.Close() | |
84 | 124 | if err != nil { |
85 | 125 | fmt.Printf("logger: on exit: couldn't close error log file\n") |
86 | 126 | } |
... | ... | @@ -129,45 +169,3 @@ func (l *Logger) splitErrorLog() { |
129 | 169 | } |
130 | 170 | } |
131 | 171 | } |
132 | - | |
133 | -func (l *Logger) LogEvent(event string) { | |
134 | - muEv.Lock() | |
135 | - defer muEv.Unlock() | |
136 | - l.eventf.WriteString(time.Now().Format(time.RFC3339) + ": " + event + "\n") | |
137 | - l.splitEventLog() | |
138 | -} | |
139 | - | |
140 | -func (l *Logger) LogError(comment string, err error) { | |
141 | - muEr.Lock() | |
142 | - defer muEr.Unlock() | |
143 | - l.errorf.WriteString(time.Now().Format(time.RFC3339) + ": " + comment + ": " + err.Error() + "\n") | |
144 | - l.splitErrorLog() | |
145 | -} | |
146 | - | |
147 | -func (l *Logger) LogDetailedEvent(event string) { | |
148 | - muEv.Lock() | |
149 | - defer muEv.Unlock() | |
150 | - _, file, line, ok := runtime.Caller(1) | |
151 | - var s string | |
152 | - if ok { | |
153 | - s = fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, event) | |
154 | - } else { | |
155 | - s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + event + "\n") | |
156 | - } | |
157 | - l.eventf.WriteString(s) | |
158 | - l.splitEventLog() | |
159 | -} | |
160 | - | |
161 | -func (l *Logger) LogDetailedError(err error) { | |
162 | - muEr.Lock() | |
163 | - defer muEr.Unlock() | |
164 | - _, file, line, ok := runtime.Caller(1) | |
165 | - var s string | |
166 | - if ok { | |
167 | - s = fmt.Sprintf("%s %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, err.Error()) | |
168 | - } else { | |
169 | - s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + err.Error() + "\n") | |
170 | - } | |
171 | - l.errorf.WriteString(s) | |
172 | - l.splitErrorLog() | |
173 | -} | ... | ... |