From ee3fcccfd26cee6657f660dc33ae7e1cc6168037 Mon Sep 17 00:00:00 2001 From: Yunkon Kim Date: Wed, 25 Sep 2024 19:19:47 +0900 Subject: [PATCH] Ensure log file directory exists before creating the log file --- src/core/common/logger/logger.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/core/common/logger/logger.go b/src/core/common/logger/logger.go index 193de07ef..2a243d549 100644 --- a/src/core/common/logger/logger.go +++ b/src/core/common/logger/logger.go @@ -102,6 +102,16 @@ func NewLogger(config Config) *zerolog.Logger { Compress: config.Compress, } + // Ensure the log file directory exists before creating the log file + dir := filepath.Dir(config.LogFilePath) + if _, err := os.Stat(dir); os.IsNotExist(err) { + // Create the directory if it does not exist + err = os.MkdirAll(dir, 0755) // Set permissions as needed + if err != nil { + log.Fatal().Msgf("Failed to create log directory: %v", err) + } + } + // Ensure the log file exists before changing its permissions if _, err := os.Stat(config.LogFilePath); os.IsNotExist(err) { // Create the log file if it does not exist