Commit 64f64c09 authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

Log stub.

No related merge requests found
Showing with 153 additions and 0 deletions
+153 -0
package log
import "os"
var Default *Logger = WriterLogger(os.Stderr)
func Debug(message string) { Default.Debug(message) }
func Info(message string) { Default.Info(message) }
func Notice(message string) { Default.Notice(message) }
func Warning(message string) { Default.Warning(message) }
func Error(message string) { Default.Error(message) }
func Critical(message string) { Default.Critical(message) }
func Debugf(template string, args ...interface{}) { Default.Debugf(template, args...) }
func Infof(template string, args ...interface{}) { Default.Infof(template, args...) }
func Noticef(template string, args ...interface{}) { Default.Noticef(template, args...) }
func Warningf(template string, args ...interface{}) { Default.Warningf(template, args...) }
func Errorf(template string, args ...interface{}) { Default.Errorf(template, args...) }
func Criticalf(template string, args ...interface{}) { Default.Criticalf(template, args...) }
var ErrorHandler func(err error) = defaultErrorHandler
func defaultErrorHandler(err error) {
_, _ = os.Stderr.WriteString("Error while logging: " + err.Error() + "\n")
}
package log
import (
"fmt"
"time"
)
type Entry struct {
Timestamp time.Time
Level Level
Message string
Fields *Fields
}
func NewEntry(level Level, message string, fields *Fields) *Entry {
return &Entry{time.Now(), level, message, fields}
}
func NewEntryf(level Level, template string, args []interface{}, fields *Fields) *Entry {
return NewEntry(level, fmt.Sprintf(template, args...), fields)
}
package log
type Fields struct {
Name string
Value interface{}
previous *Fields
}
func (f *Fields) With(name string, value interface{}) *Fields {
return &Fields{name, value, f}
}
func (f *Fields) ForEach(do func(string, interface{}) bool) {
for i := f; i != nil && do(i.Name, i.Value); i = i.previous {
}
}
package log
type Level byte
const (
DEBUG Level = iota
INFO
NOTICE
WARNING
ERROR
CRITICAL
)
func (l Level) String() string {
switch l {
case DEBUG:
return "DEBUG"
case INFO:
return "INFO"
case NOTICE:
return "NOTICE"
case WARNING:
return "WARNING"
case ERROR:
return "ERROR"
case CRITICAL:
return "CRITICAL"
}
panic("unknown log level")
}
package log
type logger interface {
Log(*Entry)
}
type Logger struct {
logger
Fields *Fields
}
func (l *Logger) WithField(name string, value interface{}) *Logger {
return &Logger{l, l.Fields.With(name, value)}
}
func (l *Logger) Debug(message string) { l.Log(NewEntry(DEBUG, message, l.Fields)) }
func (l *Logger) Info(message string) { l.Log(NewEntry(INFO, message, l.Fields)) }
func (l *Logger) Notice(message string) { l.Log(NewEntry(NOTICE, message, l.Fields)) }
func (l *Logger) Warning(message string) { l.Log(NewEntry(WARNING, message, l.Fields)) }
func (l *Logger) Error(message string) { l.Log(NewEntry(ERROR, message, l.Fields)) }
func (l *Logger) Critical(message string) { l.Log(NewEntry(CRITICAL, message, l.Fields)) }
func (l *Logger) Debugf(template string, args ...interface{}) {
l.Log(NewEntryf(DEBUG, template, args, l.Fields))
}
func (l *Logger) Infof(template string, args ...interface{}) {
l.Log(NewEntryf(INFO, template, args, l.Fields))
}
func (l *Logger) Noticef(template string, args ...interface{}) {
l.Log(NewEntryf(NOTICE, template, args, l.Fields))
}
func (l *Logger) Warningf(template string, args ...interface{}) {
l.Log(NewEntryf(WARNING, template, args, l.Fields))
}
func (l *Logger) Errorf(template string, args ...interface{}) {
l.Log(NewEntryf(ERROR, template, args, l.Fields))
}
func (l *Logger) Criticalf(template string, args ...interface{}) {
l.Log(NewEntryf(CRITICAL, template, args, l.Fields))
}
package log
import (
"io"
)
type writerLogger struct {
target io.Writer
}
func (l writerLogger) Log(entry *Entry) {
}
func WriterLogger(target io.WriteCloser) *Logger {
return &Logger{logger: writerLogger{target}}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment