/
xray_log.go
85 lines (72 loc) · 2.37 KB
/
xray_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
// Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
package xraylog
import (
"fmt"
"io"
"sync"
"time"
)
// Logger is the logging interface used by xray. fmt.Stringer is used to
// defer expensive serialization operations until the message is actually
// logged (i.e. don't bother serializing debug messages if they aren't going
// to show up).
type Logger interface {
// Log can be called concurrently from multiple goroutines so make sure
// your implemenation is goroutine safe.
Log(level LogLevel, msg fmt.Stringer)
}
// LogLevel represents the severity of a log message, where a higher value
// means more severe. The integer value should not be serialized as it is
// subject to change.
type LogLevel int
const (
LogLevelDebug LogLevel = iota + 1
LogLevelInfo
LogLevelWarn
LogLevelError
)
func (ll LogLevel) String() string {
switch ll {
case LogLevelDebug:
return "DEBUG"
case LogLevelInfo:
return "INFO"
case LogLevelWarn:
return "WARN"
case LogLevelError:
return "ERROR"
default:
return fmt.Sprintf("UNKNOWNLOGLEVEL<%d>", ll)
}
}
// NewDefaultLogger makes a Logger object that writes newline separated
// messages to w, if the level of the message is at least minLogLevel.
// The default logger synchronizes around Write() calls to the underlying
// io.Writer.
func NewDefaultLogger(w io.Writer, minLogLevel LogLevel) Logger {
return &defaultLogger{w: w, minLevel: minLogLevel}
}
type defaultLogger struct {
mu sync.Mutex
w io.Writer
minLevel LogLevel
}
func (l *defaultLogger) Log(ll LogLevel, msg fmt.Stringer) {
if ll < l.minLevel {
return
}
l.mu.Lock()
defer l.mu.Unlock()
fmt.Fprintf(l.w, "%s [%s] %s\n", time.Now().Format(time.RFC3339), ll, msg)
}
// NullLogger can be used to disable logging (pass to xray.SetLogger()).
var NullLogger = nullLogger{}
type nullLogger struct{}
func (nl nullLogger) Log(ll LogLevel, msg fmt.Stringer) {
}