forked from distribworks/dkron
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add logrus Hook to write logs to stderr and stdout based on log level (…
- Loading branch information
Showing
2 changed files
with
36 additions
and
0 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,32 @@ | ||
package logging | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type LogSplitter struct{} | ||
|
||
// Levels returns levels that are supported by the hook | ||
func (l *LogSplitter) Levels() []logrus.Level { | ||
return []logrus.Level{logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel, logrus.WarnLevel, logrus.InfoLevel, logrus.DebugLevel, logrus.TraceLevel} | ||
} | ||
|
||
// Fire handles logging events based on log levels. | ||
func (l *LogSplitter) Fire(entry *logrus.Entry) error { | ||
if entry == nil { | ||
return fmt.Errorf("logrus entry is nil") | ||
} | ||
|
||
switch entry.Level { | ||
case logrus.WarnLevel, logrus.DebugLevel, logrus.InfoLevel, logrus.TraceLevel: | ||
entry.Logger.Out = os.Stdout | ||
case logrus.ErrorLevel, logrus.PanicLevel, logrus.FatalLevel: | ||
entry.Logger.Out = os.Stderr | ||
default: | ||
entry.Logger.Out = os.Stdout | ||
} | ||
return nil | ||
} |