slogger: rename Logger to Slogger
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
leo 2023-05-11 17:06:20 +02:00
parent f23ec569c2
commit 1fb7479d8e
Signed by: wanderer
SSH Key Fingerprint: SHA256:Dp8+iwKHSlrMEHzE3bJnPng70I7LEsa3IJXRH/U+idQ
9 changed files with 37 additions and 37 deletions

@ -13,8 +13,8 @@ import (
type App struct { type App struct {
e *echo.Echo e *echo.Echo
slogger *slogging.Logger slogger *slogging.Slogger
logger slogging.Logger logger slogging.Slogger
initialised bool initialised bool
embeds Embeds embeds Embeds
templatesPath string templatesPath string
@ -32,7 +32,7 @@ type Embeds struct {
} }
// Init allows setting App's important fields - once. // 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 { if !a.initialised {
e := echo.New() e := echo.New()
@ -84,7 +84,7 @@ func (a *App) E() *echo.Echo {
} }
// Logger returns app's pointer to the global logger instance. // 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 return &a.logger
} }

@ -32,7 +32,7 @@ func New() *Settings {
// the settings struct. Order of preference for values is (from higher to // the settings struct. Order of preference for values is (from higher to
// lower) as follows: flag -> Env var -> configuration file. // lower) as follows: flag -> Env var -> configuration file.
func (s *Settings) Consolidate(conf *config.Config, host *string, port *int, devel *bool, version string) { 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( log.Logger = log.Logger.With(
slog.Group("pcmt extra", slog.String("module", "app/settings")), slog.Group("pcmt extra", slog.String("module", "app/settings")),
) )

@ -31,14 +31,14 @@ type Config struct {
} }
} }
var log slogging.Logger var log slogging.Slogger
func LoadConfig(conf string, isPath bool) (*Config, error) { func LoadConfig(conf string, isPath bool) (*Config, error) {
var config Config var config Config
var err error var err error
slogger := slogging.GetLogger() slogger := slogging.Logger()
// initialise if not already initialised. // initialise if not already initialised.
if slogger == nil { if slogger == nil {
slogger = slogging.Init(true) slogger = slogging.Init(true)

@ -9,12 +9,12 @@ import (
var ( var (
setting *settings.Settings setting *settings.Settings
appver string appver string
slogger *slogging.Logger slogger *slogging.Slogger
log slogging.Logger log slogging.Slogger
) )
func InitHandlers(s *settings.Settings) { func InitHandlers(s *settings.Settings) {
slogger = slogging.GetLogger() slogger = slogging.Logger()
log = *slogger // have a local copy. log = *slogger // have a local copy.
log.Logger = log.Logger.With( log.Logger = log.Logger.With(
slog.Group("pcmt extra", slog.String("module", "handlers")), slog.Group("pcmt extra", slog.String("module", "handlers")),

@ -14,7 +14,7 @@ type CtxKey struct{}
// DropAll deletes an re-creates the whole db. // DropAll deletes an re-creates the whole db.
func DropAll(ctx context.Context, client *ent.Client) error { func DropAll(ctx context.Context, client *ent.Client) error {
slogger := ctx.Value(CtxKey{}).(*slogging.Logger) slogger := ctx.Value(CtxKey{}).(*slogging.Slogger)
log := *slogger log := *slogger
log.Logger = log.Logger.With( log.Logger = log.Logger.With(
@ -37,7 +37,7 @@ func DropAll(ctx context.Context, client *ent.Client) error {
// IsSetUp deletes the whole db. // IsSetUp deletes the whole db.
func IsSetUp(ctx context.Context, client *ent.Client) (bool, error) { 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 := *slogger
log.Logger = log.Logger.With( log.Logger = log.Logger.With(

@ -21,9 +21,9 @@ var (
bluemondayPolicy = bluemonday.UGCPolicy() bluemondayPolicy = bluemonday.UGCPolicy()
setting *settings.Settings setting *settings.Settings
// global logger. // global logger.
slogger *slogging.Logger slogger *slogging.Slogger
// local logger copy. // local logger copy.
log slogging.Logger log slogging.Slogger
// path to "templates" folder (empty if LiveMode). // path to "templates" folder (empty if LiveMode).
tmplPath string tmplPath string
tmplSuffix = ".tmpl" tmplSuffix = ".tmpl"
@ -32,7 +32,7 @@ var (
// Init saves settings and embedded templates to package-level state and also // Init saves settings and embedded templates to package-level state and also
// "initialises" template rendering. // "initialises" template rendering.
func Init(s *settings.Settings, tmpls fs.FS) { func Init(s *settings.Settings, tmpls fs.FS) {
slogger = slogging.GetLogger() slogger = slogging.Logger()
log = *slogger // have a local copy. log = *slogger // have a local copy.
log.Logger = log.Logger.With( log.Logger = log.Logger.With(
slog.Group("pcmt extra", slog.String("module", "modules/template")), slog.Group("pcmt extra", slog.String("module", "modules/template")),

@ -26,7 +26,7 @@ type User struct {
// CreateUser adds a user entry to the database. // CreateUser adds a user entry to the database.
func CreateUser(ctx context.Context, client *ent.Client, email, username, password string) (*ent.User, error) { 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 := *slogger
log.Logger = log.Logger.With( 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) { 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 := *slogger
log.Logger = log.Logger.With( 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 // Exists determines whether the username OR email in question was previously
// used to register a user. // used to register a user.
func Exists(ctx context.Context, client *ent.Client, username, email string) (bool, error) { 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 := *slogger
log.Logger = log.Logger.With( 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 // available or taken, returns a bool and an error, which will be nil unless
// the error is one of IsNot{Found,Singular}. // the error is one of IsNot{Found,Singular}.
func UsernameExists(ctx context.Context, client *ent.Client, username string) (bool, error) { //nolint:dupl 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 := *slogger
log.Logger = log.Logger.With( 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 // used; returns a bool and an error, which will be nil unless the error is not
// one of IsNot{Found,Singular}. // one of IsNot{Found,Singular}.
func EmailExists(ctx context.Context, client *ent.Client, email string) (bool, error) { //nolint:dupl 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 := *slogger
log.Logger = log.Logger.With( log.Logger = log.Logger.With(

8
run.go

@ -63,9 +63,9 @@ var (
license = flag.Bool("license", false, "Print licensing information and exit") license = flag.Bool("license", false, "Print licensing information and exit")
version = "dev" version = "dev"
// the global logger. // the global logger.
slogger *slogging.Logger slogger *slogging.Slogger
// local logger instance. // local logger instance.
log slogging.Logger log slogging.Slogger
) )
func run() error { func run() error {
@ -98,8 +98,8 @@ func run() error {
conf, host, port, devel, version, conf, host, port, devel, version,
) )
slogger = slogging.GetLogger() // init is performed in the config package. slogger = slogging.Logger() // init is performed in the config package.
log = *slogger // local copy. log = *slogger // local copy.
log.Logger = log.Logger.With( log.Logger = log.Logger.With(
// local attrs. // local attrs.
slog.Group("pcmt extra", slog.String("module", "run")), slog.Group("pcmt extra", slog.String("module", "run")),

@ -7,7 +7,7 @@ import (
"golang.org/x/exp/slog" "golang.org/x/exp/slog"
) )
type Logger struct { type Slogger struct {
*slog.Logger *slog.Logger
} }
@ -18,23 +18,23 @@ const (
LevelError LevelError
) )
var logger *Logger var logger *Slogger
var opts slog.HandlerOptions var opts slog.HandlerOptions
func GetLogger() *Logger { func Logger() *Slogger {
return logger return logger
} }
func Init(jsonHandler bool) *Logger { func Init(jsonHandler bool) *Slogger {
opts = slog.HandlerOptions{ opts = slog.HandlerOptions{
Level: slog.LevelInfo, Level: slog.LevelInfo,
} }
if jsonHandler { if jsonHandler {
logger = &Logger{slog.New(opts.NewJSONHandler(os.Stderr))} logger = &Slogger{slog.New(opts.NewJSONHandler(os.Stderr))}
} else { } else {
logger = &Logger{slog.New(opts.NewTextHandler(os.Stderr))} logger = &Slogger{slog.New(opts.NewTextHandler(os.Stderr))}
} }
slog.SetDefault(logger.Logger) 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 // SetLevel allows setting log level and returns the pointer to the re-created
// logger. // logger.
func SetLevel(level int) *Logger { func SetLevel(level int) *Slogger {
switch level { switch level {
case LevelInfo: case LevelInfo:
opts.Level = slog.LevelInfo opts.Level = slog.LevelInfo
@ -67,9 +67,9 @@ func SetLevel(level int) *Logger {
} }
if _, ok := logger.Handler().(*slog.JSONHandler); ok { if _, ok := logger.Handler().(*slog.JSONHandler); ok {
logger = &Logger{slog.New(opts.NewJSONHandler(os.Stderr))} logger = &Slogger{slog.New(opts.NewJSONHandler(os.Stderr))}
} else { } else {
logger = &Logger{slog.New(opts.NewTextHandler(os.Stderr))} logger = &Slogger{slog.New(opts.NewTextHandler(os.Stderr))}
} }
slog.SetDefault(logger.Logger) slog.SetDefault(logger.Logger)
@ -77,18 +77,18 @@ func SetLevel(level int) *Logger {
return 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...)) 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...)) 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...)) 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...)) l.Error(fmt.Sprintf(msg, args...))
} }