diff --git a/app/app.go b/app/app.go index 5a81254..dad1a28 100644 --- a/app/app.go +++ b/app/app.go @@ -13,8 +13,8 @@ import ( type App struct { e *echo.Echo - slogger *slogging.Logger - logger slogging.Logger + slogger *slogging.Slogger + logger slogging.Slogger initialised bool embeds Embeds templatesPath string @@ -32,7 +32,7 @@ type Embeds struct { } // Init allows setting App's important fields - once. -func (a *App) Init(s *settings.Settings, logger *slogging.Logger, dbclient *ent.Client) error { +func (a *App) Init(s *settings.Settings, logger *slogging.Slogger, dbclient *ent.Client) error { if !a.initialised { e := echo.New() @@ -84,7 +84,7 @@ func (a *App) E() *echo.Echo { } // Logger returns app's pointer to the global logger instance. -func (a *App) Logger() *slogging.Logger { +func (a *App) Logger() *slogging.Slogger { return &a.logger } diff --git a/app/settings/settings.go b/app/settings/settings.go index dbd4115..7af9fc8 100644 --- a/app/settings/settings.go +++ b/app/settings/settings.go @@ -32,7 +32,7 @@ func New() *Settings { // the settings struct. Order of preference for values is (from higher to // lower) as follows: flag -> Env var -> configuration file. func (s *Settings) Consolidate(conf *config.Config, host *string, port *int, devel *bool, version string) { - log := *slogging.GetLogger() // have a local copy. + log := *slogging.Logger() // have a local copy. log.Logger = log.Logger.With( slog.Group("pcmt extra", slog.String("module", "app/settings")), ) diff --git a/config/config.go b/config/config.go index 37bfb19..40bf127 100644 --- a/config/config.go +++ b/config/config.go @@ -31,14 +31,14 @@ type Config struct { } } -var log slogging.Logger +var log slogging.Slogger func LoadConfig(conf string, isPath bool) (*Config, error) { var config Config var err error - slogger := slogging.GetLogger() + slogger := slogging.Logger() // initialise if not already initialised. if slogger == nil { slogger = slogging.Init(true) diff --git a/handlers/config.go b/handlers/config.go index 0c29ccb..c45a671 100644 --- a/handlers/config.go +++ b/handlers/config.go @@ -9,12 +9,12 @@ import ( var ( setting *settings.Settings appver string - slogger *slogging.Logger - log slogging.Logger + slogger *slogging.Slogger + log slogging.Slogger ) func InitHandlers(s *settings.Settings) { - slogger = slogging.GetLogger() + slogger = slogging.Logger() log = *slogger // have a local copy. log.Logger = log.Logger.With( slog.Group("pcmt extra", slog.String("module", "handlers")), diff --git a/modules/db/db.go b/modules/db/db.go index 30bd4b2..35525b3 100644 --- a/modules/db/db.go +++ b/modules/db/db.go @@ -14,7 +14,7 @@ type CtxKey struct{} // DropAll deletes an re-creates the whole db. func DropAll(ctx context.Context, client *ent.Client) error { - slogger := ctx.Value(CtxKey{}).(*slogging.Logger) + slogger := ctx.Value(CtxKey{}).(*slogging.Slogger) log := *slogger log.Logger = log.Logger.With( @@ -37,7 +37,7 @@ func DropAll(ctx context.Context, client *ent.Client) error { // IsSetUp deletes the whole db. func IsSetUp(ctx context.Context, client *ent.Client) (bool, error) { - slogger := ctx.Value(CtxKey{}).(*slogging.Logger) + slogger := ctx.Value(CtxKey{}).(*slogging.Slogger) log := *slogger log.Logger = log.Logger.With( diff --git a/modules/template/template.go b/modules/template/template.go index 3702a4c..cb04af8 100644 --- a/modules/template/template.go +++ b/modules/template/template.go @@ -21,9 +21,9 @@ var ( bluemondayPolicy = bluemonday.UGCPolicy() setting *settings.Settings // global logger. - slogger *slogging.Logger + slogger *slogging.Slogger // local logger copy. - log slogging.Logger + log slogging.Slogger // path to "templates" folder (empty if LiveMode). tmplPath string tmplSuffix = ".tmpl" @@ -32,7 +32,7 @@ var ( // Init saves settings and embedded templates to package-level state and also // "initialises" template rendering. func Init(s *settings.Settings, tmpls fs.FS) { - slogger = slogging.GetLogger() + slogger = slogging.Logger() log = *slogger // have a local copy. log.Logger = log.Logger.With( slog.Group("pcmt extra", slog.String("module", "modules/template")), diff --git a/modules/user/user.go b/modules/user/user.go index b468e86..dcdc48e 100644 --- a/modules/user/user.go +++ b/modules/user/user.go @@ -26,7 +26,7 @@ type User struct { // CreateUser adds a user entry to the database. func CreateUser(ctx context.Context, client *ent.Client, email, username, password string) (*ent.User, error) { - slogger := ctx.Value(CtxKey{}).(*slogging.Logger) + slogger := ctx.Value(CtxKey{}).(*slogging.Slogger) log := *slogger log.Logger = log.Logger.With( @@ -66,7 +66,7 @@ func CreateUser(ctx context.Context, client *ent.Client, email, username, passwo } func QueryUser(ctx context.Context, client *ent.Client, username string) (*ent.User, error) { - slogger := ctx.Value(CtxKey{}).(*slogging.Logger) + slogger := ctx.Value(CtxKey{}).(*slogging.Slogger) log := *slogger log.Logger = log.Logger.With( @@ -95,7 +95,7 @@ func QueryUser(ctx context.Context, client *ent.Client, username string) (*ent.U // Exists determines whether the username OR email in question was previously // used to register a user. func Exists(ctx context.Context, client *ent.Client, username, email string) (bool, error) { - slogger := ctx.Value(CtxKey{}).(*slogging.Logger) + slogger := ctx.Value(CtxKey{}).(*slogging.Slogger) log := *slogger log.Logger = log.Logger.With( @@ -137,7 +137,7 @@ func Exists(ctx context.Context, client *ent.Client, username, email string) (bo // available or taken, returns a bool and an error, which will be nil unless // the error is one of IsNot{Found,Singular}. func UsernameExists(ctx context.Context, client *ent.Client, username string) (bool, error) { //nolint:dupl - slogger := ctx.Value(CtxKey{}).(*slogging.Logger) + slogger := ctx.Value(CtxKey{}).(*slogging.Slogger) log := *slogger log.Logger = log.Logger.With( @@ -177,7 +177,7 @@ func UsernameExists(ctx context.Context, client *ent.Client, username string) (b // used; returns a bool and an error, which will be nil unless the error is not // one of IsNot{Found,Singular}. func EmailExists(ctx context.Context, client *ent.Client, email string) (bool, error) { //nolint:dupl - slogger := ctx.Value(CtxKey{}).(*slogging.Logger) + slogger := ctx.Value(CtxKey{}).(*slogging.Slogger) log := *slogger log.Logger = log.Logger.With( diff --git a/run.go b/run.go index c963c1e..bde42e3 100644 --- a/run.go +++ b/run.go @@ -63,9 +63,9 @@ var ( license = flag.Bool("license", false, "Print licensing information and exit") version = "dev" // the global logger. - slogger *slogging.Logger + slogger *slogging.Slogger // local logger instance. - log slogging.Logger + log slogging.Slogger ) func run() error { @@ -98,8 +98,8 @@ func run() error { conf, host, port, devel, version, ) - slogger = slogging.GetLogger() // init is performed in the config package. - log = *slogger // local copy. + 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")), diff --git a/slogging/log.go b/slogging/log.go index 47c4a7e..d354ae4 100644 --- a/slogging/log.go +++ b/slogging/log.go @@ -7,7 +7,7 @@ import ( "golang.org/x/exp/slog" ) -type Logger struct { +type Slogger struct { *slog.Logger } @@ -18,23 +18,23 @@ const ( LevelError ) -var logger *Logger +var logger *Slogger var opts slog.HandlerOptions -func GetLogger() *Logger { +func Logger() *Slogger { return logger } -func Init(jsonHandler bool) *Logger { +func Init(jsonHandler bool) *Slogger { opts = slog.HandlerOptions{ Level: slog.LevelInfo, } if jsonHandler { - logger = &Logger{slog.New(opts.NewJSONHandler(os.Stderr))} + logger = &Slogger{slog.New(opts.NewJSONHandler(os.Stderr))} } else { - logger = &Logger{slog.New(opts.NewTextHandler(os.Stderr))} + logger = &Slogger{slog.New(opts.NewTextHandler(os.Stderr))} } slog.SetDefault(logger.Logger) @@ -46,7 +46,7 @@ func Init(jsonHandler bool) *Logger { // SetLevel allows setting log level and returns the pointer to the re-created // logger. -func SetLevel(level int) *Logger { +func SetLevel(level int) *Slogger { switch level { case LevelInfo: opts.Level = slog.LevelInfo @@ -67,9 +67,9 @@ func SetLevel(level int) *Logger { } if _, ok := logger.Handler().(*slog.JSONHandler); ok { - logger = &Logger{slog.New(opts.NewJSONHandler(os.Stderr))} + logger = &Slogger{slog.New(opts.NewJSONHandler(os.Stderr))} } else { - logger = &Logger{slog.New(opts.NewTextHandler(os.Stderr))} + logger = &Slogger{slog.New(opts.NewTextHandler(os.Stderr))} } slog.SetDefault(logger.Logger) @@ -77,18 +77,18 @@ func SetLevel(level int) *Logger { return logger } -func (l *Logger) Debugf(msg string, args ...any) { +func (l *Slogger) Debugf(msg string, args ...any) { l.Debug(fmt.Sprintf(msg, args...)) } -func (l *Logger) Infof(msg string, args ...any) { +func (l *Slogger) Infof(msg string, args ...any) { l.Info(fmt.Sprintf(msg, args...)) } -func (l *Logger) Warnf(msg string, args ...any) { +func (l *Slogger) Warnf(msg string, args ...any) { l.Warn(fmt.Sprintf(msg, args...)) } -func (l *Logger) Errorf(msg string, args ...any) { +func (l *Slogger) Errorf(msg string, args ...any) { l.Error(fmt.Sprintf(msg, args...)) }