Commit aa60a45eecb42ae6216e3eb53d0a876abb64b082

Authored by Marko Tikvić
1 parent 98cf2ceebd
Exists in master

changed log file signature

Showing 1 changed file with 3 additions and 1 deletions   Show diff stats
1 package gologger 1 package gologger
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "os" 5 "os"
6 "path/filepath" 6 "path/filepath"
7 "runtime" 7 "runtime"
8 "strings"
8 "sync" 9 "sync"
9 "time" 10 "time"
10 ) 11 )
11 12
12 const ( 13 const (
13 MaxLogSize5MB int64 = 5 * 1024 * 1024 14 MaxLogSize5MB int64 = 5 * 1024 * 1024
14 MaxLogSize1MB int64 = 1 * 1024 * 1024 15 MaxLogSize1MB int64 = 1 * 1024 * 1024
15 MaxLogSize500KB int64 = 500 * 1024 16 MaxLogSize500KB int64 = 500 * 1024
16 MaxLogSize100KB int64 = 100 * 1024 17 MaxLogSize100KB int64 = 100 * 1024
17 MaxLogSize512B int64 = 512 18 MaxLogSize512B int64 = 512
18 logDirName = "log" 19 logDirName = "log"
19 ) 20 )
20 21
21 type Logger struct { 22 type Logger struct {
22 mu *sync.Mutex 23 mu *sync.Mutex
23 outputFile *os.File 24 outputFile *os.File
24 25
25 outputFileName string 26 outputFileName string
26 maxFileSize int64 27 maxFileSize int64
27 } 28 }
28 29
29 func New(name string, maxFileSize int64) (logger *Logger, err error) { 30 func New(name string, maxFileSize int64) (logger *Logger, err error) {
30 logger = &Logger{} 31 logger = &Logger{}
31 32
32 logger.outputFileName = name + "-log" 33 logger.outputFileName = name + "-log"
33 logger.mu = &sync.Mutex{} 34 logger.mu = &sync.Mutex{}
34 logger.maxFileSize = maxFileSize 35 logger.maxFileSize = maxFileSize
35 36
36 err = os.Mkdir(logDirName, os.ModePerm) 37 err = os.Mkdir(logDirName, os.ModePerm)
37 if err != nil { 38 if err != nil {
38 if !os.IsExist(err) { 39 if !os.IsExist(err) {
39 fmt.Printf("logger: mkdir: couldn't create event log directory\n") 40 fmt.Printf("logger: mkdir: couldn't create event log directory\n")
40 return nil, err 41 return nil, err
41 } 42 }
42 } 43 }
43 44
44 timestamp := "_" + time.Now().Format(time.RFC3339) 45 date := strings.Split(time.Now().Format(time.RFC3339), "T")[0]
46 timestamp := "_" + date
45 path := filepath.Join(logDirName, logger.outputFileName+timestamp+".txt") 47 path := filepath.Join(logDirName, logger.outputFileName+timestamp+".txt")
46 logger.outputFile, err = os.Create(path) 48 logger.outputFile, err = os.Create(path)
47 if err != nil { 49 if err != nil {
48 fmt.Printf("logger: new: couldn't create event log file\n") 50 fmt.Printf("logger: new: couldn't create event log file\n")
49 return nil, err 51 return nil, err
50 } 52 }
51 53
52 return logger, nil 54 return logger, nil
53 } 55 }
54 56
55 func (logger *Logger) Print(format string, v ...interface{}) { 57 func (logger *Logger) Print(format string, v ...interface{}) {
56 msg := fmt.Sprintf(format, v...) 58 msg := fmt.Sprintf(format, v...)
57 fmt.Printf(time.Now().Format(time.RFC3339) + ": " + msg + "\n") 59 fmt.Printf(time.Now().Format(time.RFC3339) + ": " + msg + "\n")
58 } 60 }
59 61
60 func (logger *Logger) Log(format string, v ...interface{}) { 62 func (logger *Logger) Log(format string, v ...interface{}) {
61 if logger.outputFile != nil { 63 if logger.outputFile != nil {
62 logger.mu.Lock() 64 logger.mu.Lock()
63 defer logger.mu.Unlock() 65 defer logger.mu.Unlock()
64 66
65 msg := fmt.Sprintf(format, v...) 67 msg := fmt.Sprintf(format, v...)
66 s := time.Now().Format(time.RFC3339) + ": " + msg + "\n" 68 s := time.Now().Format(time.RFC3339) + ": " + msg + "\n"
67 if logger.shouldSplit(len(s)) { 69 if logger.shouldSplit(len(s)) {
68 logger.split() 70 logger.split()
69 } 71 }
70 logger.outputFile.WriteString(s) 72 logger.outputFile.WriteString(s)
71 } 73 }
72 } 74 }
73 75
74 // TODO(marko) 76 // TODO(marko)
75 /* 77 /*
76 func (logger *Logger) LogHTTPRequest(req *http.Request) { 78 func (logger *Logger) LogHTTPRequest(req *http.Request) {
77 if logger.outputFile != nil { 79 if logger.outputFile != nil {
78 logger.mu.Lock() 80 logger.mu.Lock()
79 defer logger.mu.Unlock() 81 defer logger.mu.Unlock()
80 82
81 msg := fmt.Sprintf(format, v...) 83 msg := fmt.Sprintf(format, v...)
82 if logger.shouldSplit(len(s)) { 84 if logger.shouldSplit(len(s)) {
83 logger.split() 85 logger.split()
84 } 86 }
85 logger.outputFile.WriteString(time.Now().Format(time.RFC3339) + ": " + msg + "\n") 87 logger.outputFile.WriteString(time.Now().Format(time.RFC3339) + ": " + msg + "\n")
86 } 88 }
87 } 89 }
88 */ 90 */
89 91
90 func (logger *Logger) Trace(format string, v ...interface{}) { 92 func (logger *Logger) Trace(format string, v ...interface{}) {
91 if logger.outputFile != nil { 93 if logger.outputFile != nil {
92 logger.mu.Lock() 94 logger.mu.Lock()
93 defer logger.mu.Unlock() 95 defer logger.mu.Unlock()
94 _, file, line, ok := runtime.Caller(1) 96 _, file, line, ok := runtime.Caller(1)
95 97
96 s := "" 98 s := ""
97 msg := fmt.Sprintf(format, v...) 99 msg := fmt.Sprintf(format, v...)
98 if ok { 100 if ok {
99 s = fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, msg) 101 s = fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, msg)
100 } else { 102 } else {
101 s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + msg + "\n") 103 s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + msg + "\n")
102 } 104 }
103 if logger.shouldSplit(len(s)) { 105 if logger.shouldSplit(len(s)) {
104 logger.split() 106 logger.split()
105 } 107 }
106 logger.outputFile.WriteString(s) 108 logger.outputFile.WriteString(s)
107 } 109 }
108 } 110 }
109 111
110 func (logger *Logger) Close() { 112 func (logger *Logger) Close() {
111 if logger.outputFile != nil { 113 if logger.outputFile != nil {
112 err := logger.outputFile.Close() 114 err := logger.outputFile.Close()
113 if err != nil { 115 if err != nil {
114 fmt.Printf("logger: on exit: couldn't close event log file\n") 116 fmt.Printf("logger: on exit: couldn't close event log file\n")
115 } 117 }
116 } 118 }
117 } 119 }
118 120
119 func (l *Logger) split() { 121 func (l *Logger) split() {
120 timestamp := "_" + time.Now().Format(time.RFC3339) 122 timestamp := "_" + time.Now().Format(time.RFC3339)
121 // close old file 123 // close old file
122 err := l.outputFile.Close() 124 err := l.outputFile.Close()
123 if err != nil { 125 if err != nil {
124 fmt.Printf("logger: split: couldn't close event file\n") 126 fmt.Printf("logger: split: couldn't close event file\n")
125 return 127 return
126 } 128 }
127 // open new file 129 // open new file
128 var errnew error 130 var errnew error
129 path := filepath.Join(logDirName, l.outputFileName+timestamp) 131 path := filepath.Join(logDirName, l.outputFileName+timestamp)
130 l.outputFile, errnew = os.Create(path) 132 l.outputFile, errnew = os.Create(path)
131 133
132 if errnew != nil { 134 if errnew != nil {
133 fmt.Printf("logger: split: couldn't create event log file\n") 135 fmt.Printf("logger: split: couldn't create event log file\n")
134 } 136 }
135 } 137 }
136 138
137 func (l *Logger) shouldSplit(nextEntrySize int) bool { 139 func (l *Logger) shouldSplit(nextEntrySize int) bool {
138 stats, _ := l.outputFile.Stat() 140 stats, _ := l.outputFile.Stat()
139 return int64(nextEntrySize) >= (l.maxFileSize - stats.Size()) 141 return int64(nextEntrySize) >= (l.maxFileSize - stats.Size())
140 } 142 }
141 143