This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathlogger.go
148 lines (123 loc) · 3.24 KB
/
logger.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2015 The Tango Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tango
import (
"io"
"time"
"gitea.com/lunny/log"
)
// Logger defines the logger interface for tango use
type Logger interface {
Debugf(format string, v ...interface{})
Debug(v ...interface{})
Infof(format string, v ...interface{})
Info(v ...interface{})
Warnf(format string, v ...interface{})
Warn(v ...interface{})
Errorf(format string, v ...interface{})
Error(v ...interface{})
}
// CompositeLogger defines a composite loggers
type CompositeLogger struct {
loggers []Logger
}
// NewCompositeLogger creates a composite loggers
func NewCompositeLogger(logs ...Logger) Logger {
return &CompositeLogger{loggers: logs}
}
// Debugf implementes Logger interface
func (l *CompositeLogger) Debugf(format string, v ...interface{}) {
for _, log := range l.loggers {
log.Debugf(format, v...)
}
}
// Debug implementes Logger interface
func (l *CompositeLogger) Debug(v ...interface{}) {
for _, log := range l.loggers {
log.Debug(v...)
}
}
// Infof implementes Logger interface
func (l *CompositeLogger) Infof(format string, v ...interface{}) {
for _, log := range l.loggers {
log.Infof(format, v...)
}
}
// Info implementes Logger interface
func (l *CompositeLogger) Info(v ...interface{}) {
for _, log := range l.loggers {
log.Info(v...)
}
}
// Warnf implementes Logger interface
func (l *CompositeLogger) Warnf(format string, v ...interface{}) {
for _, log := range l.loggers {
log.Warnf(format, v...)
}
}
// Warn implementes Logger interface
func (l *CompositeLogger) Warn(v ...interface{}) {
for _, log := range l.loggers {
log.Warn(v...)
}
}
// Errorf implementes Logger interface
func (l *CompositeLogger) Errorf(format string, v ...interface{}) {
for _, log := range l.loggers {
log.Errorf(format, v...)
}
}
// Error implementes Logger interface
func (l *CompositeLogger) Error(v ...interface{}) {
for _, log := range l.loggers {
log.Error(v...)
}
}
// NewLogger use the default logger with special writer
func NewLogger(out io.Writer) Logger {
l := log.New(out, "[tango] ", log.Ldefault())
l.SetOutputLevel(log.Ldebug)
return l
}
// LogInterface defines logger interface to inject logger to struct
type LogInterface interface {
SetLogger(Logger)
}
// Log implementes LogInterface
type Log struct {
Logger
}
// SetLogger implementes LogInterface
func (l *Log) SetLogger(log Logger) {
l.Logger = log
}
// Logging returns handler to log informations
func Logging() HandlerFunc {
return func(ctx *Context) {
start := time.Now()
p := ctx.Req().URL.Path
if len(ctx.Req().URL.RawQuery) > 0 {
p = p + "?" + ctx.Req().URL.RawQuery
}
ctx.Debug("Started", ctx.Req().Method, p, "for", ctx.IP())
if action := ctx.Action(); action != nil {
if l, ok := action.(LogInterface); ok {
l.SetLogger(ctx.Logger)
}
}
ctx.Next()
if !ctx.Written() {
if ctx.Result == nil {
ctx.Result = NotFound()
}
ctx.HandleError()
}
statusCode := ctx.Status()
if statusCode >= 200 && statusCode < 400 {
ctx.Info(ctx.Req().Method, statusCode, time.Since(start), p)
} else {
ctx.Error(ctx.Req().Method, statusCode, time.Since(start), p, ctx.Result)
}
}
}