Improve client logging.
This commit is contained in:
@@ -21,6 +21,7 @@ type database struct {
|
||||
options core.Options
|
||||
app *application.Application
|
||||
db *sqlx.DB
|
||||
logger *slog.Logger
|
||||
migrations map[string]fs.FS
|
||||
dbPath string
|
||||
version int64
|
||||
@@ -51,7 +52,7 @@ func (d *database) Configure() error {
|
||||
|
||||
d.db = db
|
||||
|
||||
slog.Info("Database opened.", "path", d.dbPath)
|
||||
d.logger.Info("Database opened.", "path", d.dbPath)
|
||||
|
||||
if err := d.initializeSysInfoHandler(); err != nil {
|
||||
return fmt.Errorf("configure: %w", err)
|
||||
@@ -93,6 +94,10 @@ func (d *database) ConnectDependencies() error {
|
||||
}
|
||||
|
||||
func (d *database) Initialize() error {
|
||||
d.logger = d.app.NewLogger("service", core.ServiceNameDatabase)
|
||||
|
||||
d.logger.Info("Initializing...")
|
||||
|
||||
d.migrations = make(map[string]fs.FS, 0)
|
||||
|
||||
return nil
|
||||
|
@@ -2,7 +2,6 @@ package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
@@ -24,7 +23,7 @@ func (d *database) configureDBPath() error {
|
||||
|
||||
d.dbPath = filepath.Join(rootDir, "database.sqlite3")
|
||||
|
||||
slog.Info("Database path configured.", "path", d.dbPath)
|
||||
d.logger.Info("Database path configured.", "path", d.dbPath)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@@ -9,6 +9,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"bunker/client/internal/services/core"
|
||||
"bunker/commons"
|
||||
|
||||
"github.com/pressly/goose/v3"
|
||||
)
|
||||
@@ -16,7 +17,7 @@ import (
|
||||
var errMigrationsAlreadyRegistered = errors.New("migrations already registered")
|
||||
|
||||
func (d *database) applyMigrations() error {
|
||||
slog.Info("Migrating database...")
|
||||
d.logger.Info("Migrating database...")
|
||||
|
||||
modules := make([]string, 0)
|
||||
|
||||
@@ -28,8 +29,11 @@ func (d *database) applyMigrations() error {
|
||||
|
||||
_ = goose.SetDialect(string(goose.DialectSQLite3))
|
||||
|
||||
gooseLogger := commons.NewGooseLogger(d.logger)
|
||||
goose.SetLogger(gooseLogger)
|
||||
|
||||
for _, module := range modules {
|
||||
slog.Info("Migrating database for module...", "module", module)
|
||||
d.logger.Info("Migrating database for module...", "module", module)
|
||||
|
||||
goose.SetBaseFS(d.migrations[module])
|
||||
goose.SetTableName(strings.ReplaceAll(module, "/", "_") + "_migrations")
|
||||
@@ -45,7 +49,7 @@ func (d *database) applyMigrations() error {
|
||||
|
||||
d.version += moduleDBVersion
|
||||
|
||||
slog.Info(
|
||||
d.logger.Info(
|
||||
"Database for module migrated to latest version",
|
||||
"module", module,
|
||||
"module_db_version", moduleDBVersion,
|
||||
@@ -53,7 +57,7 @@ func (d *database) applyMigrations() error {
|
||||
)
|
||||
}
|
||||
|
||||
slog.Info("Database migrated.", "version", d.version)
|
||||
d.logger.Info("Database migrated.", "version", d.version)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@@ -3,7 +3,6 @@ package database
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
|
||||
"bunker/client/internal/services/core"
|
||||
@@ -15,7 +14,7 @@ func (d *database) Exec(ctx context.Context, query string, params ...interface{}
|
||||
query = d.db.Rebind(query)
|
||||
}
|
||||
|
||||
slog.Debug("Executing query.", "query", query, "params", fmt.Sprintf("%+v", params), "module", "core/database")
|
||||
d.logger.Debug("Executing query.", "query", query, "params", fmt.Sprintf("%+v", params), "module", "core/database")
|
||||
|
||||
if _, err := d.db.ExecContext(ctx, query, params...); err != nil {
|
||||
return fmt.Errorf("%w: failed to Exec(): %w", core.ErrDatabase, err)
|
||||
@@ -30,7 +29,7 @@ func (d *database) Get(ctx context.Context, target interface{}, query string, pa
|
||||
query = d.db.Rebind(query)
|
||||
}
|
||||
|
||||
slog.Debug(
|
||||
d.logger.Debug(
|
||||
"Getting single data from database with query.",
|
||||
"query", query,
|
||||
"params", fmt.Sprintf("%+v", params),
|
||||
@@ -50,7 +49,7 @@ func (d *database) NamedExec(ctx context.Context, query string, param interface{
|
||||
query = d.db.Rebind(query)
|
||||
}
|
||||
|
||||
slog.Debug("Executing named query.", "query", query, "params", fmt.Sprintf("%+v", param), "module", "core/database")
|
||||
d.logger.Debug("Executing named query.", "query", query, "params", fmt.Sprintf("%+v", param), "module", "core/database")
|
||||
|
||||
if _, err := d.db.NamedExecContext(ctx, query, param); err != nil {
|
||||
return fmt.Errorf("%w: failed to NamedExec(): %w", core.ErrDatabase, err)
|
||||
@@ -65,7 +64,7 @@ func (d *database) Select(ctx context.Context, target interface{}, query string,
|
||||
query = d.db.Rebind(query)
|
||||
}
|
||||
|
||||
slog.Debug(
|
||||
d.logger.Debug(
|
||||
"Selecting from database with query.",
|
||||
"query", query,
|
||||
"params", fmt.Sprintf("%+v", params),
|
||||
|
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
type transaction struct {
|
||||
transaction *sqlx.Tx
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func (d *database) Transaction(ctx context.Context) (core.DatabaseTransaction, error) {
|
||||
@@ -22,6 +23,7 @@ func (d *database) Transaction(ctx context.Context) (core.DatabaseTransaction, e
|
||||
|
||||
txHandler := &transaction{
|
||||
transaction: txn,
|
||||
logger: d.logger.With("module", "transactioner"),
|
||||
}
|
||||
|
||||
return txHandler, nil
|
||||
@@ -30,7 +32,7 @@ func (d *database) Transaction(ctx context.Context) (core.DatabaseTransaction, e
|
||||
func (t *transaction) Apply(steps ...core.TransactionFunc) error {
|
||||
for stepNumber, stepFunc := range steps {
|
||||
if err := stepFunc(t.transaction); err != nil {
|
||||
slog.Error(
|
||||
t.logger.Error(
|
||||
"Error occurred.",
|
||||
"step", stepNumber,
|
||||
"error", err.Error(),
|
||||
@@ -39,7 +41,7 @@ func (t *transaction) Apply(steps ...core.TransactionFunc) error {
|
||||
)
|
||||
|
||||
if rollbackErr := t.transaction.Rollback(); rollbackErr != nil {
|
||||
slog.Error(
|
||||
t.logger.Error(
|
||||
"Transaction rollback failed.",
|
||||
"error", err.Error(),
|
||||
"module", "core/database",
|
||||
@@ -54,7 +56,7 @@ func (t *transaction) Apply(steps ...core.TransactionFunc) error {
|
||||
}
|
||||
|
||||
if err := t.transaction.Commit(); err != nil {
|
||||
slog.Error(
|
||||
t.logger.Error(
|
||||
"Transaction commit failed.",
|
||||
"error", err.Error(),
|
||||
"module", "core/database",
|
||||
@@ -62,7 +64,7 @@ func (t *transaction) Apply(steps ...core.TransactionFunc) error {
|
||||
)
|
||||
|
||||
if rollbackErr := t.transaction.Rollback(); rollbackErr != nil {
|
||||
slog.Error(
|
||||
t.logger.Error(
|
||||
"Transaction rollback failed.",
|
||||
"error", err.Error(),
|
||||
"module", "core/database",
|
||||
|
Reference in New Issue
Block a user