5 "github.com/revel/config"
9 // The LogHandler defines the interface to handle the log records
11 // The Multilogger reduces the number of exposed defined logging variables,
12 // and allows the output to be easily refined
13 MultiLogger interface {
14 // New returns a new Logger that has this logger's context plus the given context
15 New(ctx ...interface{}) MultiLogger
17 // SetHandler updates the logger to write records to the specified handler.
18 SetHandler(h LogHandler)
20 // Set the stack depth for the logger
21 SetStackDepth(int) MultiLogger
23 // Log a message at the given level with context key/value pairs
24 Debug(msg string, ctx ...interface{})
26 // Log a message at the given level formatting message with the parameters
27 Debugf(msg string, params ...interface{})
29 // Log a message at the given level with context key/value pairs
30 Info(msg string, ctx ...interface{})
32 // Log a message at the given level formatting message with the parameters
33 Infof(msg string, params ...interface{})
35 // Log a message at the given level with context key/value pairs
36 Warn(msg string, ctx ...interface{})
38 // Log a message at the given level formatting message with the parameters
39 Warnf(msg string, params ...interface{})
41 // Log a message at the given level with context key/value pairs
42 Error(msg string, ctx ...interface{})
44 // Log a message at the given level formatting message with the parameters
45 Errorf(msg string, params ...interface{})
47 // Log a message at the given level with context key/value pairs
48 Crit(msg string, ctx ...interface{})
50 // Log a message at the given level formatting message with the parameters
51 Critf(msg string, params ...interface{})
53 // Log a message at the given level with context key/value pairs and exits
54 Fatal(msg string, ctx ...interface{})
56 // Log a message at the given level formatting message with the parameters and exits
57 Fatalf(msg string, params ...interface{})
59 // Log a message at the given level with context key/value pairs and panics
60 Panic(msg string, ctx ...interface{})
62 // Log a message at the given level formatting message with the parameters and panics
63 Panicf(msg string, params ...interface{})
66 // The log handler interface
67 LogHandler interface {
72 // The log stack handler interface
73 LogStackHandler interface {
78 // The log handler interface which has child logs
79 ParentLogHandler interface {
80 SetChild(handler LogHandler) LogHandler
83 // The log format interface
85 Format(r *Record) []byte
91 // Used for the callback to LogFunctionMap
94 ReplaceExistingHandler bool
95 HandlerWrap ParentLogHandler
97 ExtendedOptions map[string]interface{}
102 Message string // The message
103 Time time.Time // The time
104 Level LogLevel //The level
105 Call CallStack // The call stack if built
106 Context ContextMap // The context
109 // The lazy structure to implement a function to be invoked only if needed
111 Fn interface{} // the function
114 // Currently the only requirement for the callstack is to support the Formatter method
115 // which stack.Call does so we use that
116 CallStack interface {
117 fmt.Formatter // Requirement
121 // FormatFunc returns a new Format object which uses
122 // the given function to perform record formatting.
123 func FormatFunc(f func(*Record) []byte) LogFormat {
127 type formatFunc func(*Record) []byte
129 func (f formatFunc) Format(r *Record) []byte {
132 func NewRecord(message string, level LogLevel) *Record {
133 return &Record{Message: message, Context: ContextMap{}, Level: level}
137 LvlCrit LogLevel = iota // Critical
140 LvlInfo // Information
144 // A list of all the log levels
145 var LvlAllList = []LogLevel{LvlDebug, LvlInfo, LvlWarn, LvlError, LvlCrit}
147 // Implements the ParentLogHandler
148 type parentLogHandler struct {
149 setChild func(handler LogHandler) LogHandler
152 // Create a new parent log handler
153 func NewParentLogHandler(callBack func(child LogHandler) LogHandler) ParentLogHandler {
154 return &parentLogHandler{callBack}
157 // Sets the child of the log handler
158 func (p *parentLogHandler) SetChild(child LogHandler) LogHandler {
159 return p.setChild(child)
162 // Create a new log options
163 func NewLogOptions(cfg *config.Context, replaceHandler bool, phandler ParentLogHandler, lvl ...LogLevel) (logOptions *LogOptions) {
164 logOptions = &LogOptions{
166 ReplaceExistingHandler: replaceHandler,
167 HandlerWrap: phandler,
169 ExtendedOptions: map[string]interface{}{},
174 // Assumes options will be an even number and have a string, value syntax
175 func (l *LogOptions) SetExtendedOptions(options ...interface{}) {
176 for x := 0; x < len(options); x += 2 {
177 l.ExtendedOptions[options[x].(string)] = options[x+1]
181 // Gets a string option with default
182 func (l *LogOptions) GetStringDefault(option, value string) string {
183 if v, found := l.ExtendedOptions[option]; found {
189 // Gets an int option with default
190 func (l *LogOptions) GetIntDefault(option string, value int) int {
191 if v, found := l.ExtendedOptions[option]; found {
197 // Gets a boolean option with default
198 func (l *LogOptions) GetBoolDefault(option string, value bool) bool {
199 if v, found := l.ExtendedOptions[option]; found {