Skip to content

Commit

Permalink
added verbose logging
Browse files Browse the repository at this point in the history
  • Loading branch information
j-d-ha committed Dec 17, 2024
1 parent ab6bbc3 commit a2c00cd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
1 change: 0 additions & 1 deletion .idea/watcherTasks.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ GLOBAL OPTIONS:
--address value, -a value Address of locally running lambda. Port can be set with env var _LAMBDA_SERVER_PORT. (default: "localhost:8000")
--parse-json, -p Parse response values like 'body' as JSON. (default: false)
--executionLimit value, -e value Execution time limit for this lambda in seconds. (default: 5)
--verbose, -v Enable verbose logging for debugging. (default: false)
--help, -h show help (default: false)
```

Expand Down
45 changes: 34 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,7 @@ func main() {
}

func run(ctx context.Context, w io.Writer) error { //nolint:funlen,cyclop
logger := slog.New(
tint.NewHandler(
w, &tint.Options{
Level: slog.LevelDebug,
TimeFormat: "15:04:05.000",
},
),
)
logLevel := slog.LevelInfo

cmd := &cli.Command{
Usage: "A tool for invoking AWS Lambdas locally",
Expand All @@ -56,6 +49,18 @@ func run(ctx context.Context, w io.Writer) error { //nolint:funlen,cyclop
Value: 5, //nolint:mnd
Usage: "Execution time limit for this lambda in seconds.",
},
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Usage: "Enable verbose logging for debugging.",
Action: func(_ context.Context, _ *cli.Command, b bool) error {
if b {
logLevel = slog.LevelDebug
}

return nil
},
},
},
Commands: []*cli.Command{
{
Expand Down Expand Up @@ -99,6 +104,16 @@ func run(ctx context.Context, w io.Writer) error { //nolint:funlen,cyclop
template := cmd.String("template")
parseJSON := cmd.Bool("parse-json")

logger := slog.New(
tint.NewHandler(
w, &tint.Options{
Level: logLevel,
TimeFormat: "15:04:05.000",
},
),
)

// create lambda client
lambdaRPC := NewLambdaLambdaRPCClient(lambdaAddress, executionLimit)

// run local API gateway
Expand Down Expand Up @@ -148,9 +163,7 @@ func run(ctx context.Context, w io.Writer) error { //nolint:funlen,cyclop
return fmt.Errorf("[in run.event] failed to open event file: %w", err)
}
defer func() {
if err = file.Close(); err != nil {
logger.Error("Error closing file", "err", err)
}
_ = file.Close()
}()

bytes, err := io.ReadAll(file)
Expand All @@ -174,6 +187,16 @@ func run(ctx context.Context, w io.Writer) error { //nolint:funlen,cyclop
event := cmd.String("string")
parseJSON := cmd.Bool("parse-json")

logger := slog.New(
tint.NewHandler(
w, &tint.Options{
Level: logLevel,
TimeFormat: "15:04:05.000",
},
),
)

// create lambda client
lambdaRPC := NewLambdaLambdaRPCClient(lambdaAddress, executionLimit)

// invoke lambda with event
Expand Down
5 changes: 5 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ test: ## run tests
@echo "Running tests"
@go test -v ./... -count=1

.PHONEY: lambdalocal-help
lambdalocal-help: ## show lambdalocal help
go run . --help

### Examples

.PHONEY: run_event
run_event:
go run . \
--parse-json \
--verbose \
event \
--file "./example/api-gateway-basic/event.json"

Expand Down

0 comments on commit a2c00cd

Please sign in to comment.