forked from tadaweb/ion
-
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.
[lawrencegripper#140] add a redactor mechanism with logrus backend to…
… edit the output
- Loading branch information
Showing
1 changed file
with
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package logger | ||
|
||
import ( | ||
"bytes" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type RedactorFunc func([]byte) []byte | ||
|
||
type Redactor struct { | ||
backend logrus.Formatter | ||
|
||
Redactor RedactorFunc | ||
} | ||
|
||
func (redactor Redactor) Format(entry *logrus.Entry) ([]byte, error) { | ||
serialized, err := redactor.backend.Format(entry) | ||
|
||
if err == nil { | ||
serialized = redactor.Redactor(serialized) | ||
} | ||
|
||
return serialized, err | ||
} | ||
|
||
func (redactor *Redactor) init() { | ||
redactor.Redactor = func(in []byte) []byte { return in } | ||
} | ||
|
||
func NewJsonRedactor() Redactor { | ||
redactor := Redactor{} | ||
redactor.backend = new(logrus.JSONFormatter) | ||
return redactor | ||
} | ||
|
||
func NewJsonSecretRedactor(fun RedactorFunc) Redactor { | ||
redactor := Redactor{} | ||
redactor.backend = new(logrus.JSONFormatter) | ||
redactor.Redactor = fun | ||
return redactor | ||
} | ||
|
||
func NewTextRedactor() Redactor { | ||
redactor := Redactor{} | ||
redactor.backend = new(logrus.TextFormatter) | ||
return redactor | ||
} | ||
|
||
func NewTextSecretRedactor(fun RedactorFunc) Redactor { | ||
redactor := Redactor{} | ||
redactor.backend = new(logrus.TextFormatter) | ||
redactor.Redactor = fun | ||
return redactor | ||
} | ||
|
||
func NewSecretRedact(secrets [][]byte, redacted []byte) RedactorFunc { | ||
return func(serialized []byte) []byte { | ||
out := serialized | ||
for _, s := range secrets { | ||
out = bytes.Replace(out, s, redacted, -1) | ||
} | ||
return out | ||
} | ||
} |