FILE / ScuroNeko/SNekLog

io.go

Исходный файл и его история в репозитории.
FILE v2.1.0
Files
SNekLog/io.go
T
ScuroNeko 3eac1851ec
Golang lint / lint (push) Successful in 24s
(new): prepare v2.1.0 release
- 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
2026-04-27 15:42:24 +03:00

140 lines
3.2 KiB
Go

package sneklog
import (
"fmt"
"os"
)
// Infof logs a formatted info message.
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...)
}
// Warnf logs a formatted warning message.
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...)
}
// Errorf logs a formatted error message.
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...)
}
// Fatalf logs a formatted fatal message and exits the process with code 1.
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...)
os.Exit(1)
}
// Debugf logs a formatted debug message.
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...)
}
// Print write message without trailing "\n"
// Good for database
func (l *Logger) Print(level LogLevel, m ...any) {
if l.level.n < level.n {
return
}
if len(l.writers) == 0 {
return
}
tb := getFullTraceback(1)
for _, writer := range l.writers {
if writer == nil {
continue
}
err := writer.Print(level, l.prefix, tb, l.replaceAll(m...)...)
if err != nil {
l.reportWriterError(err)
}
}
}
// Println
// Docker requires "\n" at end to write to log.
// print not work for docker, otherwise it will work and write into stdout
func (l *Logger) Println(level LogLevel, m ...any) {
if l.level.n < level.n {
return
}
if len(l.writers) == 0 {
return
}
tb := getFullTraceback(1)
messages := append(append(make([]any, 0, len(m)+1), l.replaceAll(m...)...), "\n")
for _, writer := range l.writers {
if writer == nil {
continue
}
err := writer.Print(level, l.prefix, tb, messages...)
if err != nil {
l.reportWriterError(err)
}
}
}
// reportWriterError writes internal writer failures directly to stderr to avoid
// re-entering the same logger path that just failed.
func (l *Logger) reportWriterError(err error) {
if err == nil {
return
}
_, _ = fmt.Fprintf(os.Stderr, "[%s] logger writer error: %v\n", l.prefix, err)
}