[go: nahoru, domu]

Skip to content

Commit

Permalink
Adds option for omitting db log args, groups under db attr key (#2136)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickzelei committed Jun 13, 2024
1 parent 82373e4 commit 24746ad
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 68 deletions.
2 changes: 1 addition & 1 deletion backend/internal/nucleusdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewFromConfig(config *ConnectConfig) (*NucleusDb, error) {
return nil, err
}
pgxconfig.ConnConfig.Tracer = &tracelog.TraceLog{
Logger: pgxslog.NewLogger(slog.Default()),
Logger: pgxslog.NewLogger(slog.Default(), pgxslog.GetShouldOmitArgs()),
LogLevel: pgxslog.GetDatabaseLogLevel(),
}

Expand Down
23 changes: 19 additions & 4 deletions backend/internal/pgx-slog/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import (
// Copied to avoid adding another go mod dependency which also allows us to ensure this is always compatible with our version of pgx

type Logger struct {
l *slog.Logger
l *slog.Logger
omitArgs bool
}

func NewLogger(l *slog.Logger) *Logger {
return &Logger{l: l}
func NewLogger(l *slog.Logger, omitArgs bool) *Logger {
return &Logger{l: l, omitArgs: omitArgs}
}

func (l *Logger) Log(ctx context.Context, level tracelog.LogLevel, msg string, data map[string]any) {
Expand All @@ -27,6 +28,9 @@ func (l *Logger) Log(ctx context.Context, level tracelog.LogLevel, msg string, d

attrs := make([]slog.Attr, 0, len(data))
for k, v := range data {
if k == "args" && l.omitArgs {
continue
}
attrs = append(attrs, slog.Any(k, v))
}

Expand All @@ -47,7 +51,14 @@ func (l *Logger) Log(ctx context.Context, level tracelog.LogLevel, msg string, d
lvl = slog.LevelError
attrs = append(attrs, slog.Any("INVALID_PGX_LOG_LEVEL", level))
}
l.l.LogAttrs(ctx, lvl, msg, attrs...)

attrAny := make([]any, len(attrs))
for i, attr := range attrs {
attrAny[i] = attr
}
dbGroup := slog.Group("db", attrAny...)

l.l.LogAttrs(ctx, lvl, msg, dbGroup)
}

// Returns a tracelog.LogLevel as configured by the environment
Expand All @@ -59,3 +70,7 @@ func GetDatabaseLogLevel() tracelog.LogLevel {
}
return ll
}

func GetShouldOmitArgs() bool {
return viper.GetBool("DB_OMIT_ARGS")
}
4 changes: 2 additions & 2 deletions backend/pkg/sqlconnect/pgpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (s *PgPool) Open(ctx context.Context) (pg_queries.DBTX, error) {
return nil, fmt.Errorf("unable to parse dsn into pg config: %w", err)
}
config.ConnConfig.Tracer = &tracelog.TraceLog{
Logger: pgxslog.NewLogger(s.logger),
Logger: pgxslog.NewLogger(s.logger, pgxslog.GetShouldOmitArgs()),
LogLevel: pgxslog.GetDatabaseLogLevel(),
}
config.ConnConfig.DefaultQueryExecMode = pgx.QueryExecModeExec
Expand All @@ -88,7 +88,7 @@ func (s *PgPool) Open(ctx context.Context) (pg_queries.DBTX, error) {
return nil, err
}
config.ConnConfig.Tracer = &tracelog.TraceLog{
Logger: pgxslog.NewLogger(s.logger),
Logger: pgxslog.NewLogger(s.logger, pgxslog.GetShouldOmitArgs()),
LogLevel: pgxslog.GetDatabaseLogLevel(),
}
config.ConnConfig.DefaultQueryExecMode = pgx.QueryExecModeExec
Expand Down
2 changes: 1 addition & 1 deletion backend/pkg/sqlmanager/sql-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func (s *SqlManager) NewSqlDbFromUrl(
return nil, err
}
pgxconfig.ConnConfig.Tracer = &tracelog.TraceLog{
Logger: pgxslog.NewLogger(slog.Default()),
Logger: pgxslog.NewLogger(slog.Default(), pgxslog.GetShouldOmitArgs()),
LogLevel: pgxslog.GetDatabaseLogLevel(),
}
pgxconfig.ConnConfig.DefaultQueryExecMode = pgx.QueryExecModeExec
Expand Down
2 changes: 2 additions & 0 deletions compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ services:
- DB_MIGRATIONS_TABLE=neosync_api_schema_migrations
- DB_MIGRATIONS_TABLE_QUOTED=false

- DB_LOG_LEVEL=ERROR

- AUTH_ENABLED=false

networks:
Expand Down
122 changes: 63 additions & 59 deletions docs/docs/deploy/environment-variables.md

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion docs/docs/guides/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,10 @@ Valid options for `DB_LOG_LEVEL` are (not case sensitive):
- error
- none

**Note** - When turning on database logging, the statements **include arguments**. So if planning to run this in any production environment, you may leak PII or other sensitive information. Work is in progress to allow omitting the arguments from the logging to include only statements.
**Note** - When turning on database logging, the statements **include arguments** default.

So if planning to run this in any production environment, you may leak PII or other sensitive information.

To disable arguments from being listed in the query, enable the `DB_OMIT_ARGS=true` environment variable.

All database logs will be grouped under the `db` attribute key.

0 comments on commit 24746ad

Please sign in to comment.