Skip to content

Commit 19ea3df

Browse files
Add docs from gofiber/fiber@64a7113
1 parent ab7054c commit 19ea3df

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

β€Ždocs/core/api/log.mdβ€Ž

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const (
2525

2626
## Custom Log
2727

28-
Fiber provides the `AllLogger` interface for adapting various log libraries.
28+
Fiber provides the generic `AllLogger[T]` interface for adapting various log libraries.
2929

3030
```go
3131
type CommonLogger interface {
@@ -34,9 +34,20 @@ type CommonLogger interface {
3434
WithLogger
3535
}
3636

37-
type AllLogger interface {
37+
type ConfigurableLogger[T any] interface {
38+
// SetLevel sets logging level.
39+
SetLevel(level Level)
40+
41+
// SetOutput sets the logger output.
42+
SetOutput(w io.Writer)
43+
44+
// Logger returns the logger instance.
45+
Logger() T
46+
}
47+
48+
type AllLogger[T any] interface {
3849
CommonLogger
39-
ConfigurableLogger
50+
ConfigurableLogger[T]
4051
WithLogger
4152
}
4253
```
@@ -106,7 +117,7 @@ import (
106117
fiberlog "github.com/gofiber/fiber/v3/log"
107118
)
108119

109-
var _ fiberlog.AllLogger = (*customLogger)(nil)
120+
var _ fiberlog.AllLogger[*log.Logger] = (*customLogger)(nil)
110121

111122
type customLogger struct {
112123
stdlog *log.Logger
@@ -115,9 +126,13 @@ type customLogger struct {
115126
// Implement required methods for the AllLogger interface...
116127

117128
// Inject your custom logger
118-
fiberlog.SetLogger(&customLogger{
129+
fiberlog.SetLogger[*log.Logger](&customLogger{
119130
stdlog: log.New(os.Stdout, "CUSTOM ", log.LstdFlags),
120131
})
132+
133+
// Retrieve the underlying *log.Logger for direct use
134+
std := fiberlog.DefaultLogger[*log.Logger]().Logger()
135+
std.Println("custom logging")
121136
```
122137

123138
## Set Level
@@ -141,7 +156,7 @@ Setting the log level allows you to control the verbosity of the logs, filtering
141156
### Writing Logs to Stderr
142157

143158
```go
144-
var logger fiberlog.AllLogger = &defaultLogger{
159+
var logger fiberlog.AllLogger[*log.Logger] = &defaultLogger{
145160
stdlog: log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile|log.Lmicroseconds),
146161
depth: 4,
147162
}
@@ -193,12 +208,8 @@ You can use Logger to retrieve the logger instance. It is useful when you need t
193208
To retrieve the Logger instance, use the following method:
194209

195210
```go
196-
logger := fiberlog.DefaultLogger() // Call DefaultLogger to get the default logger instance
197-
198-
stdlogger, ok := logger.Logger().(*log.Logger) // Get the logger instance and assert it to *log.Logger
199-
if !ok {
200-
panic("logger is not *log.Logger")
201-
}
211+
logger := fiberlog.DefaultLogger[*log.Logger]() // Get the default logger instance
202212

213+
stdlogger := logger.Logger() // stdlogger is *log.Logger
203214
stdlogger.SetFlags(0) // Hide timestamp by setting flags to 0
204215
```

β€Ždocs/core/whats_new.mdβ€Ž

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ INFO Total process count: 1
995995

996996
## πŸ“ƒ Log
997997

998-
`fiber.AllLogger` interface now has a new method called `Logger`. This method can be used to get the underlying logger instance from the Fiber logger middleware. This is useful when you want to configure the logger middleware with a custom logger and still want to access the underlying logger instance.
998+
`fiber.AllLogger[T]` interface now has a new generic type parameter `T` and a method called `Logger`. This method can be used to get the underlying logger instance from the Fiber logger middleware. This is useful when you want to configure the logger middleware with a custom logger and still want to access the underlying logger instance with the appropriate type.
999999

10001000
You can find more details about this feature in [/docs/api/log.md](./api/log.md#logger).
10011001

@@ -1436,6 +1436,7 @@ fiber migrate --to 3.0.0
14361436
- [🧠 Context](#-context-1)
14371437
- [πŸ“Ž Binding (was Parser)](#-parser)
14381438
- [πŸ”„ Redirect](#-redirect-1)
1439+
- [🧾 Log](#-log-1)
14391440
- [🌎 Client package](#-client-package-1)
14401441
- [πŸ› οΈ Utils](#utils-migration)
14411442
- [🧬 Middlewares](#-middlewares-1)
@@ -1903,6 +1904,10 @@ Fiber v3 enhances the redirect functionality by introducing new methods and impr
19031904

19041905
</details>
19051906

1907+
#### 🧾 Log
1908+
1909+
The `ConfigurableLogger` and `AllLogger` interfaces now use generics. You can specify the underlying logger type when implementing these interfaces. While `any` can be used for maximum flexibility in some contexts, when retrieving the concrete logger via `log.DefaultLogger`, you must specify the exact underlying logger type, for example `log.DefaultLogger[*MyLogger]().Logger()`.
1910+
19061911
### 🌎 Client package
19071912

19081913
Fiber v3 introduces a completely rebuilt client package with numerous new features such as Cookiejar, request/response hooks, and more. Here is a guide to help you migrate from Fiber v2 to Fiber v3.

0 commit comments

Comments
Β (0)