Skip to content

refactor(gtfs): replace direct stderr printing with structured logging#301

Open
3rabiii wants to merge 4 commits intoOneBusAway:mainfrom
3rabiii:refactor-gtfs-logging
Open

refactor(gtfs): replace direct stderr printing with structured logging#301
3rabiii wants to merge 4 commits intoOneBusAway:mainfrom
3rabiii:refactor-gtfs-logging

Conversation

@3rabiii
Copy link
Contributor

@3rabiii 3rabiii commented Feb 2, 2026

Description

This PR refactors GetVehicleForTrip in gtfs_manager.go to use the project's structured logger (slog / logging.LogError) instead of writing directly to os.stderr using fmt.Fprintf.

Changes

  • Replaced fmt.Fprintf(os.Stderr, ...) with logging.LogError.
  • Added structured context fields (e.g., trip_id, block_id) to the logs for better observability.
  • Removed unused os import.

@aaronbrethorst
fixes : #300

Copy link
Member

@aaronbrethorst aaronbrethorst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Adel, thanks for tackling this logging cleanup — replacing raw stderr writes with structured logging is exactly the kind of improvement that pays off in observability. The structured context fields (trip_id, block_id) are a nice touch. Before we can merge this, I need you to fix one issue:

Critical

  1. Nil error panic in LogError (internal/gtfs/gtfs_manager.go:351-353): The condition on line 351 is err != nil || !requestedTrip.BlockID.Valid, which means err can be nil when the block ID is simply invalid. logging.LogError unconditionally calls err.Error() (structured_logging.go:30), so passing a nil error will cause a nil pointer dereference panic. The original fmt.Fprintf handled this gracefully because %v prints <nil> for nil values. You'll need to either guard against nil before calling LogError, or split the condition into two separate checks with different log messages (which would also be more informative — "trip not found" vs. "trip has no block ID" are different situations worth distinguishing).

Thanks again, and I look forward to merging this change.

@3rabiii
Copy link
Contributor Author

3rabiii commented Feb 6, 2026

Thanks for the critical catch! I've addressed the potential nil pointer panic in GetVehicleForTrip:

  1. Split the Condition: Refactored the logic to check err != nil separately from !requestedTrip.BlockID.Valid.
  2. Safe Logging: logging.LogError is now guarded and only called for actual DB errors.
  3. Improved Semantics: Missing Block IDs are now treated as Debug info rather than errors, as suggested.

Ready for review!
Screenshot From 2026-02-06 11-15-34

@aaronbrethorst
fixes : #300

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor: Replace fmt.Fprintf with structured logging in GTFS Manager

2 participants