Skip to content

Commit

Permalink
refactor: function to init logs writer in a separated file
Browse files Browse the repository at this point in the history
  • Loading branch information
luisnquin committed Feb 3, 2024
1 parent 6678071 commit fc30fdc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
26 changes: 26 additions & 0 deletions cmd/nao/log.go
Original file line number Diff line number Diff line change
@@ -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
}
18 changes: 6 additions & 12 deletions cmd/nao/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"os"
"os/user"
"path"
"runtime"

"github.com/luisnquin/nao/v3/internal"
Expand All @@ -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() {
Expand All @@ -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().
Expand Down

0 comments on commit fc30fdc

Please sign in to comment.