FILE / ScuroNeko/SNekLog

io.go

Исходный файл и его история в репозитории.
FILE dd3c7286d27688e6b17037e94d522b5ca6cce968
Files
SNekLog/io.go
T
ScuroNeko e6d15b530f
Golang lint / lint (push) Successful in 1m36s
(new): add threshold-based log level filtering and docs
- add threshold-aware LogLevel constructors
- add Logger.SetThresholdMode and SameThreshold
- preserve legacy SameLevel behavior for compatibility
- extend tests for threshold mode and compatibility
- update README and release notes for v2.2.0
2026-04-28 09:45:40 +03:00

131 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...)
}
func (l *Logger) print(level LogLevel, newline bool, m ...any) {
if l.threshold && l.level.th < level.th {
return
} else if l.level.n < level.n {
return
}
if len(l.writers) == 0 {
return
}
tb := getFullTraceback(1)
messages := l.replaceAll(m...)
if newline {
messages = append(messages, any("\n"))
}
for _, writer := range l.writers {
if writer == nil {
continue
}
err := writer.Print(level, l.prefix, tb, messages...)
if err != nil {
l.reportWriterError(err)
}
}
}
// Print logs a message without appending a trailing newline semantic.
func (l *Logger) Print(level LogLevel, m ...any) { l.print(level, false, m...) }
// Println logs a message and appends a trailing newline semantic for writers that need it.
func (l *Logger) Println(level LogLevel, m ...any) { l.print(level, true, m...) }
// Printf formats according to a format specifier and logs the resulting message.
func (l *Logger) Printf(level LogLevel, format string, args ...any) {
l.print(level, false, fmt.Sprintf(format, args...))
}
// 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)
}