From 46d84e46b0b98765fecf7b526959fb848ee877fc Mon Sep 17 00:00:00 2001 From: James Elliott Date: Mon, 6 Jun 2022 09:50:10 +1000 Subject: [PATCH] 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. --- docs/configuration/logging.md | 10 ++++++++++ internal/logging/logger.go | 6 +++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/docs/configuration/logging.md b/docs/configuration/logging.md index a3d176d9e..b0b5a95bf 100644 --- a/docs/configuration/logging.md +++ b/docs/configuration/logging.md @@ -87,6 +87,16 @@ 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
type: boolean diff --git a/internal/logging/logger.go b/internal/logging/logger.go index 9cd10f121..9b5540cce 100644 --- a/internal/logging/logger.go +++ b/internal/logging/logger.go @@ -3,6 +3,8 @@ package logging import ( "io" "os" + "strings" + "time" logrus_stack "github.com/Gurpartap/logrus-stack" "github.com/sirupsen/logrus" @@ -30,7 +32,9 @@ func InitializeLogger(config schema.LogConfiguration, log bool) error { } 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 { return err