Files
projectreshoot/cmd/projectreshoot/logger.go
2026-01-02 19:50:10 +11:00

47 lines
1000 B
Go

package main
import (
"io"
"os"
"git.haelnorr.com/h/golib/hlog"
"github.com/pkg/errors"
)
// Take in the desired logOutput and a console writer to use
func setupLogger(logLevel hlog.Level, logOutput string, w *io.Writer, logDirectory string) (*hlog.Logger, error) {
// Setup the logfile
var logfile *os.File = nil
if logOutput == "both" || logOutput == "file" {
logfile, err := hlog.NewLogFile(logDirectory)
if err != nil {
return nil, errors.Wrap(err, "hlog")
}
defer logfile.Close()
}
// Setup the console writer
var consoleWriter io.Writer
if logOutput == "both" || logOutput == "console" {
if w != nil {
consoleWriter = *w
} else {
if logOutput == "console" {
return nil, errors.New("Console logging specified as sole method but no writer provided")
}
}
}
// Setup the logger
logger, err := hlog.NewLogger(
logLevel,
consoleWriter,
logfile,
logDirectory,
)
if err != nil {
return nil, errors.Wrap(err, "hlog")
}
return logger, nil
}