go: change how logger is initialised
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
* init in run.go * use slogger instead of slog in main * print different messages based on whether we're initialising or re-initialising slogger..
This commit is contained in:
parent
64f330b034
commit
9ac8810f67
@ -110,6 +110,7 @@ func Load(conf string, isPath bool) (*Config, error) {
|
|||||||
slogger := slogging.Logger()
|
slogger := slogging.Logger()
|
||||||
// initialise if not already initialised.
|
// initialise if not already initialised.
|
||||||
if slogger == nil {
|
if slogger == nil {
|
||||||
|
/// this should never happen, as the logger is initialised in run.go.
|
||||||
slogger = slogging.Init(true)
|
slogger = slogging.Init(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
7
main.go
7
main.go
@ -7,13 +7,16 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"git.dotya.ml/mirre-mt/pcmt/slogging"
|
"git.dotya.ml/mirre-mt/pcmt/slogging"
|
||||||
"golang.org/x/exp/slog"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := run()
|
err := run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l := slog.New(slog.NewJSONHandler(os.Stderr, slogging.Opts()))
|
l := slogging.Logger()
|
||||||
|
if l == nil {
|
||||||
|
l = slogging.Init(true)
|
||||||
|
}
|
||||||
|
|
||||||
l.Error("unrecoverable failure, stopping the app", "error", err)
|
l.Error("unrecoverable failure, stopping the app", "error", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
18
run.go
18
run.go
@ -97,6 +97,17 @@ func run() error { //nolint:gocognit
|
|||||||
printHeader()
|
printHeader()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slogger = slogging.Logger()
|
||||||
|
if slogger == nil {
|
||||||
|
slogger = slogging.Init(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
log = *slogger // local copy.
|
||||||
|
log.Logger = log.Logger.With(
|
||||||
|
// local attrs.
|
||||||
|
slog.Group("pcmt extra", slog.String("module", "run")),
|
||||||
|
)
|
||||||
|
|
||||||
// TODO: allow different configuration formats (toml, ini)
|
// TODO: allow different configuration formats (toml, ini)
|
||||||
// TODO: rename main.go to pcmt.go
|
// TODO: rename main.go to pcmt.go
|
||||||
// TODO: add flake.nix
|
// TODO: add flake.nix
|
||||||
@ -120,13 +131,6 @@ func run() error { //nolint:gocognit
|
|||||||
conf, host, port, devel, version,
|
conf, host, port, devel, version,
|
||||||
)
|
)
|
||||||
|
|
||||||
slogger = slogging.Logger() // init is performed in the config package.
|
|
||||||
log = *slogger // local copy.
|
|
||||||
log.Logger = log.Logger.With(
|
|
||||||
// local attrs.
|
|
||||||
slog.Group("pcmt extra", slog.String("module", "run")),
|
|
||||||
)
|
|
||||||
|
|
||||||
// expected connstring form for "github.com/xiaoqidun/entps":
|
// expected connstring form for "github.com/xiaoqidun/entps":
|
||||||
// "file:ent?mode=memory&cache=shared&_fk=1"
|
// "file:ent?mode=memory&cache=shared&_fk=1"
|
||||||
// and for the postgres driver "github.com/lib/pq":
|
// and for the postgres driver "github.com/lib/pq":
|
||||||
|
@ -21,9 +21,11 @@ const (
|
|||||||
LevelError
|
LevelError
|
||||||
)
|
)
|
||||||
|
|
||||||
var logger *Slogger
|
var (
|
||||||
|
logger *Slogger
|
||||||
var opts *slog.HandlerOptions
|
opts *slog.HandlerOptions
|
||||||
|
initialised = false
|
||||||
|
)
|
||||||
|
|
||||||
func Opts() *slog.HandlerOptions {
|
func Opts() *slog.HandlerOptions {
|
||||||
return opts
|
return opts
|
||||||
@ -46,7 +48,12 @@ func Init(jsonHandler bool) *Slogger {
|
|||||||
|
|
||||||
slog.SetDefault(logger.Logger)
|
slog.SetDefault(logger.Logger)
|
||||||
|
|
||||||
logger.Info("slog logger initialised")
|
if initialised {
|
||||||
|
logger.Info("slog logger reinitialised")
|
||||||
|
} else {
|
||||||
|
initialised = true
|
||||||
|
logger.Info("slog logger initialised")
|
||||||
|
}
|
||||||
|
|
||||||
return logger
|
return logger
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user