Fiber Log
We can use logs to observe the behavior of the program, diagnose issues, or configure corresponding alerts. Defining a well-structured log can improve search efficiency and facilitate problem handling.
Fiber provides a default way to print logs to standard output. It also provides several global functions, such as log.Info
, log.Errorf
, and log.Warnw
.
Log Levels
const (
LevelTrace Level = iota
LevelDebug
LevelInfo
LevelWarn
LevelError
LevelFatal
LevelPanic
)
Custom Log
Fiber provides the AllLogger
interface to adapt to various logging libraries.
type CommonLogger interface {
Logger
FormatLogger
WithLogger
}
type AllLogger interface {
CommonLogger
ControlLogger
WithLogger
}
Printing Logs
Note: Calling methods at a Fatal
level will interrupt program execution after printing the log. Please use with caution. Directly printing logs at different levels will be input into the messageKey
and are by default set to msg
.
log.Info("Hello, World!")
log.Debug("Are you OK?")
log.Info("42 is the answer to life, the universe, and everything")
log.Warn("We are under attack!")
log.Error("Houston, we have a problem.")
log.Fatal("So Long, and Thanks for All the Fislog.")
log.Panic("The system is down.")
Formatting and printing logs at different levels, all methods end with f
.
log.Debugf("Hello %s", "boy")
log.Infof("%d is the answer to life, the universe, and everything", 233)
log.Warnf("We are under attack %s!", "boss")
log.Errorf("%s, we have a problem.", "Master Shifu")
log.Fatalf("So Long, and Thanks for All the %s.", "banana")
Printing messages with keys and values. If the keys and values are not paired, KEYVALS UNPAIRED
will be printed.
log.Debugw("", "Hello", "boy")
log.Infow("", "number", 233)
log.Warnw("", "job", "boss")
log.Errorw("", "name", "Master Shifu")
log.Fatalw("", "fruit", "banana")
Global Log
If you only want to use a simple log function in a project and print it globally at any time, we provide a global log.
import "github.com/gofiber/fiber/v2/log"
log.Info("info")
log.Warn("warn")
The above code uses the default log.DefaultLogger
for standard output. You can also find adapters already implemented in the contrib
folder, or use your own implemented Logger
and set the global logger using log.SetLogger
.
import (
"log"
fiberlog "github.com/gofiber/fiber/v2/log"
)
var _ log.AllLogger = (*customLogger)(nil)
type customLogger struct {
stdlog *log.Logger
}
// ...
// Inject custom logger
fiberlog.SetLogger(customLogger)
Set Level
log.SetLevel
sets the logs that are lower than this level and will not be output. The default log recorder is LevelTrace
.
Please note that this method is not concurrency safe.
import "github.com/gofiber/fiber/v2/log"
log.SetLevel(log.LevelInfo)
Set Output
log.SetOutput
sets the output destination for the logging. By default, the logger prints logs to the console.
var logger AllLogger = &defaultLogger{
stdlog: log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile|log.Lmicroseconds),
depth: 4,
}
Set the output destination to a file.
// Output to the test.log file
f, err := os.OpenFile("test.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
return
}
log.SetOutput(f)
Set the output destination to both the console and a file.
// Output to the test.log file
file, _ := os.OpenFile("test.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
iw := io.MultiWriter(os.Stdout, file)
log.SetOutput(iw)
Bind Context
Set the context, and use the following method to return a CommonLogger
instance bound to the specified context.
commonLogger := log.WithContext(ctx)
commonLogger.Info("info")
Note: This is the default logging library for the Fiber framework. Of course, you can choose other well-known Go open-source logging libraries.