-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from nsmith5/logging/middleware
Use a middleware pattern for all logging.
- Loading branch information
Showing
9 changed files
with
201 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package agent | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
logrus "github.com/sirupsen/logrus" | ||
) | ||
|
||
func newLogger(c Config) *logrus.Logger { | ||
log := logrus.New() | ||
log.SetOutput(os.Stdout) | ||
switch c.Logging.Level { | ||
case "panic": | ||
log.SetLevel(logrus.PanicLevel) | ||
case "fatal": | ||
log.SetLevel(logrus.FatalLevel) | ||
case "error": | ||
log.SetLevel(logrus.ErrorLevel) | ||
case "warn": | ||
log.SetLevel(logrus.WarnLevel) | ||
case "info": | ||
log.SetLevel(logrus.InfoLevel) | ||
case "debug": | ||
log.SetLevel(logrus.DebugLevel) | ||
case "trace": | ||
log.SetLevel(logrus.TraceLevel) | ||
} | ||
log.SetFormatter(&logrus.JSONFormatter{}) | ||
log.SetReportCaller(true) | ||
|
||
return log | ||
} | ||
|
||
type logAgent struct { | ||
inner Agent | ||
log *logrus.Logger | ||
} | ||
|
||
// WithLogging adds logging to agent | ||
func withLogging(a Agent, log *logrus.Logger) Agent { | ||
return &logAgent{inner: a, log: log} | ||
} | ||
|
||
func (a *logAgent) Run() error { | ||
a.log.Info("agent: run launching") | ||
err := a.inner.Run() | ||
if err != nil { | ||
a.log.WithFields(logrus.Fields{ | ||
`err`: err, | ||
}).Error("agent: exit with error") | ||
return err | ||
} | ||
a.log.Debug("agent: run exit without error") | ||
return nil | ||
} | ||
|
||
func (a *logAgent) Shutdown(ctx context.Context) error { | ||
a.log.Info("agent: shutting down") | ||
err := a.inner.Shutdown(ctx) | ||
if err != nil { | ||
a.log.WithFields(logrus.Fields{ | ||
`err`: err, | ||
}).Error("agent: failed to shutdown gracefully") | ||
return err | ||
} | ||
a.log.Debug("agent: successful graceful shutdown") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package outputs | ||
|
||
import "github.com/sirupsen/logrus" | ||
|
||
type ologger struct { | ||
inner Output | ||
log *logrus.Logger | ||
} | ||
|
||
// WithLogging logs an output driver | ||
func WithLogging(o Output, log *logrus.Logger) Output { | ||
return &ologger{inner: o, log: log} | ||
} | ||
|
||
func (ol *ologger) Send(e Event) error { | ||
ol.log.Debugf("output: driver %s sending event", ol.inner.Name()) | ||
err := ol.inner.Send(e) | ||
if err != nil { | ||
ol.log.WithFields(logrus.Fields{ | ||
`err`: err, | ||
`driver`: ol.inner.Name(), | ||
}).Error("output: failed to send event") | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (ol *ologger) Name() string { | ||
return ol.inner.Name() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.