fix(logs): Small refacto and allow multiple providers at the same time
This commit is contained in:
@ -16,6 +16,13 @@ import (
|
||||
// be available in the queue.
|
||||
var BufSize uint = 8192
|
||||
|
||||
// defaultFormater will be use if no formatter is given.
|
||||
var defaultFormater = &logrus.TextFormatter{
|
||||
DisableColors: true,
|
||||
TimestampFormat: time.RFC3339Nano,
|
||||
QuoteEmptyFields: true,
|
||||
}
|
||||
|
||||
// Hook will write logs to a file.
|
||||
type Hook struct {
|
||||
Level logrus.Level
|
||||
@ -27,19 +34,21 @@ type Hook struct {
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
// XXX: Maybe just take a formatter in input
|
||||
|
||||
// NewFileHook creates a hook to be added to an instance of logger.
|
||||
func NewFileHook(w io.Writer, of OutputFormat) *Hook {
|
||||
func NewFileHook(w io.Writer, f logrus.Formatter) *Hook {
|
||||
if w == nil {
|
||||
logrus.Error("Can't create File Hook with an empty writer")
|
||||
return nil
|
||||
}
|
||||
|
||||
if f == nil {
|
||||
f = defaultFormater
|
||||
}
|
||||
|
||||
hook := &Hook{
|
||||
Level: logrus.DebugLevel,
|
||||
synchronous: true,
|
||||
f: handleFormat(of),
|
||||
f: f,
|
||||
w: w,
|
||||
}
|
||||
|
||||
@ -49,16 +58,20 @@ func NewFileHook(w io.Writer, of OutputFormat) *Hook {
|
||||
// NewAsyncFileHook creates a hook to be added to an instance of logger.
|
||||
// The hook created will be asynchronous, and it's the responsibility of the user to call the Flush method
|
||||
// before exiting to empty the log queue.
|
||||
func NewAsyncFileHook(w io.Writer, of OutputFormat) *Hook {
|
||||
func NewAsyncFileHook(w io.Writer, f logrus.Formatter) *Hook {
|
||||
if w == nil {
|
||||
logrus.Error("Can't create File Hook with an empty writer")
|
||||
return nil
|
||||
}
|
||||
|
||||
if f == nil {
|
||||
f = defaultFormater
|
||||
}
|
||||
|
||||
hook := &Hook{
|
||||
Level: logrus.DebugLevel,
|
||||
buf: make(chan logrus.Entry, BufSize),
|
||||
f: handleFormat(of),
|
||||
f: f,
|
||||
w: w,
|
||||
}
|
||||
|
||||
@ -80,23 +93,6 @@ func (hook *Hook) Flush() {
|
||||
hook.wg.Wait()
|
||||
}
|
||||
|
||||
// handleFormat will take a OutputFormat and will transform it in a formatter.
|
||||
func handleFormat(of OutputFormat) logrus.Formatter {
|
||||
|
||||
if of == OutputFormat_JSON {
|
||||
return &logrus.JSONFormatter{
|
||||
PrettyPrint: false,
|
||||
TimestampFormat: time.RFC3339Nano,
|
||||
}
|
||||
}
|
||||
|
||||
return &logrus.TextFormatter{
|
||||
DisableColors: true,
|
||||
TimestampFormat: time.RFC3339Nano,
|
||||
QuoteEmptyFields: true,
|
||||
}
|
||||
}
|
||||
|
||||
// Fire is called when a log event is fired.
|
||||
// We assume the entry will be altered by another hook,
|
||||
// otherwise we might be logging something wrong to Graylog.
|
||||
|
@ -70,7 +70,24 @@ func NewHook(c *ConfigStruct) (logrus.Hook, error) {
|
||||
w = fh
|
||||
}
|
||||
|
||||
h := NewAsyncFileHook(w, outputFormat)
|
||||
h := NewAsyncFileHook(w, handleFormat(outputFormat))
|
||||
|
||||
return h, nil
|
||||
}
|
||||
|
||||
// handleFormat will take a OutputFormat and will transform it in a formatter.
|
||||
func handleFormat(of OutputFormat) logrus.Formatter {
|
||||
|
||||
if of == OutputFormat_JSON {
|
||||
return &logrus.JSONFormatter{
|
||||
PrettyPrint: false,
|
||||
TimestampFormat: time.RFC3339Nano,
|
||||
}
|
||||
}
|
||||
|
||||
return &logrus.TextFormatter{
|
||||
DisableColors: true,
|
||||
TimestampFormat: time.RFC3339Nano,
|
||||
QuoteEmptyFields: true,
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user