forked from glaslos/loggers-mapper-revel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
revel.go
128 lines (110 loc) · 3.21 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
package revel
import (
"fmt"
"runtime"
"strings"
"github.com/Vivino/go-loggers"
"github.com/Vivino/go-loggers/mappers"
"github.com/revel/revel"
)
// 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).\n")
return a
}
// LevelPrint is a Mapper method
func (l *Logger) LevelPrint(lev mappers.Level, i ...interface{}) {
i = append([]interface{}{caller(3) + " "}, i...)
getRevelLevel(lev)(fmt.Sprint(i...))
}
// LevelPrintf is a Mapper method
func (l *Logger) LevelPrintf(lev mappers.Level, format string, i ...interface{}) {
getRevelLevel(lev)(fmt.Sprintf(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)(fmt.Sprintln(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)(fmt.Sprint(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)(fmt.Sprintf(format, i...))
}
func (r *revelPostfixLogger) LevelPrintln(lev mappers.Level, i ...interface{}) {
i = append(i, r.postfix)
getRevelLevel(lev)(fmt.Sprintln(i...))
}
func getRevelLevel(lev mappers.Level) func(string, ...interface{}) {
switch lev {
case mappers.LevelDebug:
return revel.AppLog.Debug
case mappers.LevelInfo:
return revel.AppLog.Info
case mappers.LevelWarn:
return revel.AppLog.Warn
case mappers.LevelError:
return revel.AppLog.Error
case mappers.LevelFatal:
return revel.AppLog.Fatal
case mappers.LevelPanic:
return revel.AppLog.Panic
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)
}