Skip to content

Commit

Permalink
chore: logging fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
moshloop committed Aug 16, 2024
1 parent ef7aaf9 commit 45a844b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
23 changes: 20 additions & 3 deletions logger/default.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package logger

import (
"crypto/md5"
"encoding/hex"
"flag"
"fmt"
"strings"
Expand Down Expand Up @@ -44,7 +46,7 @@ func Infof(format string, args ...interface{}) {

// Secretf is like Tracef, but attempts to strip any secrets from the text
func Secretf(format string, args ...interface{}) {
currentLogger.Tracef(stripSecrets(fmt.Sprintf(format, args...)))
currentLogger.Tracef(StripSecrets(fmt.Sprintf(format, args...)))
}

// Prettyf is like Tracef, but pretty prints the entire struct
Expand Down Expand Up @@ -95,10 +97,25 @@ func StandardLogger() Logger {
return currentLogger
}

// stripSecrets takes a YAML or INI formatted text and removes any potentially secret data
func PrintableSecret(secret string) string {
if len(secret) == 0 {
return "<nil>"
} else if len(secret) > 30 {
sum := md5.Sum([]byte(secret))
hash := hex.EncodeToString(sum[:])
return fmt.Sprintf("md5(%s),length=%d", hash[0:8], len(secret))
} else if len(secret) > 16 {
return fmt.Sprintf("%s****%s", secret[0:1], secret[len(secret)-2:])
} else if len(secret) > 10 {
return fmt.Sprintf("****%s", secret[len(secret)-1:])
}
return "****"
}

// StripSecrets takes a YAML or INI formatted text and removes any potentially secret data
// as denoted by keys containing "pass" or "secret" or exact matches for "key"
// the last character of the secret is kept to aid in troubleshooting
func stripSecrets(text string) string {
func StripSecrets(text string) string {
out := ""
for _, line := range strings.Split(text, "\n") {

Expand Down
26 changes: 17 additions & 9 deletions logger/slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/kr/pretty"
"github.com/lmittmann/tint"
"github.com/lrita/cmap"
"github.com/samber/lo"
)

var (
Expand Down Expand Up @@ -131,15 +132,22 @@ func GetLogger(names ...string) *SlogLogger {
return parent
}

key := strings.ToLower(strings.Join(names, ""))
if key == "" {
return parent
}

if v, ok := namedLoggers.Load(key); ok {
return v
path := ""
for _, name := range names {
if !strings.Contains(name, " ") {
name = strings.ToLower(strings.Join(lo.Words(name), " "))
} else {
name = strings.ToLower(name)
}
if path != "" {
path += "."
}
path = path + name
if v, ok := namedLoggers.Load(path); ok {
return v
}
}
child, _ := namedLoggers.LoadOrStore(key, New(key))
child, _ := namedLoggers.LoadOrStore(path, New(path))
return child
}

Expand Down Expand Up @@ -172,7 +180,7 @@ func (s SlogLogger) Infof(format string, args ...interface{}) {
}

func (s SlogLogger) Secretf(format string, args ...interface{}) {
s.Debugf(stripSecrets(fmt.Sprintf(format, args...)))
s.Debugf(StripSecrets(fmt.Sprintf(format, args...)))
}

func (s SlogLogger) Prettyf(msg string, obj interface{}) {
Expand Down

0 comments on commit 45a844b

Please sign in to comment.