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