Skip to content

Commit

Permalink
fix(logging): 🐛 (again) create directory for log file if not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Aug 16, 2024
1 parent d244aa9 commit e60a7f0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
10 changes: 5 additions & 5 deletions internal/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package logging

import (
"errors"
"fmt"
"log/slog"
_ "net/http/pprof" // #nosec G108
Expand All @@ -19,8 +20,6 @@ import (
const (
LevelTrace = slog.Level(-8)
LevelFatal = slog.Level(12)

defaultPerms = 0o644
)

//nolint:misspell
Expand Down Expand Up @@ -104,14 +103,15 @@ func openLogFile(logFile string) (*os.File, error) {
logDir := filepath.Base(logFile)
// Create the log directory if it does not exist.
_, err := os.Stat(logDir)
if os.IsNotExist(err) {

if err == nil || errors.Is(err, os.ErrNotExist) {
err = os.MkdirAll(logDir, os.ModePerm)
if err != nil {
return nil, fmt.Errorf("unable to log file directory %s: %w", logDir, err)
return nil, fmt.Errorf("unable to create log file directory %s: %w", logDir, err)
}
}
// Open the log file.
logFileHandle, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, defaultPerms)
logFileHandle, err := os.Create(logFile)
if err != nil {
return nil, fmt.Errorf("unable to open log file: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (r *UpgradeCmd) Run(ctx *Context) error {
if ctx.NoLogFile {
logFile = ""
} else {
logFile = filepath.Join(xdg.DataHome, "go-hass-agent-upgrade.log")
logFile = filepath.Join(xdg.ConfigHome, ctx.AppID, "upgrade.log")
}

logger := logging.New(ctx.LogLevel, logFile)
Expand Down

0 comments on commit e60a7f0

Please sign in to comment.