Skip to content

Commit 4e5e237

Browse files
committed
update log.md
1 parent 4039a7b commit 4e5e237

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

docs/api/log.md

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ type CommonLogger interface {
3838
}
3939

4040
type AllLogger interface {
41-
CommonLogger
42-
ControlLogger
43-
WithLogger
41+
CommonLogger
42+
ControlLogger
43+
WithLogger
4444
}
4545
```
4646

@@ -99,7 +99,7 @@ import (
9999
var _ log.AllLogger = (*customLogger)(nil)
100100

101101
type customLogger struct {
102-
stdlog *log.Logger
102+
stdlog *log.Logger
103103
}
104104

105105
// ...
@@ -118,11 +118,39 @@ import "github.com/gofiber/fiber/v2/log"
118118

119119
log.SetLevel(log.LevelInfo)
120120
```
121+
## Set output
122+
123+
`log.SetOutput` sets the output destination of the logger. The default logger types the log in the console.
124+
125+
```go
126+
var logger AllLogger = &defaultLogger{
127+
stdlog: log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile|log.Lmicroseconds),
128+
depth: 4,
129+
}
130+
```
121131

132+
Set the output destination to the file.
133+
134+
```go
135+
// Output to ./test.log file
136+
f, err := os.OpenFile("test.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
137+
if err != nil {
138+
return
139+
}
140+
log.SetOutput(f)
141+
```
142+
Set the output destination to the console and file.
143+
144+
```go
145+
// Output to ./test.log file
146+
file, _ := os.OpenFile("test.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
147+
iw := io.MultiWriter(os.Stdout, file)
148+
log.SetOutput(iw)
149+
```
122150
## Bind context
123151
Set the context, using the following method will return a `CommonLogger` instance bound to the specified context
124152
```go
125-
126153
commonLogger := log.WithContext(ctx)
127154
commonLogger.Info("info")
128155
```
156+

0 commit comments

Comments
 (0)