Skip to content

Commit 7220de3

Browse files
committed
logger: add command logging
Add a 'LogCommand()' function to the 'TraceLogger', called in 'argparser.InvokeSubcommand()' right before the command is invoked. To remain consistent with Git, do this only when a toplevel command (indicated by 'isTopLevel') is invoked; nothing is done for subcommands of those commands. In the trace2 logger, this corresponds to the "cmd_name" event. Signed-off-by: Victoria Dye <[email protected]>
1 parent c07ddf9 commit 7220de3

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

internal/argparse/argparse.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ func (a *argParser) InvokeSubcommand(ctx context.Context) error {
215215
panic("subcommand has not been parsed")
216216
}
217217

218+
if a.isTopLevel {
219+
a.logger.LogCommand(ctx, a.selectedSubcommand.Name())
220+
}
221+
218222
return a.selectedSubcommand.Run(ctx, a.Args())
219223
}
220224

internal/log/logger.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
type loggedError error
1010

1111
type TraceLogger interface {
12+
LogCommand(ctx context.Context, commandName string) context.Context
1213
Error(ctx context.Context, err error) error
1314
Errorf(ctx context.Context, format string, a ...any) error
1415
Exit(ctx context.Context, exitCode int)

internal/log/trace2.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,14 @@ func (t *Trace2) logExit(ctx context.Context, exitCode int) {
166166
t.logger.Sync()
167167
}
168168

169+
func (t *Trace2) LogCommand(ctx context.Context, commandName string) context.Context {
170+
ctx, sharedFields := t.sharedFields(ctx)
171+
172+
t.logger.Info("cmd_name", sharedFields.with(zap.String("name", commandName))...)
173+
174+
return ctx
175+
}
176+
169177
func (t *Trace2) Error(ctx context.Context, err error) error {
170178
// We only want to log the error if it's not already logged deeper in the
171179
// call stack.

0 commit comments

Comments
 (0)