-
Notifications
You must be signed in to change notification settings - Fork 106
Add verbose logging #804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add verbose logging #804
Conversation
d882fca
to
919a694
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works nicely as a CLI tool, but couples us tightly to the pterm.Logger
type which will be less suitable when using pgroll
as a dependency from other Go code. I think it's fine for now but we may want to revisit this.
A general suggestion would be to introduce an interface something like:
type OperationLogger interface {
// Operation lifecycle methods
LogOperationStart(op Operation)
LogOperationComplete(op Operation)
LogOperationRollback(op Operation)
// Specific logging events for common scenarios
LogBackfillStart(table string)
LogBackfillComplete(table string)
LogConstraintValidation(table, constraint string)
LogSchemaCreation(schemaName string)
// Generic methods for custom events
LogInfo(msg string, args ...any)
LogError(msg string, err error, args ...any)
}
and implement it for pterm
such that the different operations can do:
logger.LogOperationStart(o)
rather than having to repeat themselves with "starting operation..." etc everywhere.
The loggerArgs
methods could then move to the implementation of the OperationLogger
interface:
func (l *ptermOperationLogger) extractOperationArgs(op Operation) []any {
// Extract common fields
var args []any
// Type-switch to extract operation-specific fields
switch o := op.(type) {
case *OpAddColumn:
args = append(args,
"operation", OpNameAddColumn,
"name", o.Column.Name,
"type", o.Column.Type,
"table", o.Table,
"nullable", o.Column.Nullable,
"unique", o.Column.Unique,
)
case *OpCreateTable:
args = append(args,
"operation", OpNameCreateTable,
"name", o.Name,
)
if len(o.Columns) > 0 {
columnNames := getColumnNames(o.Columns)
args = append(args, "columns", columnNames)
}
// Handle other operation types
}
return args
}
this would keep the operations themselves relatively free of logging concerns. WDYT?
pkg/migrations/op_add_column.go
Outdated
@@ -189,7 +194,9 @@ func (o *OpAddColumn) Complete(ctx context.Context, conn db.DB, s *schema.Schema | |||
return nil | |||
} | |||
|
|||
func (o *OpAddColumn) Rollback(ctx context.Context, conn db.DB, s *schema.Schema) error { | |||
func (o *OpAddColumn) Rollback(ctx context.Context, logger pterm.Logger, conn db.DB, s *schema.Schema) error { | |||
logger.Info("completing operation", logger.Args(o.loggerArgs()...)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should say rolling back operation
As we agreed offline, I am implementing the requested changes in a follow-up PR. |
As requested in #804, I added a `Logger` interface for verbose mode. I have kept the interface minimal. I only added an extra `Info` function. But the idea is that we can extend this interface when we need to emit more logs.
This PR adds a new flag called
--verbose
. If you set it,pgroll
will be more verbose.It logs the following events with contextual info to stdout:
Example outputs
Create multiple tables table
Add foreign key constraint
Rollback
Closes #784