feat(logging): allow time replacement in log file name (#3330)

* feat(logging): allow time replacement in log file name

This allows replacing `%d` with a date time format in the log `file_name` option.

Closes #3210.
This commit is contained in:
James Elliott 2022-06-06 09:50:10 +10:00 committed by GitHub
parent 48ded6a507
commit 46d84e46b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -87,6 +87,16 @@ log:
file_path: /config/authelia.log file_path: /config/authelia.log
``` ```
If you include the value `%d` in the filename it will replace this value with a date time indicative of the time
the logger was initialized in the following format:
`2006-02-01T150405Z`
```yaml
log:
file_path: /config/authelia.%d.log
```
### keep_stdout ### keep_stdout
<div markdown="1"> <div markdown="1">
type: boolean type: boolean

View File

@ -3,6 +3,8 @@ package logging
import ( import (
"io" "io"
"os" "os"
"strings"
"time"
logrus_stack "github.com/Gurpartap/logrus-stack" logrus_stack "github.com/Gurpartap/logrus-stack"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -30,7 +32,9 @@ func InitializeLogger(config schema.LogConfiguration, log bool) error {
} }
if config.FilePath != "" { if config.FilePath != "" {
f, err := os.OpenFile(config.FilePath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) filePath := strings.ReplaceAll(config.FilePath, "%d", time.Now().Format("2006-02-01T150405Z"))
f, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil { if err != nil {
return err return err