From fc30fdc798c089a3162aeb7e9332270301b2a7f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Qui=C3=B1ones?= Date: Fri, 2 Feb 2024 22:59:26 -0500 Subject: [PATCH] refactor: function to init logs writer in a separated file --- cmd/nao/log.go | 26 ++++++++++++++++++++++++++ cmd/nao/main.go | 18 ++++++------------ 2 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 cmd/nao/log.go diff --git a/cmd/nao/log.go b/cmd/nao/log.go new file mode 100644 index 0000000..74704c0 --- /dev/null +++ b/cmd/nao/log.go @@ -0,0 +1,26 @@ +package main + +import ( + "io" + "os" + "path" + + "github.com/luisnquin/nao/v3/internal" +) + +type LogsWriter interface { + io.StringWriter + io.Writer +} + +func getAppLogsWriter() LogsWriter { + fp := path.Join(os.TempDir(), "nao.log") + flags := os.O_CREATE | os.O_RDWR | os.O_APPEND + + lf, err := os.OpenFile(fp, flags, internal.PermReadWrite) + if err != nil { + panic(err) + } + + return lf +} diff --git a/cmd/nao/main.go b/cmd/nao/main.go index ac899c7..da9f2c0 100644 --- a/cmd/nao/main.go +++ b/cmd/nao/main.go @@ -6,7 +6,6 @@ import ( "io" "os" "os/user" - "path" "runtime" "github.com/luisnquin/nao/v3/internal" @@ -20,9 +19,8 @@ import ( const DEFAULT_VERSION = "unversioned" var ( - version = DEFAULT_VERSION - commit string - date string + version = DEFAULT_VERSION + commit, date string ) func main() { @@ -33,19 +31,15 @@ func main() { } }() - logFile, err := os.OpenFile(path.Join(os.TempDir(), "nao.log"), os.O_CREATE|os.O_RDWR|os.O_APPEND, internal.PermReadWrite) - if err != nil { - panic(err) - } - - logFile.WriteString("\n\n") + lw := getAppLogsWriter() + lw.WriteString("\n\n") var logger zerolog.Logger if internal.Debug { - logger = zerolog.New(io.MultiWriter(logFile, os.Stderr)) + logger = zerolog.New(io.MultiWriter(lw, os.Stderr)) } else { - logger = zerolog.New(logFile) + logger = zerolog.New(lw) } logger.Trace().