forked from hy05190134/PdfTextExtract
/
logging.go
127 lines (103 loc) · 2.96 KB
/
logging.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
/*
* This file is subject to the terms and conditions defined in
* file 'LICENSE.md', which is part of this source code package.
*/
package common
import (
"fmt"
"os"
"path/filepath"
"runtime"
)
type Logger interface {
Error(format string, args ...interface{})
Warning(format string, args ...interface{})
Notice(format string, args ...interface{})
Info(format string, args ...interface{})
Debug(format string, args ...interface{})
Trace(format string, args ...interface{})
}
// Dummy Logger does nothing.
type DummyLogger struct{}
func (this DummyLogger) Error(format string, args ...interface{}) {
}
func (this DummyLogger) Warning(format string, args ...interface{}) {
}
func (this DummyLogger) Notice(format string, args ...interface{}) {
}
func (this DummyLogger) Info(format string, args ...interface{}) {
}
func (this DummyLogger) Debug(format string, args ...interface{}) {
}
func (this DummyLogger) Trace(format string, args ...interface{}) {
}
// Simple Console Logger that the tests use.
type LogLevel int
const (
LogLevelTrace LogLevel = 5
LogLevelDebug LogLevel = 4
LogLevelInfo LogLevel = 3
LogLevelNotice LogLevel = 2
LogLevelWarning LogLevel = 1
LogLevelError LogLevel = 0
)
type ConsoleLogger struct {
LogLevel LogLevel
}
func NewConsoleLogger(logLevel LogLevel) *ConsoleLogger {
logger := ConsoleLogger{}
logger.LogLevel = logLevel
return &logger
}
func (this ConsoleLogger) Error(format string, args ...interface{}) {
if this.LogLevel >= LogLevelError {
prefix := "[ERROR] "
this.output(os.Stdout, prefix, format, args...)
}
}
func (this ConsoleLogger) Warning(format string, args ...interface{}) {
if this.LogLevel >= LogLevelWarning {
prefix := "[WARNING] "
this.output(os.Stdout, prefix, format, args...)
}
}
func (this ConsoleLogger) Notice(format string, args ...interface{}) {
if this.LogLevel >= LogLevelNotice {
prefix := "[NOTICE] "
this.output(os.Stdout, prefix, format, args...)
}
}
func (this ConsoleLogger) Info(format string, args ...interface{}) {
if this.LogLevel >= LogLevelInfo {
prefix := "[INFO] "
this.output(os.Stdout, prefix, format, args...)
}
}
func (this ConsoleLogger) Debug(format string, args ...interface{}) {
if this.LogLevel >= LogLevelDebug {
prefix := "[DEBUG] "
this.output(os.Stdout, prefix, format, args...)
}
}
func (this ConsoleLogger) Trace(format string, args ...interface{}) {
if this.LogLevel >= LogLevelTrace {
prefix := "[TRACE] "
this.output(os.Stdout, prefix, format, args...)
}
}
var Log Logger = DummyLogger{}
func SetLogger(logger Logger) {
Log = logger
}
// output writes `format`, `args` log message prefixed by the source file name, line and `prefix`
func (this ConsoleLogger) output(f *os.File, prefix string, format string, args ...interface{}) {
_, file, line, ok := runtime.Caller(3)
if !ok {
file = "???"
line = 0
} else {
file = filepath.Base(file)
}
src := fmt.Sprintf("%s %s:%d ", prefix, file, line) + format + "\n"
fmt.Fprintf(f, src, args...)
}