-
Notifications
You must be signed in to change notification settings - Fork 1k
/
logscope.go
87 lines (70 loc) · 2.22 KB
/
logscope.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
// Copyright 2013 Joe Walnes and the websocketd team.
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package libwebsocketd
import (
"sync"
"time"
)
type LogLevel int
const (
LogDebug = iota
LogTrace
LogAccess
LogInfo
LogError
LogFatal
)
type LogFunc func(logScope *LogScope, level LogLevel, levelName string, category string, msg string, args ...interface{})
type LogScope struct {
Parent *LogScope // Parent scope
MinLevel LogLevel // Minimum log level to write out.
Mutex *sync.Mutex // Should be shared across all LogScopes that write to the same destination.
Associated []AssocPair // Additional data associated with scope
LogFunc LogFunc
}
type AssocPair struct {
Key string
Value string
}
func (l *LogScope) Associate(key string, value string) {
l.Associated = append(l.Associated, AssocPair{key, value})
}
func (l *LogScope) Debug(category string, msg string, args ...interface{}) {
l.LogFunc(l, LogDebug, "DEBUG", category, msg, args...)
}
func (l *LogScope) Trace(category string, msg string, args ...interface{}) {
l.LogFunc(l, LogTrace, "TRACE", category, msg, args...)
}
func (l *LogScope) Access(category string, msg string, args ...interface{}) {
l.LogFunc(l, LogAccess, "ACCESS", category, msg, args...)
}
func (l *LogScope) Info(category string, msg string, args ...interface{}) {
l.LogFunc(l, LogInfo, "INFO", category, msg, args...)
}
func (l *LogScope) Error(category string, msg string, args ...interface{}) {
l.LogFunc(l, LogError, "ERROR", category, msg, args...)
}
func (l *LogScope) Fatal(category string, msg string, args ...interface{}) {
l.LogFunc(l, LogFatal, "FATAL", category, msg, args...)
}
func (parent *LogScope) NewLevel(logFunc LogFunc) *LogScope {
return &LogScope{
Parent: parent,
MinLevel: parent.MinLevel,
Mutex: parent.Mutex,
Associated: make([]AssocPair, 0),
LogFunc: logFunc}
}
func RootLogScope(minLevel LogLevel, logFunc LogFunc) *LogScope {
return &LogScope{
Parent: nil,
MinLevel: minLevel,
Mutex: &sync.Mutex{},
Associated: make([]AssocPair, 0),
LogFunc: logFunc}
}
func Timestamp() string {
return time.Now().Format(time.RFC1123Z)
}