Skip to content

Commit

Permalink
[lawrencegripper#140] add a redactor mechanism with logrus backend to…
Browse files Browse the repository at this point in the history
… edit the output
  • Loading branch information
JLugagne committed Jul 16, 2018
1 parent 6643a72 commit 41f4aaa
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions internal/pkg/logger/redactor.go
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
}
}

0 comments on commit 41f4aaa

Please sign in to comment.