Commit b45f88e17f0d39b7eb32b7d10a93b9d7e187851f

Authored by Marko Tikvić
1 parent 92485819d1
Exists in master

more readable datetime format

Showing 1 changed file with 15 additions and 14 deletions   Show diff stats
... ... @@ -12,6 +12,8 @@ import (
12 12 "time"
13 13 )
14 14  
  15 +const dateTimeFormat = "2006-01-02 15:04:05"
  16 +
15 17 // Block ...
16 18 const (
17 19 MaxLogSize5MB int64 = 5 * 1024 * 1024
... ... @@ -38,25 +40,25 @@ type Logger struct {
38 40 func New(name string, maxFileSize int64) (logger *Logger, err error) {
39 41 logger = &Logger{}
40 42  
41   - logger.outputFileName = name + "-log"
  43 + logger.outputFileName = name
42 44 logger.mu = &sync.Mutex{}
43 45 logger.maxFileSize = maxFileSize
44 46  
45 47 err = os.Mkdir(logDirName, os.ModePerm)
46 48 if err != nil {
47 49 if !os.IsExist(err) {
48   - fmt.Printf("logger: mkdir: couldn't create event log directory\n")
  50 + fmt.Fprintf(os.Stderr, "gologger: couldn't create event log directory: %s\n", err.Error())
49 51 return nil, err
50 52 }
51 53 }
52 54  
53   - date := strings.Replace(time.Now().Format(time.RFC3339), ":", ".", -1)
54   - logger.outputFileName += "_" + date
  55 + date := strings.Replace(time.Now().Format(dateTimeFormat), ":", ".", -1)
  56 + logger.outputFileName += " " + date
55 57 logger.fullName = logger.outputFileName + ".txt"
56 58 path := filepath.Join(logDirName, logger.fullName)
57 59 logger.outputFile, err = os.Create(path)
58 60 if err != nil {
59   - fmt.Printf("logger: new: couldn't create event log file\n")
  61 + fmt.Fprintf(os.Stderr, "gologger: couldn't create event log file: %s\n", err.Error())
60 62 return nil, err
61 63 }
62 64  
... ... @@ -73,7 +75,7 @@ func (l *Logger) Log(format string, v ...interface{}) {
73 75 defer l.mu.Unlock()
74 76  
75 77 msg := fmt.Sprintf(format, v...)
76   - s := time.Now().Format(time.RFC3339) + ": " + msg + "\n"
  78 + s := time.Now().Format(dateTimeFormat) + ": " + msg + "\n"
77 79 if l.shouldSplit(len(s)) {
78 80 l.split()
79 81 }
... ... @@ -83,7 +85,7 @@ func (l *Logger) Log(format string, v ...interface{}) {
83 85 // Print ...
84 86 func (l *Logger) Print(format string, v ...interface{}) {
85 87 msg := fmt.Sprintf(format, v...)
86   - fmt.Printf("%s: %s\n", time.Now().Format(time.RFC3339), msg)
  88 + fmt.Printf("%s: %s\n", time.Now().Format(dateTimeFormat), msg)
87 89 }
88 90  
89 91 // PrintTrace ...
... ... @@ -91,7 +93,7 @@ func (l *Logger) PrintTrace(format string, v ...interface{}) {
91 93 _, file, line, _ := runtime.Caller(1)
92 94  
93 95 msg := fmt.Sprintf(format, v...)
94   - fmt.Printf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, msg)
  96 + fmt.Printf("%s: %s %d: %s\n", time.Now().Format(dateTimeFormat), file, line, msg)
95 97 }
96 98  
97 99 // Trace ...
... ... @@ -105,7 +107,7 @@ func (l *Logger) Trace(format string, v ...interface{}) {
105 107  
106 108 _, file, line, _ := runtime.Caller(1)
107 109 msg := fmt.Sprintf(format, v...)
108   - s := fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, msg)
  110 + s := fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(dateTimeFormat), file, line, msg)
109 111  
110 112 if l.shouldSplit(len(s)) {
111 113 l.split()
... ... @@ -129,7 +131,7 @@ func (l *Logger) LogHTTPRequest(req *http.Request, userID string) string {
129 131  
130 132 b.WriteString("Request:\n")
131 133 // CLF-like header
132   - fmt.Fprintf(&b, "%s %s %s\n", req.RemoteAddr, userID, time.Now().Format(time.RFC3339))
  134 + fmt.Fprintf(&b, "%s %s %s\n", req.RemoteAddr, userID, time.Now().Format(dateTimeFormat))
133 135  
134 136 body, err := httputil.DumpRequest(req, true)
135 137 if err != nil {
... ... @@ -171,7 +173,7 @@ func (l *Logger) Close() {
171 173  
172 174 err := l.outputFile.Close()
173 175 if err != nil {
174   - fmt.Printf("logger: on exit: couldn't close event log file\n")
  176 + fmt.Fprintf(os.Stderr, "gologger: couldn't close event log file: %s\n", err.Error())
175 177 }
176 178 }
177 179  
... ... @@ -183,7 +185,7 @@ func (l *Logger) split() {
183 185 // close old file
184 186 err := l.outputFile.Close()
185 187 if err != nil {
186   - fmt.Printf("logger: split: couldn't close event file\n")
  188 + fmt.Fprintf(os.Stderr, "gologger: split: couldn't close event file\n")
187 189 return
188 190 }
189 191  
... ... @@ -194,7 +196,7 @@ func (l *Logger) split() {
194 196 l.outputFile, errnew = os.Create(path)
195 197  
196 198 if errnew != nil {
197   - fmt.Printf("logger: split: couldn't create event log file\n")
  199 + fmt.Fprintf(os.Stderr, "gologger: couldn't create new event log file: %s\n", err.Error())
198 200 }
199 201 }
200 202  
... ... @@ -202,4 +204,3 @@ func (l *Logger) shouldSplit(nextEntrySize int) bool {
202 204 stats, _ := l.outputFile.Stat()
203 205 return int64(nextEntrySize) >= (l.maxFileSize - stats.Size())
204 206 }
205   -
... ...