- add custom LogLevel constructors and richer color configuration - support ANSI, 256-color, RGB, and text attributes on LogLevel - introduce clearer preferred APIs such as NewLogger and SetName - preserve backward compatibility with deprecated wrappers for v2.0.1 APIs - restore legacy ColorStringBuilder signatures as compatibility wrappers - fix newline separator handling in Println-style output - add tests for color precedence, deprecated aliases, and LogLevel attributes - update GoDoc and bilingual README documentation
This commit is contained in:
+22
-14
@@ -17,6 +17,20 @@ type LoggerWriter interface {
|
||||
Print(level LogLevel, prefix string, traceback []*MethodTraceback, messages ...any) error
|
||||
}
|
||||
|
||||
// PrepareMessages normalizes message parts and extracts a trailing newline marker.
|
||||
func PrepareMessages(messages ...any) (bool, []any) {
|
||||
msg := Map(messages, func(el any) string { return fmt.Sprint(el) })
|
||||
newline := false
|
||||
if len(msg) > 0 && strings.HasSuffix(msg[len(msg)-1], "\n") {
|
||||
newline = true
|
||||
msg[len(msg)-1] = strings.TrimSuffix(msg[len(msg)-1], "\n")
|
||||
if msg[len(msg)-1] == "" {
|
||||
msg = msg[:len(msg)-1]
|
||||
}
|
||||
}
|
||||
return newline, Map(msg, func(el string) any { return any(el) })
|
||||
}
|
||||
|
||||
// LoggerTextWriter writes human-readable log records to an io.Writer.
|
||||
type LoggerTextWriter struct {
|
||||
LoggerWriter
|
||||
@@ -25,10 +39,13 @@ type LoggerTextWriter struct {
|
||||
formatter *Formatter
|
||||
}
|
||||
|
||||
// SetFormatter replaces the formatter used by the text writer.
|
||||
func (w *LoggerTextWriter) SetFormatter(formatter *Formatter) *LoggerTextWriter {
|
||||
w.formatter = formatter
|
||||
return w
|
||||
}
|
||||
|
||||
// Formatter returns the effective formatter for the text writer.
|
||||
func (w *LoggerTextWriter) Formatter() *Formatter {
|
||||
if w.formatter == nil {
|
||||
return DefaultTextFormatter
|
||||
@@ -43,13 +60,7 @@ func (w *LoggerTextWriter) Write(p []byte) (n int, err error) {
|
||||
|
||||
// Print formats the provided record as text and writes it to the underlying writer.
|
||||
func (w *LoggerTextWriter) Print(level LogLevel, prefix string, traceback []*MethodTraceback, messages ...any) error {
|
||||
msg := Map(messages, func(el any) string { return fmt.Sprint(el) })
|
||||
newline := false
|
||||
if len(msg) > 0 && strings.HasSuffix(msg[len(msg)-1], "\n") {
|
||||
newline = true
|
||||
msg[len(msg)-1] = strings.TrimSuffix(msg[len(msg)-1], "\n")
|
||||
}
|
||||
messages = Map(msg, func(el string) any { return any(el) })
|
||||
newline, messages := PrepareMessages(messages...)
|
||||
|
||||
f := w.Formatter()
|
||||
s := f.FormatMessage(level, prefix, traceback, messages...)
|
||||
@@ -88,12 +99,15 @@ type LoggerJsonWriter struct {
|
||||
formatter *Formatter
|
||||
}
|
||||
|
||||
// Formatter returns the effective formatter for the JSON writer.
|
||||
func (w *LoggerJsonWriter) Formatter() *Formatter {
|
||||
if w.formatter == nil {
|
||||
return DefaultJsonFormatter
|
||||
}
|
||||
return w.formatter
|
||||
}
|
||||
|
||||
// SetFormatter replaces the formatter used by the JSON writer.
|
||||
func (w *LoggerJsonWriter) SetFormatter(f *Formatter) *LoggerJsonWriter {
|
||||
w.formatter = f
|
||||
return w
|
||||
@@ -116,13 +130,7 @@ func (w *LoggerJsonWriter) Write(data []byte) (int, error) {
|
||||
|
||||
// Print encodes the provided record as JSON and writes it to the underlying writer.
|
||||
func (w *LoggerJsonWriter) Print(level LogLevel, prefix string, traceback []*MethodTraceback, messages ...any) error {
|
||||
msg := Map(messages, func(el any) string { return fmt.Sprint(el) })
|
||||
newline := false
|
||||
if len(msg) > 0 && strings.HasSuffix(msg[len(msg)-1], "\n") {
|
||||
newline = true
|
||||
msg[len(msg)-1] = strings.TrimSuffix(msg[len(msg)-1], "\n")
|
||||
}
|
||||
messages = Map(msg, func(el string) any { return any(el) })
|
||||
newline, messages := PrepareMessages(messages...)
|
||||
|
||||
f := w.Formatter()
|
||||
s := f.FormatMessage(level, prefix, traceback, messages...)
|
||||
|
||||
Reference in New Issue
Block a user