Files
lndhub.go/lib/logger.go
Michael Bumann f3a05ac268 Update log format
if no extension is given we add the date.
if an extension is given we use that path (assuming that's the exact file the user wants)
2022-01-17 10:39:14 +01:00

43 lines
768 B
Go

package lib
import (
"os"
"path/filepath"
"time"
"github.com/labstack/gommon/log"
"github.com/ziflex/lecho/v3"
)
func Logger(logFilePath string) *lecho.Logger {
logger := lecho.New(
os.Stdout, // default to STDOUT
lecho.WithLevel(log.DEBUG),
lecho.WithTimestamp(),
)
// check if a log file config is set
if logFilePath != "" {
file, err := GetLoggingFile(logFilePath)
if err != nil {
logger.Error("failed to create logging file: %v", err)
}
logger.SetOutput(file)
}
return logger
}
func GetLoggingFile(path string) (*os.File, error) {
extension := filepath.Ext(path)
if extension == "" {
path = path + time.Now().Format("-2006-01-02") + ".log"
}
f, err := os.Create(path)
if err != nil {
return nil, err
}
return f, err
}