slogger: rename Logger to Slogger
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
f23ec569c2
commit
1fb7479d8e
@ -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
|
||||
}
|
||||
|
||||
|
@ -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")),
|
||||
)
|
||||
|
@ -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)
|
||||
|
@ -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")),
|
||||
|
@ -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(
|
||||
|
@ -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")),
|
||||
|
@ -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(
|
||||
|
8
run.go
8
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")),
|
||||
|
@ -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...))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user