-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom filter for text_formatter (#212)
* Add custom filter for text_formatter In order to make console output more user friendly, lets remove some fields that are unnecessary for human consumption (team, component, service) * remove run_id as well
- Loading branch information
Showing
2 changed files
with
40 additions
and
2 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,36 @@ | ||
package log | ||
|
||
import ( | ||
"time" | ||
|
||
logrus "github.com/sirupsen/logrus" | ||
) | ||
|
||
var ( | ||
DefaultRemovedFields = []string{"component", "team", "service", "run_id"} | ||
) | ||
|
||
// FilteredTextFormatter is a logrus.TextFormatter that filters out some specific fields that are not needed for humans. | ||
// These fields are usually more helpful for machines, and with json formatting for example. | ||
type FilteredTextFormatter struct { | ||
tf logrus.TextFormatter | ||
removedFields []string | ||
} | ||
|
||
func NewFilteredTextFormatter(removedFields []string) logrus.Formatter { | ||
return &FilteredTextFormatter{ | ||
tf: logrus.TextFormatter{ | ||
FullTimestamp: true, | ||
TimestampFormat: time.TimeOnly, | ||
}, | ||
removedFields: removedFields, | ||
} | ||
} | ||
|
||
func (f *FilteredTextFormatter) Format(entry *logrus.Entry) ([]byte, error) { | ||
for _, field := range f.removedFields { | ||
delete(entry.Data, field) | ||
} | ||
|
||
return f.tf.Format(entry) | ||
} |