You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
2.9 KiB
Go

5 years ago
package blammo
import (
"fmt"
)
type Log struct {
dw EventWriter
iw EventWriter
ew EventWriter
path *Path
clock Clock
tags *Tags
}
5 years ago
func NewLog(name string, options ...Option) *Log {
l := &Log{
path: NewPath(name),
clock: SystemClock{},
5 years ago
}
for _, opt := range options {
opt.apply(l)
}
return l
5 years ago
}
func format(t string, args ...interface{}) string {
if len(args) == 0 {
return t
}
return fmt.Sprintf(t, args...)
}
5 years ago
func (l *Log) event(lvl Level, t string, args ...interface{}) *Event {
return &Event{
Time: l.clock.Now(),
Level: lvl,
Path: l.path,
Tags: l.tags,
Text: format(t, args...),
}
}
5 years ago
func (l *Log) Debug(t string, args ...interface{}) {
5 years ago
if l.dw != nil {
l.dw.WriteEvent(l.event(Debug, t, args...))
}
5 years ago
}
func (l *Log) Info(t string, args ...interface{}) {
5 years ago
if l.iw != nil {
l.iw.WriteEvent(l.event(Info, t, args...))
}
5 years ago
}
func (l *Log) Error(t string, args ...interface{}) {
5 years ago
if l.ew != nil {
l.ew.WriteEvent(l.event(Error, t, args...))
}
5 years ago
}
func (l *Log) Child(name string) *Log {
5 years ago
return &Log{
dw: l.dw,
iw: l.iw,
ew: l.ew,
path: l.path.Child(name),
clock: l.clock,
tags: l.tags,
}
5 years ago
}
5 years ago
// Tag creates a new Tags struct having the given key as its final key, with no
// associated value. All existing keys continue to exist.
func (l *Log) Tag(key string) *Log {
return &Log{
dw: l.dw,
iw: l.iw,
ew: l.ew,
path: l.path,
clock: l.clock,
tags: &Tags{key: key, parent: l.tags},
}
}
// TagInt creates a new Tags struct having the given key-value pair as the
// final key-value pair. All existing key-value pairs continue to exist.
func (l *Log) TagInt(key string, v int) *Log {
return &Log{
dw: l.dw,
iw: l.iw,
ew: l.ew,
path: l.path,
clock: l.clock,
tags: &Tags{key: key, value: v, parent: l.tags},
}
}
// TagString creates a new Tags struct having the given key-value pair as the
// final key-value pair. All existing key-value pairs continue to exist.
func (l *Log) TagString(key, v string) *Log {
return &Log{
dw: l.dw,
iw: l.iw,
ew: l.ew,
path: l.path,
clock: l.clock,
tags: &Tags{key: key, value: v, parent: l.tags},
}
5 years ago
}
5 years ago
// TagFloat creates a new Tags struct having the given key-value pair as the
// final key-value pair. All existing key-value pairs continue to exist.
func (l *Log) TagFloat(key string, v float64) *Log {
return &Log{
dw: l.dw,
iw: l.iw,
ew: l.ew,
path: l.path,
clock: l.clock,
tags: &Tags{key: key, value: v, parent: l.tags},
}
}
type Option interface{ apply(*Log) }
type optionFn func(*Log)
func (fn optionFn) apply(l *Log) { fn(l) }
func DebugWriter(w EventWriter) Option { return optionFn(func(l *Log) { l.dw = w }) }
func InfoWriter(w EventWriter) Option { return optionFn(func(l *Log) { l.iw = w }) }
func ErrorWriter(w EventWriter) Option { return optionFn(func(l *Log) { l.ew = w }) }
func UserClock(c Clock) Option { return optionFn(func(l *Log) { l.clock = c }) }