Add API Framework Revel Source Files
[iec.git] / src / foundation / api / revel / logger / logger.go
1 package logger
2
3 import (
4         "fmt"
5         "github.com/revel/config"
6         "time"
7 )
8
9 // The LogHandler defines the interface to handle the log records
10 type (
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
16
17                 // SetHandler updates the logger to write records to the specified handler.
18                 SetHandler(h LogHandler)
19
20                 // Set the stack depth for the logger
21                 SetStackDepth(int) MultiLogger
22
23                 // Log a message at the given level with context key/value pairs
24                 Debug(msg string, ctx ...interface{})
25
26                 // Log a message at the given level formatting message with the parameters
27                 Debugf(msg string, params ...interface{})
28
29                 // Log a message at the given level with context key/value pairs
30                 Info(msg string, ctx ...interface{})
31
32                 // Log a message at the given level formatting message with the parameters
33                 Infof(msg string, params ...interface{})
34
35                 // Log a message at the given level with context key/value pairs
36                 Warn(msg string, ctx ...interface{})
37
38                 // Log a message at the given level formatting message with the parameters
39                 Warnf(msg string, params ...interface{})
40
41                 // Log a message at the given level with context key/value pairs
42                 Error(msg string, ctx ...interface{})
43
44                 // Log a message at the given level formatting message with the parameters
45                 Errorf(msg string, params ...interface{})
46
47                 // Log a message at the given level with context key/value pairs
48                 Crit(msg string, ctx ...interface{})
49
50                 // Log a message at the given level formatting message with the parameters
51                 Critf(msg string, params ...interface{})
52
53                 // Log a message at the given level with context key/value pairs and exits
54                 Fatal(msg string, ctx ...interface{})
55
56                 // Log a message at the given level formatting message with the parameters and exits
57                 Fatalf(msg string, params ...interface{})
58
59                 // Log a message at the given level with context key/value pairs and panics
60                 Panic(msg string, ctx ...interface{})
61
62                 // Log a message at the given level formatting message with the parameters and panics
63                 Panicf(msg string, params ...interface{})
64         }
65
66         // The log handler interface
67         LogHandler interface {
68                 Log(*Record) error
69                 //log15.Handler
70         }
71
72         // The log stack handler interface
73         LogStackHandler interface {
74                 LogHandler
75                 GetStack() int
76         }
77
78         // The log handler interface which has child logs
79         ParentLogHandler interface {
80                 SetChild(handler LogHandler) LogHandler
81         }
82
83         // The log format interface
84         LogFormat interface {
85                 Format(r *Record) []byte
86         }
87
88         // The log level type
89         LogLevel int
90
91         // Used for the callback to LogFunctionMap
92         LogOptions struct {
93                 Ctx                    *config.Context
94                 ReplaceExistingHandler bool
95                 HandlerWrap            ParentLogHandler
96                 Levels                 []LogLevel
97                 ExtendedOptions        map[string]interface{}
98         }
99
100         // The log record
101         Record struct {
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
107         }
108
109         // The lazy structure to implement a function to be invoked only if needed
110         Lazy struct {
111                 Fn interface{} // the function
112         }
113
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
118         }
119 )
120
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 {
124         return formatFunc(f)
125 }
126
127 type formatFunc func(*Record) []byte
128
129 func (f formatFunc) Format(r *Record) []byte {
130         return f(r)
131 }
132 func NewRecord(message string, level LogLevel) *Record {
133         return &Record{Message: message, Context: ContextMap{}, Level: level}
134 }
135
136 const (
137         LvlCrit  LogLevel = iota // Critical
138         LvlError                 // Error
139         LvlWarn                  // Warning
140         LvlInfo                  // Information
141         LvlDebug                 // Debug
142 )
143
144 // A list of all the log levels
145 var LvlAllList = []LogLevel{LvlDebug, LvlInfo, LvlWarn, LvlError, LvlCrit}
146
147 // Implements the ParentLogHandler
148 type parentLogHandler struct {
149         setChild func(handler LogHandler) LogHandler
150 }
151
152 // Create a new parent log handler
153 func NewParentLogHandler(callBack func(child LogHandler) LogHandler) ParentLogHandler {
154         return &parentLogHandler{callBack}
155 }
156
157 // Sets the child of the log handler
158 func (p *parentLogHandler) SetChild(child LogHandler) LogHandler {
159         return p.setChild(child)
160 }
161
162 // Create a new log options
163 func NewLogOptions(cfg *config.Context, replaceHandler bool, phandler ParentLogHandler, lvl ...LogLevel) (logOptions *LogOptions) {
164         logOptions = &LogOptions{
165                 Ctx: cfg,
166                 ReplaceExistingHandler: replaceHandler,
167                 HandlerWrap:            phandler,
168                 Levels:                 lvl,
169                 ExtendedOptions:        map[string]interface{}{},
170         }
171         return
172 }
173
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]
178         }
179 }
180
181 // Gets a string option with default
182 func (l *LogOptions) GetStringDefault(option, value string) string {
183         if v, found := l.ExtendedOptions[option]; found {
184                 return v.(string)
185         }
186         return value
187 }
188
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 {
192                 return v.(int)
193         }
194         return value
195 }
196
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 {
200                 return v.(bool)
201         }
202         return value
203 }