fiber/log/log.go
Hao Chun Chang 67021360e1
🔥 Feature: Add AllLogger to Config (#3153)
* 🔥 Feature: Add SetFlags to Logger Interface

🔥 Feature: Add fiberlog Logger field to config

* 🚨 Test: custom-defined Logger and LoggerFunc

* 📚 Doc: add LoggerFunc and Logger to middleware logger

* 🚨 Test: fine-tune custom Logger and LoggerFunc

* 📚 Doc: add Logger documentation

📚 Doc: add custom Logger example

* 🩹 fix: add default Logger field to default config

* 📚 Doc: remove Logger field in middleware logger

📚 Doc: add example of using fiber logger interface

* 🚨 Test: add tests for using fiber logger interface wrapper

* 📚 Doc: update custom logger example

* Update docs/middleware/logger.md

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* update

* update logger docs

* update what's new

* replace setflags with getloggerinstance

* fix linter

* update

* Fix markdownlint issues

* apply reviews & improve coverage

* fix linter

* rename controllogger

* Update whats_new.md

expandable example

---------

Co-authored-by: RW <rene@gofiber.io>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Muhammed Efe Cetin <efectn@protonmail.com>
Co-authored-by: Juan Calderon-Perez <835733+gaby@users.noreply.github.com>
2024-12-01 13:32:52 +01:00

111 lines
2.5 KiB
Go

package log
import (
"context"
"fmt"
"io"
"log"
"os"
)
var logger AllLogger = &defaultLogger{
stdlog: log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile|log.Lmicroseconds),
depth: 4,
}
// Logger is a logger interface that provides logging function with levels.
type Logger interface {
Trace(v ...any)
Debug(v ...any)
Info(v ...any)
Warn(v ...any)
Error(v ...any)
Fatal(v ...any)
Panic(v ...any)
}
// FormatLogger is a logger interface that output logs with a format.
type FormatLogger interface {
Tracef(format string, v ...any)
Debugf(format string, v ...any)
Infof(format string, v ...any)
Warnf(format string, v ...any)
Errorf(format string, v ...any)
Fatalf(format string, v ...any)
Panicf(format string, v ...any)
}
// WithLogger is a logger interface that output logs with a message and key-value pairs.
type WithLogger interface {
Tracew(msg string, keysAndValues ...any)
Debugw(msg string, keysAndValues ...any)
Infow(msg string, keysAndValues ...any)
Warnw(msg string, keysAndValues ...any)
Errorw(msg string, keysAndValues ...any)
Fatalw(msg string, keysAndValues ...any)
Panicw(msg string, keysAndValues ...any)
}
type CommonLogger interface {
Logger
FormatLogger
WithLogger
}
// ConfigurableLogger provides methods to config a logger.
type ConfigurableLogger interface {
// SetLevel sets logging level.
//
// Available levels: Trace, Debug, Info, Warn, Error, Fatal, Panic.
SetLevel(level Level)
// SetOutput sets the logger output.
SetOutput(w io.Writer)
// Logger returns the logger instance. It can be used to adjust the logger configurations in case of need.
Logger() any
}
// AllLogger is the combination of Logger, FormatLogger, CtxLogger and ConfigurableLogger.
// Custom extensions can be made through AllLogger
type AllLogger interface {
CommonLogger
ConfigurableLogger
// WithContext returns a new logger with the given context.
WithContext(ctx context.Context) CommonLogger
}
// Level defines the priority of a log message.
// When a logger is configured with a level, any log message with a lower
// log level (smaller by integer comparison) will not be output.
type Level int
// The levels of logs.
const (
LevelTrace Level = iota
LevelDebug
LevelInfo
LevelWarn
LevelError
LevelFatal
LevelPanic
)
var strs = []string{
"[Trace] ",
"[Debug] ",
"[Info] ",
"[Warn] ",
"[Error] ",
"[Fatal] ",
"[Panic] ",
}
func (lv Level) toString() string {
if lv >= LevelTrace && lv <= LevelPanic {
return strs[lv]
}
return fmt.Sprintf("[?%d] ", lv)
}