-
Notifications
You must be signed in to change notification settings - Fork 30
/
log.go
97 lines (77 loc) · 3 KB
/
log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"fmt"
"os"
"path"
"path/filepath"
"strconv"
"strings"
alog "github.com/cenkalti/log"
)
var log alog.Logger
// // ERROR - 2012-08-17 17:35:35 --> [VuduNonFatalException] D:\Inetpub\wwwvirtual\www.bonbon.hr\vudu_system\voodoo\URI.php: 170 [VuduNonFatalException] (The URI you submitted has disallowed characters. (g=js&121)) (404)
// // log_format := "%{color}[%{level:.4s}] %{time:15:04:05.000000} %{id:03x} %{shortfile} [%{longpkg}] %{longfunc} -> %{color:reset}%{message}"
func InitLogger() {
processName := path.Base(os.Args[0])
baseName := strings.Replace(processName, ".exe", "", -1)
logFilename := fmt.Sprintf("%s.log", baseName)
// Log levels (DEBUG, INFO, NOTICE, WARNING, ERROR, CRITICAL)
log = alog.NewLogger(processName)
log.SetLevel(alog.DEBUG) // forward all messages to handler
consoleLog := alog.NewWriterHandler(os.Stderr)
consoleLog.SetFormatter(logFormatter{})
consoleLog.SetLevel(alog.DEBUG)
consoleLog.Colorize = true
fileLog := alog.NewWriterHandler(logFile(logFilename))
fileLog.SetLevel(alog.NOTICE)
//log.SetHandler(fileLog)
multi := alog.NewMultiHandler(consoleLog, fileLog)
multi.SetFormatter(logFormatter{})
log.SetHandler(multi)
}
type logFormatter struct{}
// %.4s limitira na 4
// %-4s padda lijevo 4 spacea
// Format outputs a message like "2014-02-28 18:15:57 [example] INFO somethinfig happened"
func (f logFormatter) Format(rec *alog.Record) string {
// return fmt.Sprintf("%s %.4s [%s] %s (%s)",
return fmt.Sprintf("%s %.4s [%s] %s",
fmt.Sprint(rec.Time)[:19],
alog.LevelNames[rec.Level],
//
// XXX TODO
// u nekom trenu poceo je ispisivati full path do filea sto je kriticno necitko
// [C:\Users\Neven\Dropbox\Seven\projects\go\workspace\src\nivas.hr\chatprinter\chatprinter.exe]
// mozda je do gorc1.4
// rec.LoggerName,
//"chatprinter", // hardkodiram za log
filepath.Base(rec.Filename)+":"+strconv.Itoa(rec.Line),
rec.Message)
// rec.Message+", "+strconv.Itoa(rec.ProcessID)+", "+rec.ProcessName) // 302100, chatapp.exe
}
// original
// // Format outputs a message like "2014-02-28 18:15:57 [example] INFO somethinfig happened"
// func (f logFormatter) Format(rec *alog.Record) string {
// return fmt.Sprintf("%s %-8s [%s] %-8s %s",
// fmt.Sprint(rec.Time)[:19],
// alog.LevelNames[rec.Level],
// rec.LoggerName,
// filepath.Base(rec.Filename)+":"+strconv.Itoa(rec.Line),
// rec.Message
// }
func logFile(fileName string) *os.File {
if file, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0660); err == nil {
return file
} else {
//return nil
log.Fatalf("Cannot open log file '%s': %v\n", fileName, err)
return nil
}
}
//https://github.com/cenkalti/rain/blob/d45493ebdc299b1d88a8e3ddd6b0195f6bc1c2ac/log.go
// func recoverAndLog(l logger) {
// if err := recover(); err != nil {
// buf := make([]byte, 10000)
// l.Critical(err, "\n", string(buf[:runtime.Stack(buf, false)]))
// }
// }