From c04850fa6c4ee5322d8eb38101c8d8885128215d Mon Sep 17 00:00:00 2001 From: tyler Date: Tue, 26 Dec 2023 11:30:29 -0500 Subject: [PATCH] Fixed issue with initializing logs --- app.go | 7 +------ internal/config/config.go | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/app.go b/app.go index e809ac5..464e85e 100644 --- a/app.go +++ b/app.go @@ -57,12 +57,7 @@ func (a *App) startup(ctx context.Context) { } func (a *App) initLog() error { - fp, err := config.LogFile() - if err != nil { - return fmt.Errorf("error getting filepath for log file: %v", err) - } - - f, err := os.OpenFile(fp, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) + f, err := config.LogFile() if err != nil { return fmt.Errorf("error opening log file: %v", err) } diff --git a/internal/config/config.go b/internal/config/config.go index 24085be..76175a1 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -21,13 +21,25 @@ const ( logFile = "logs.txt" ) -func LogFile() (string, error) { +func LogFile() (*os.File, error) { dir, err := buildConfigDir() if err != nil { - return "", fmt.Errorf("config: error getting config directory: %v", err) + return nil, fmt.Errorf("config: error getting config directory: %v", err) } - return filepath.Join(dir, logFile), nil + err = os.MkdirAll(dir, 0750) + if err != nil { + return nil, fmt.Errorf("config: error making config directory: %v", err) + } + + fp := filepath.Join(dir, logFile) + + f, err := os.OpenFile(fp, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) + if err != nil { + return nil, fmt.Errorf("config: error opening log file: %v", err) + } + + return f, nil } func buildConfigDir() (string, error) {