feat: add message replacement for log records
Add Logger.AddReplacer to mask or normalize message text before records reach writers. Document replacement behavior in README and package GoDoc, and update the example. Also fix JSON writer edge cases: - avoid panic on empty message lists - preserve newline semantics when the last message already ends with n - keep original message argument types for custom writers when no replacers are configured Update dependencies and add release notes for the next release.
This commit is contained in:
@@ -9,10 +9,12 @@ import (
|
||||
func (l *Logger) Infof(format string, args ...any) {
|
||||
l.print(INFO, fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
// Info logs an info message.
|
||||
func (l *Logger) Info(m ...any) {
|
||||
l.print(INFO, m...)
|
||||
}
|
||||
|
||||
// Infoln logs an info message and appends a newline semantic for writers that need it.
|
||||
func (l *Logger) Infoln(m ...any) {
|
||||
l.println(INFO, m...)
|
||||
@@ -22,10 +24,12 @@ func (l *Logger) Infoln(m ...any) {
|
||||
func (l *Logger) Warnf(format string, args ...any) {
|
||||
l.print(WARN, fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
// Warn logs a warning message.
|
||||
func (l *Logger) Warn(m ...any) {
|
||||
l.print(WARN, m...)
|
||||
}
|
||||
|
||||
// Warnln logs a warning message with newline semantic.
|
||||
func (l *Logger) Warnln(m ...any) {
|
||||
l.println(WARN, m...)
|
||||
@@ -35,10 +39,12 @@ func (l *Logger) Warnln(m ...any) {
|
||||
func (l *Logger) Errorf(format string, args ...any) {
|
||||
l.print(ERROR, fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
// Error logs an error message.
|
||||
func (l *Logger) Error(m ...any) {
|
||||
l.print(ERROR, m...)
|
||||
}
|
||||
|
||||
// Errorln logs an error message with newline semantic.
|
||||
func (l *Logger) Errorln(m ...any) {
|
||||
l.println(ERROR, m...)
|
||||
@@ -49,11 +55,13 @@ func (l *Logger) Fatalf(format string, args ...any) {
|
||||
l.print(FATAL, fmt.Sprintf(format, args...))
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Fatal logs a fatal message and exits the process with code 1.
|
||||
func (l *Logger) Fatal(m ...any) {
|
||||
l.print(FATAL, m...)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Fatalln logs a fatal message with newline semantic and exits the process with code 1.
|
||||
func (l *Logger) Fatalln(m ...any) {
|
||||
l.println(FATAL, m...)
|
||||
@@ -64,10 +72,12 @@ func (l *Logger) Fatalln(m ...any) {
|
||||
func (l *Logger) Debugf(format string, args ...any) {
|
||||
l.print(DEBUG, fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
// Debug logs a debug message.
|
||||
func (l *Logger) Debug(m ...any) {
|
||||
l.print(DEBUG, m...)
|
||||
}
|
||||
|
||||
// Debugln logs a debug message with newline semantic.
|
||||
func (l *Logger) Debugln(m ...any) {
|
||||
l.println(DEBUG, m...)
|
||||
@@ -88,7 +98,7 @@ func (l *Logger) print(level LogLevel, m ...any) {
|
||||
if writer == nil {
|
||||
continue
|
||||
}
|
||||
err := writer.Print(level, l.prefix, tb, m...)
|
||||
err := writer.Print(level, l.prefix, tb, l.replaceAll(m...)...)
|
||||
if err != nil {
|
||||
l.reportWriterError(err)
|
||||
}
|
||||
@@ -111,7 +121,7 @@ func (l *Logger) println(level LogLevel, m ...any) {
|
||||
if writer == nil {
|
||||
continue
|
||||
}
|
||||
err := writer.Print(level, l.prefix, tb, messages...)
|
||||
err := writer.Print(level, l.prefix, tb, l.replaceAll(messages...)...)
|
||||
if err != nil {
|
||||
l.reportWriterError(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user