Skip to content

Conversation

@Ojasvi-Poonia
Copy link

Addresses #332

Motivation

When building applications with many routes spread across multiple files and packages, it's helpful to see all registered routes at startup for debugging. This is especially useful during development and when routes aren't behaving as expected. Other popular routers like Gin provide similar functionality.

Changes

  • Add PrintRoutes() method to print routes to stdout
  • Add PrintRoutesWithWriter(w io.Writer) for flexible output
  • Format output as [METHOD ] /path with alignment
  • Uses chi.Walk() to traverse the route tree
  • Handles route groups, subrouters, and mounted routers correctly
  • Comprehensive test coverage (6 test cases)
  • Working example in _examples/debug/

Example Usage

r := chi.NewRouter()
r.Get("/", homeHandler)
r.Post("/users", createUserHandler)
r.Get("/users/{id}", getUserHandler)

// Print all routes during development
r.PrintRoutes()

Output:

[GET    ] /
[POST   ] /users
[GET    ] /users/{id}

Testing

go test ./... -v

All tests pass

Example

cd _examples/debug
go run main.go

Prints all registered routes before starting the server.

Backward Compatibility

This is a purely additive change with no breaking changes. All existing code continues to work without modification.

Checklist

  • Tests added and passing
  • Example provided
  • No breaking changes
  • Follows project code style
  • Uses existing chi.Walk() function

Related Issues

Closes #332


Note: This is part of an academic project where I need to contribute features to open-source Go projects. I chose chi because it's actively maintained, and this feature genuinely helps developers during debugging. I'm excited to contribute to the chi community!

- Add PrintRoutes() and PrintRoutesWithWriter() methods to Mux
- Routes printed in aligned format: [METHOD  ] /path
- Uses chi.Walk() to traverse the route tree
- Handles route groups, subrouters, and mounted routers
- Comprehensive test coverage with 6 test cases
- Working example in _examples/debug/

Addresses go-chi#332
@titpetric
Copy link

Consider I may want to inject my logging function. I can't use a Writer because then I have to sink it into something else. I'd like to pass logger.Info which I suppose is a func(string)

- Add PrintRoutesFunc(func(string)) method for logger integration
- Supports any logging library (logrus, zap, slog, standard log)
- Allows filtering, formatting, or custom handling of route info
- Add tests for custom logging function
- Add example showing integration with various loggers
- Update README with usage examples

Addresses feedback in go-chi#332
@Ojasvi-Poonia
Copy link
Author

I've added a third method PrintRoutesFunc() that accepts a custom logging function.

New Feature

// Works with any logging function
r.PrintRoutesFunc(logger.Info)

// Or custom handling
r.PrintRoutesFunc(func(s string) {
    // Filter, format, or process route info
    if strings.Contains(s, "GET") {
        log.Println("Found GET route:", s)
    }
})

Benefits

Works with any logging library (logrus, zap, slog, standard log)
Allows filtering or custom processing of routes
No need to sink io.Writer into another logger
More idiomatic for structured logging

Three Methods Available

  1. PrintRoutes() - Simple stdout output
  2. PrintRoutesWithWriter(io.Writer) - For files, buffers
  3. PrintRoutesFunc(func(string)) - NEW - For logger integration

Examples Added

  • Standard library log integration
  • Custom logger simulation (logrus/zap style)
  • Filtering example (only print GET routes)

All tests passing

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

Labels

None yet

2 participants