Commit 901a92955ac1cc968ec359d5b8a3f7ed40f2933a
1 parent
829723543a
Exists in
master
added new line after tracing for more clarity in logs
Showing
1 changed file
with
7 additions
and
7 deletions
Show diff stats
main.go
1 | package gologger | 1 | package gologger |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "bytes" | 4 | "bytes" |
5 | "encoding/json" | 5 | "encoding/json" |
6 | "fmt" | 6 | "fmt" |
7 | "os" | 7 | "os" |
8 | "path/filepath" | 8 | "path/filepath" |
9 | "strings" | 9 | "strings" |
10 | "sync" | 10 | "sync" |
11 | "time" | 11 | "time" |
12 | ) | 12 | ) |
13 | 13 | ||
14 | const dateTimeFormat = "2006-01-02 15:04:05" | 14 | const dateTimeFormat = "2006-01-02 15:04:05" |
15 | const thisFile = "" | 15 | const thisFile = "" |
16 | 16 | ||
17 | // Block ... | 17 | // Block ... |
18 | const ( | 18 | const ( |
19 | MaxLogSize5MB int64 = 5 * 1024 * 1024 | 19 | MaxLogSize5MB int64 = 5 * 1024 * 1024 |
20 | MaxLogSize1MB int64 = 1 * 1024 * 1024 | 20 | MaxLogSize1MB int64 = 1 * 1024 * 1024 |
21 | MaxLogSize500KB int64 = 500 * 1024 | 21 | MaxLogSize500KB int64 = 500 * 1024 |
22 | MaxLogSize100KB int64 = 100 * 1024 | 22 | MaxLogSize100KB int64 = 100 * 1024 |
23 | MaxLogSize512B int64 = 512 | 23 | MaxLogSize512B int64 = 512 |
24 | ) | 24 | ) |
25 | 25 | ||
26 | // Logger ... | 26 | // Logger ... |
27 | type Logger struct { | 27 | type Logger struct { |
28 | mu *sync.Mutex | 28 | mu *sync.Mutex |
29 | outputFile *os.File | 29 | outputFile *os.File |
30 | 30 | ||
31 | outputFileName string | 31 | outputFileName string |
32 | fullName string | 32 | fullName string |
33 | maxFileSize int64 | 33 | maxFileSize int64 |
34 | 34 | ||
35 | splitCount int | 35 | splitCount int |
36 | 36 | ||
37 | directory string | 37 | directory string |
38 | } | 38 | } |
39 | 39 | ||
40 | // New ... | 40 | // New ... |
41 | func New(name, dir string, maxFileSize int64) (logger *Logger, err error) { | 41 | func New(name, dir string, maxFileSize int64) (logger *Logger, err error) { |
42 | logger = &Logger{} | 42 | logger = &Logger{} |
43 | 43 | ||
44 | logger.outputFileName = name | 44 | logger.outputFileName = name |
45 | logger.mu = &sync.Mutex{} | 45 | logger.mu = &sync.Mutex{} |
46 | logger.maxFileSize = maxFileSize | 46 | logger.maxFileSize = maxFileSize |
47 | logger.directory = dir | 47 | logger.directory = dir |
48 | 48 | ||
49 | err = os.Mkdir(dir, os.ModePerm) | 49 | err = os.Mkdir(dir, os.ModePerm) |
50 | if err != nil { | 50 | if err != nil { |
51 | if !os.IsExist(err) { | 51 | if !os.IsExist(err) { |
52 | return nil, err | 52 | return nil, err |
53 | } | 53 | } |
54 | } | 54 | } |
55 | 55 | ||
56 | date := strings.Replace(time.Now().Format(dateTimeFormat), ":", ".", -1) | 56 | date := strings.Replace(time.Now().Format(dateTimeFormat), ":", ".", -1) |
57 | logger.outputFileName += " " + date | 57 | logger.outputFileName += " " + date |
58 | logger.fullName = logger.outputFileName + ".txt" | 58 | logger.fullName = logger.outputFileName + ".txt" |
59 | path := filepath.Join(dir, logger.fullName) | 59 | path := filepath.Join(dir, logger.fullName) |
60 | if logger.outputFile, err = os.Create(path); err != nil { | 60 | if logger.outputFile, err = os.Create(path); err != nil { |
61 | return nil, err | 61 | return nil, err |
62 | } | 62 | } |
63 | 63 | ||
64 | return logger, nil | 64 | return logger, nil |
65 | } | 65 | } |
66 | 66 | ||
67 | // Log ... | 67 | // Log ... |
68 | func (l *Logger) Log(format string, v ...interface{}) { | 68 | func (l *Logger) Log(format string, v ...interface{}) { |
69 | if l.outputFile == nil { | 69 | if l.outputFile == nil { |
70 | return | 70 | return |
71 | } | 71 | } |
72 | 72 | ||
73 | l.mu.Lock() | 73 | l.mu.Lock() |
74 | defer l.mu.Unlock() | 74 | defer l.mu.Unlock() |
75 | 75 | ||
76 | msg := fmt.Sprintf(format, v...) | 76 | msg := fmt.Sprintf(format, v...) |
77 | s := time.Now().Format(dateTimeFormat) + ": " + msg + "\n" | 77 | s := time.Now().Format(dateTimeFormat) + ": " + msg + "\n" |
78 | if l.shouldSplit(len(s)) { | 78 | if l.shouldSplit(len(s)) { |
79 | l.split() | 79 | l.split() |
80 | } | 80 | } |
81 | l.outputFile.WriteString(s) | 81 | l.outputFile.WriteString(s) |
82 | } | 82 | } |
83 | 83 | ||
84 | // Print ... | 84 | // Print ... |
85 | func (l *Logger) Print(format string, v ...interface{}) { | 85 | func (l *Logger) Print(format string, v ...interface{}) { |
86 | msg := fmt.Sprintf(format, v...) | 86 | msg := fmt.Sprintf(format, v...) |
87 | fmt.Printf("%s: %s\n", time.Now().Format(dateTimeFormat), msg) | 87 | fmt.Printf("%s: %s\n", time.Now().Format(dateTimeFormat), msg) |
88 | } | 88 | } |
89 | 89 | ||
90 | // PrintTrace ... | ||
91 | func (l *Logger) PrintTrace(format string, v ...interface{}) { | ||
92 | s := getTrace(format, v...) | ||
93 | fmt.Printf("%s", s) | ||
94 | } | ||
95 | |||
96 | // Trace ... | 90 | // Trace ... |
97 | func (l *Logger) Trace(format string, v ...interface{}) string { | 91 | func (l *Logger) Trace(format string, v ...interface{}) string { |
98 | if l.outputFile == nil { | 92 | if l.outputFile == nil { |
99 | return "" | 93 | return "" |
100 | } | 94 | } |
101 | 95 | ||
102 | l.mu.Lock() | 96 | l.mu.Lock() |
103 | defer l.mu.Unlock() | 97 | defer l.mu.Unlock() |
104 | 98 | ||
105 | s := getTrace(format, v...) | 99 | s := getTrace(format, v...) + "\n" |
106 | 100 | ||
107 | if l.shouldSplit(len(s)) { | 101 | if l.shouldSplit(len(s)) { |
108 | l.split() | 102 | l.split() |
109 | } | 103 | } |
110 | l.outputFile.WriteString(s) | 104 | l.outputFile.WriteString(s) |
111 | 105 | ||
112 | return s | 106 | return s |
113 | } | 107 | } |
114 | 108 | ||
109 | // PrintTrace ... | ||
110 | func (l *Logger) PrintTrace(format string, v ...interface{}) { | ||
111 | s := getTrace(format, v...) | ||
112 | fmt.Printf("%s\n", s) | ||
113 | } | ||
114 | |||
115 | // PrintAndTrace ... | 115 | // PrintAndTrace ... |
116 | func (l *Logger) PrintAndTrace(format string, v ...interface{}) { | 116 | func (l *Logger) PrintAndTrace(format string, v ...interface{}) { |
117 | t := l.Trace(format, v...) | 117 | t := l.Trace(format, v...) |
118 | fmt.Println(t) | 118 | fmt.Println(t) |
119 | } | 119 | } |
120 | 120 | ||
121 | // Close ... | 121 | // Close ... |
122 | func (l *Logger) Close() error { | 122 | func (l *Logger) Close() error { |
123 | if l.outputFile == nil { | 123 | if l.outputFile == nil { |
124 | return nil | 124 | return nil |
125 | } | 125 | } |
126 | 126 | ||
127 | return l.outputFile.Close() | 127 | return l.outputFile.Close() |
128 | } | 128 | } |
129 | 129 | ||
130 | func (l *Logger) split() error { | 130 | func (l *Logger) split() error { |
131 | if l.outputFile == nil { | 131 | if l.outputFile == nil { |
132 | return nil | 132 | return nil |
133 | } | 133 | } |
134 | 134 | ||
135 | // close old file | 135 | // close old file |
136 | err := l.outputFile.Close() | 136 | err := l.outputFile.Close() |
137 | if err != nil { | 137 | if err != nil { |
138 | return err | 138 | return err |
139 | } | 139 | } |
140 | 140 | ||
141 | // open new file | 141 | // open new file |
142 | l.splitCount++ | 142 | l.splitCount++ |
143 | path := filepath.Join(l.directory, l.outputFileName+fmt.Sprintf("(%d)", l.splitCount)+".txt") | 143 | path := filepath.Join(l.directory, l.outputFileName+fmt.Sprintf("(%d)", l.splitCount)+".txt") |
144 | l.outputFile, err = os.Create(path) | 144 | l.outputFile, err = os.Create(path) |
145 | 145 | ||
146 | return err | 146 | return err |
147 | } | 147 | } |
148 | 148 | ||
149 | func (l *Logger) shouldSplit(nextEntrySize int) bool { | 149 | func (l *Logger) shouldSplit(nextEntrySize int) bool { |
150 | stats, _ := l.outputFile.Stat() | 150 | stats, _ := l.outputFile.Stat() |
151 | return int64(nextEntrySize) >= (l.maxFileSize - stats.Size()) | 151 | return int64(nextEntrySize) >= (l.maxFileSize - stats.Size()) |
152 | } | 152 | } |
153 | 153 | ||
154 | func printJSON(in []byte) (out []byte, err error) { | 154 | func printJSON(in []byte) (out []byte, err error) { |
155 | var buf bytes.Buffer | 155 | var buf bytes.Buffer |
156 | err = json.Indent(&buf, in, "", " ") | 156 | err = json.Indent(&buf, in, "", " ") |
157 | return buf.Bytes(), err | 157 | return buf.Bytes(), err |
158 | } | 158 | } |