work in progress: logging with zerolog
This commit is contained in:
parent
82deb2c7f2
commit
fe4cdbb3b8
3 changed files with 17 additions and 5 deletions
|
@ -2,6 +2,7 @@ package logging
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"git.sr.ht/~cco/go-scopes/config"
|
"git.sr.ht/~cco/go-scopes/config"
|
||||||
|
@ -12,7 +13,7 @@ import (
|
||||||
|
|
||||||
type Logger = zerolog.Logger
|
type Logger = zerolog.Logger
|
||||||
|
|
||||||
var globalLogger Logger = log.Logger
|
var globalLogger *Logger = &log.Logger
|
||||||
|
|
||||||
type Cfg struct {
|
type Cfg struct {
|
||||||
*config.BaseCfg
|
*config.BaseCfg
|
||||||
|
@ -24,23 +25,30 @@ func Setup(ctx lib.Context, cfg *Cfg, home string) {
|
||||||
if cfg == nil {
|
if cfg == nil {
|
||||||
return // use unchanged predefined logger
|
return // use unchanged predefined logger
|
||||||
}
|
}
|
||||||
fmt.Println("logging:", home, cfg.Logfile)
|
home, _ = filepath.Abs(home)
|
||||||
fn := cfg.Logfile
|
fn := cfg.Logfile
|
||||||
|
// TODO: use: logger := New(cfg, home)
|
||||||
if fn == "" {
|
if fn == "" {
|
||||||
fn = filepath.Join(home, "log", "scopes.log")
|
fn = filepath.Join(home, "log", "scopes.log")
|
||||||
} else if !filepath.IsAbs(fn) {
|
} else if !filepath.IsAbs(fn) {
|
||||||
fn = filepath.Join(home, fn)
|
fn = filepath.Join(home, fn)
|
||||||
}
|
}
|
||||||
fmt.Println("logfile:", fn)
|
fmt.Println("logfile:", fn)
|
||||||
|
if f, err := os.OpenFile(fn, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0664); err == nil {
|
||||||
|
logger := zerolog.New(f).With().Timestamp().Logger()
|
||||||
|
*globalLogger = logger
|
||||||
|
} else {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(cfg *Cfg, home string) *Logger {
|
func New(cfg *Cfg, home string) *Logger {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetLogger(ctx lib.Context) *zerolog.Logger {
|
func GetLogger(ctx lib.Context) *Logger {
|
||||||
if ctx == nil {
|
if ctx == nil {
|
||||||
return &globalLogger
|
return globalLogger
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,6 +53,6 @@ const (
|
||||||
// file `settings.go` that should not be stored in code repository.
|
// file `settings.go` that should not be stored in code repository.
|
||||||
func Overrides() config.Settings {
|
func Overrides() config.Settings {
|
||||||
return config.Settings{
|
return config.Settings{
|
||||||
HOME: "tests",
|
//HOME: "tests",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
4
tests/log/scopes.log
Normal file
4
tests/log/scopes.log
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
{"level":"info","service":"config","time":"2023-06-26T10:21:47+02:00","message":"started"}
|
||||||
|
{"level":"info","service":"test-receiver","time":"2023-06-26T10:21:47+02:00","message":"started"}
|
||||||
|
{"level":"info","service":"config","time":"2023-06-26T10:28:53+02:00","message":"started"}
|
||||||
|
{"level":"info","service":"test-receiver","time":"2023-06-26T10:28:53+02:00","message":"started"}
|
Loading…
Add table
Reference in a new issue