forked from birkirb/loggers-mapper-revel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
revel.go
134 lines (116 loc) · 3.38 KB
/
revel.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package revel
import (
"fmt"
stdlog "log"
"runtime"
"strings"
"github.com/revel/revel"
"gopkg.in/birkirb/loggers.v1"
"gopkg.in/birkirb/loggers.v1/mappers"
)
// Logger is a Contextual logger wrapper over Revel's logger.
type Logger struct{}
// NewLogger returns a Contextual Logger for revel's internal logger.
// Note that Revel's loggers must be initialized before any logging can be made.
func NewLogger() loggers.Contextual {
var l *Logger
var a = mappers.NewContextualMap(l)
a.Info("Now using Revel's logger package (via loggers/mappers/revel).")
// Remove filename printing from revel logger.
revel.TRACE.SetFlags(stdlog.Ldate | stdlog.Ltime)
revel.INFO.SetFlags(stdlog.Ldate | stdlog.Ltime)
revel.WARN.SetFlags(stdlog.Ldate | stdlog.Ltime)
revel.ERROR.SetFlags(stdlog.Ldate | stdlog.Ltime)
return a
}
// LevelPrint is a Mapper method
func (l *Logger) LevelPrint(lev mappers.Level, i ...interface{}) {
i = append([]interface{}{caller(3) + " "}, i...)
getRevelLevel(lev).Print(i...)
}
// LevelPrintf is a Mapper method
func (l *Logger) LevelPrintf(lev mappers.Level, format string, i ...interface{}) {
getRevelLevel(lev).Printf(caller(3)+" "+format, i...)
}
// LevelPrintln is a Mapper method
func (l *Logger) LevelPrintln(lev mappers.Level, i ...interface{}) {
i = append([]interface{}{caller(3)}, i...)
getRevelLevel(lev).Println(i...)
}
// WithField returns an advanced logger with a pre-set field.
func (l *Logger) WithField(key string, value interface{}) loggers.Advanced {
return l.WithFields(key, value)
}
// WithFields returns an advanced logger with pre-set fields.
func (l *Logger) WithFields(fields ...interface{}) loggers.Advanced {
s := make([]string, len(fields)/2)
for i := 0; i+1 < len(fields); i = i + 2 {
key := fields[i]
value := fields[i+1]
s = append(s, fmt.Sprint(key, "=", value))
}
r := revelPostfixLogger{strings.Join(s, " ")}
return mappers.NewAdvancedMap(&r)
}
type revelPostfixLogger struct {
postfix string
}
func (r *revelPostfixLogger) LevelPrint(lev mappers.Level, i ...interface{}) {
i = append(i, r.postfix)
getRevelLevel(lev).Print(i...)
}
func (r *revelPostfixLogger) LevelPrintf(lev mappers.Level, format string, i ...interface{}) {
if len(r.postfix) > 0 {
format = format + " %s"
i = append(i, r.postfix)
}
getRevelLevel(lev).Printf(format, i...)
}
func (r *revelPostfixLogger) LevelPrintln(lev mappers.Level, i ...interface{}) {
i = append(i, r.postfix)
getRevelLevel(lev).Println(i...)
}
func getRevelLevel(lev mappers.Level) loggers.Standard {
switch lev {
case mappers.LevelDebug:
return revel.TRACE
case mappers.LevelInfo:
return revel.INFO
case mappers.LevelWarn:
return revel.WARN
case mappers.LevelError:
return revel.ERROR
case mappers.LevelFatal:
return revel.ERROR
case mappers.LevelPanic:
return revel.ERROR
default:
panic("unreachable")
}
}
// shortenFile returns the folder and file name of an absolute file path.
func shortenFile(file string) string {
short := file
foundOne := false
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
if !foundOne {
foundOne = true
continue
}
short = file[i+1:]
break
}
}
return short
}
// caller returns the funtion call line at the specified depth
// as "dir/file.go:n:
func caller(depth int) string {
_, file, line, ok := runtime.Caller(depth + 1)
if !ok {
file = "???"
line = 0
}
return fmt.Sprintf("%s:%d:", shortenFile(file), line)
}