Debian Codegraph @

logger/logger.go

raw
1 /*
2 Copyright 2013-2014 Canonical Ltd.
3
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License version 3, as published
6 by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranties of
10 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 // Package logger defines a simple logger API with level of logging control.
18 package logger
19
20 import (
21 "fmt"
22 "io"
23 "log"
24 "os"
25 "runtime"
26
27 "gitlab.com/ubports/development/core/lomiri-push-service/config"
28 )
29
30 // Logger is a simple logger interface with logging at levels.
31 type Logger interface {
32 // Re-expose base Output for logging events.
33 Output(calldept int, s string) error
34 // Errorf logs an error.
35 Errorf(format string, v ...interface{})
36 // Fatalf logs an error and exits the program with os.Exit(1).
37 Fatalf(format string, v ...interface{})
38 // PanicStackf logs an error message and a stacktrace, for use
39 // in panic recovery.
40 PanicStackf(format string, v ...interface{})
41 // Infof logs an info message.
42 Infof(format string, v ...interface{})
43 // Debugf logs a debug message.
44 Debugf(format string, v ...interface{})
45 }
46
47 type simpleLogger struct {
48 outputFunc func(calldepth int, s string) error
49 nlevel int
50 }
51
52 const (
53 calldepthBase = 3
54 lError = iota
55 lInfo
56 lDebug
57 )
58
59 var levelToNLevel = map[string]int{
60 "error": lError,
61 "info": lInfo,
62 "debug": lDebug,
63 }
64
65 // MinimalLogger is the minimal interface required to build a simple logger.
66 type MinimalLogger interface {
67 Output(calldepth int, s string) error
68 }
69
70 // NewSimpleLoggerFromMinimalLogger creates a logger logging only up
71 // to the given level. The level can be, in order: "error", "info",
72 // "debug". It takes a value just implementing stlib Logger.Output().
73 func NewSimpleLoggerFromMinimalLogger(minLog MinimalLogger, level string) Logger {
74 nlevel := levelToNLevel[level]
75 return &simpleLogger{
76 minLog.Output,
77 nlevel,
78 }
79 }
80
81 // NewSimpleLogger creates a logger logging only up to the given
82 // level. The level can be, in order: "error", "info", "debug". It takes an
83 // io.Writer.
84 func NewSimpleLogger(w io.Writer, level string) Logger {
85 flags := log.Ldate | log.Ltime | log.Lmicroseconds
86 if levelToNLevel[level] >= lDebug {
87 flags = flags | log.Lshortfile
88 }
89 return NewSimpleLoggerFromMinimalLogger(
90 log.New(w, "", flags),
91 level,
92 )
93 }
94
95 func (lg *simpleLogger) Output(calldepth int, s string) error {
96 return lg.outputFunc(calldepth+2, s)
97 }
98
99 func (lg *simpleLogger) Errorf(format string, v ...interface{}) {
100 lg.outputFunc(calldepthBase, fmt.Sprintf("ERROR "+format, v...))
101 }
102
103 var osExit = os.Exit // for testing
104
105 func (lg *simpleLogger) Fatalf(format string, v ...interface{}) {
106 lg.outputFunc(calldepthBase, fmt.Sprintf("ERROR "+format, v...))
107 osExit(1)
108 }
109
110 func (lg *simpleLogger) PanicStackf(format string, v ...interface{}) {
111 msg := fmt.Sprintf(format, v...)
112 stack := make([]byte, 8*1024) // Stack writes less but doesn't fail
113 stackWritten := runtime.Stack(stack, false)
114 stack = stack[:stackWritten]
115 lg.outputFunc(calldepthBase, fmt.Sprintf("ERROR(PANIC) %s:\n%s", msg, stack))
116 }
117
118 func (lg *simpleLogger) Infof(format string, v ...interface{}) {
119 if lg.nlevel >= lInfo {
120 lg.outputFunc(calldepthBase, fmt.Sprintf("INFO "+format, v...))
121 }
122 }
123
124 func (lg *simpleLogger) Debugf(format string, v ...interface{}) {
125 if lg.nlevel >= lDebug {
126 lg.outputFunc(calldepthBase, fmt.Sprintf("DEBUG "+format, v...))
127 }
128 }
129
130 // config bits
131
132 // ConfigLogLevel can hold a log level in a configuration struct.
133 type ConfigLogLevel string
134
135 func (cll *ConfigLogLevel) ConfigFromJSONString() {}
136
137 func (cll *ConfigLogLevel) UnmarshalJSON(b []byte) error {
138 return config.UnmarshalJSONViaString(cll, b)
139 }
140
141 func (cll *ConfigLogLevel) SetFromString(enc string) error {
142 _, ok := levelToNLevel[enc]
143 if !ok {
144 return fmt.Errorf("not a log level: %s", enc)
145 }
146 *cll = ConfigLogLevel(enc)
147 return nil
148 }
149
150 // Level returns the log level string held in cll.
151 func (cll ConfigLogLevel) Level() string {
152 return string(cll)
153 }