Skip to content

fix: convert to JSON structured logging using log/slog#102

Merged
rm-hull merged 5 commits into
mainfrom
gemini/patch-issue-101-7b0c9e5
May 17, 2026
Merged

fix: convert to JSON structured logging using log/slog#102
rm-hull merged 5 commits into
mainfrom
gemini/patch-issue-101-7b0c9e5

Conversation

@gemini-pr-agent
Copy link
Copy Markdown
Contributor

This is an automated PR generated by Gemini in response to issue #101.

Summary

The codebase has been refactored to use Go's log/slog for structured JSON logging, following the patterns in the provided reference.

Key Changes:

  • Centralized Configuration: Created pkg/logger/logger.go to initialize the global slog logger with a JSONHandler.
  • Systematic Migration: Replaced all instances of the standard log package with log/slog across all cmd/, internal/, and internal/importer/ files.
  • Consistent Logging: Replaced log.Printf with slog.Info, slog.Error, and slog.Warn using structured key-value pairs.
  • Fatal Error Handling: Replaced log.Fatalf with slog.Error followed by os.Exit(1) to maintain correct exit behavior while allowing for structured logging.
  • Test Updates: Updated tests to accommodate the global slog logger.

All tests passed successfully following the refactor.

Caution

Please review the generated code carefully before merging!

Closes #101

@gemini-pr-agent gemini-pr-agent Bot added go Pull requests that update go code ai-generated labels May 17, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 17, 2026

TestsPassed ✅SkippedFailed
JUnit Test Report27 ran27 passed0 skipped0 failed
TestResult
JUnit Test Report
com/map-services/company-data-api/internal.TestParseCSVWithHeader✅ passed
com/map-services/company-data-api/internal.TestParseCSVWithoutHeader✅ passed
com/map-services/company-data-api/internal.TestParseCSVMalformed✅ passed
com/map-services/company-data-api/internal.TestParseCSVEmpty/with_header✅ passed
com/map-services/company-data-api/internal.TestParseCSVEmpty/without_header✅ passed
com/map-services/company-data-api/internal.TestParseCSVEmpty✅ passed
com/map-services/company-data-api/internal.TestParseCSVFromFuncError✅ passed
com/map-services/company-data-api/internal/importer.TestFromCodePointCSV✅ passed
com/map-services/company-data-api/internal/importer.TestFromCodePointCSVInvalidEasting✅ passed
com/map-services/company-data-api/internal/importer.TestFromCodePointCSVInvalidNorthing✅ passed
com/map-services/company-data-api/internal/importer.TestCodePointToTuple✅ passed
com/map-services/company-data-api/internal/importer.TestImportCodePoint✅ passed
com/map-services/company-data-api/internal/importer.TestImportCodePointMultipleRecords✅ passed
com/map-services/company-data-api/internal/importer.TestImportCodePointPrepareError✅ passed
com/map-services/company-data-api/internal/importer.TestImportCodePointExecError✅ passed
com/map-services/company-data-api/internal/importer.TestProcessCodePointCSV✅ passed
com/map-services/company-data-api/internal/importer.TestFromCompanyDataCSV✅ passed
com/map-services/company-data-api/internal/importer.TestFromCompanyDataCSVShortRecord✅ passed
com/map-services/company-data-api/internal/importer.TestFromCompanyDataCSVInvalidDate✅ passed
com/map-services/company-data-api/internal/importer.TestCompanyDataToTuple✅ passed
com/map-services/company-data-api/internal/importer.TestImportCompanyData✅ passed
com/map-services/company-data-api/internal/importer.TestProcessCompanyDataCSV✅ passed
com/map-services/company-data-api/internal/importer.TestProcessCompanyDataCSVBatching✅ passed
com/map-services/company-data-api/internal/importer.TestInsertCompanyDataBatchRollbackOnError✅ passed
com/map-services/company-data-api/internal/middleware.TestRequestLogger/Logs_standard_paths✅ passed
com/map-services/company-data-api/internal/middleware.TestRequestLogger/Excludes_paths✅ passed
com/map-services/company-data-api/internal/middleware.TestRequestLogger✅ passed

@coveralls
Copy link
Copy Markdown

coveralls commented May 17, 2026

Coverage Report for CI Build 26001526437

Coverage increased (+1.2%) to 40.099%

Details

  • Coverage increased (+1.2%) from the base build.
  • Patch coverage: 58 uncovered changes across 11 files (30 of 88 lines covered, 34.09%).
  • 5 coverage regressions across 3 files.

Uncovered Changes

Top 10 Files by Coverage Impact Changed Covered %
cmd/api_server.go 14 0 0.0%
internal/logger.go 8 0 0.0%
cmd/code_point.go 7 0 0.0%
cmd/companies_house.go 7 0 0.0%
internal/downloader.go 6 0 0.0%
internal/importer/code_point.go 7 3 42.86%
internal/importer/company_data.go 7 3 42.86%
internal/repositories/search.go 3 0 0.0%
internal/middleware/logger.go 26 24 92.31%
internal/routes/search.go 2 0 0.0%

Coverage Regressions

5 previously-covered lines in 3 files lost coverage.

File Lines Losing Coverage Coverage
cmd/api_server.go 3 0.0%
cmd/code_point.go 1 0.0%
cmd/companies_house.go 1 0.0%

Coverage Stats

Coverage Status
Relevant Lines: 808
Covered Lines: 324
Line Coverage: 40.1%
Coverage Strength: 0.46 hits per line

💛 - Coveralls

@rm-hull
Copy link
Copy Markdown
Collaborator

rm-hull commented May 17, 2026

@gemini-pr-agent the PR doesnt seem to add a request middleware for GIN, can you add please?

gemini-pr-agent Bot and others added 2 commits May 17, 2026 19:00
The requested request middleware has been implemented for the Gin router.

### Summary of Changes

1.  **Created **: Added a new Gin middleware called  which logs the status, method, path, query, latency, and client IP of every request.
2.  **Updated **:
    *   Imported .
    *   Registered  in the  chain of the API server.

The code has been verified and compiles correctly.
@rm-hull
Copy link
Copy Markdown
Collaborator

rm-hull commented May 17, 2026

@gemini-pr-agent is it necessary to have both:

		middleware.RequestLogger(),
		gin.LoggerWithWriter(gin.DefaultWriter, "/healthz", "/metrics"),

Also:

The `RequestLogger` middleware has been refactored to support variadic exclude paths and now mirrors the implementation pattern from the requested repository. Unit tests have been added to verify that the middleware correctly logs standard requests and skips excluded paths.

### Changes Summary
- **Refactored `internal/middleware/request.go`**: Updated `RequestLogger` to accept a logger and variadic `excludedPaths`. It now correctly filters requests based on the provided paths and uses the `slog` logger with consistent formatting.
- **Updated `cmd/api_server.go`**: Updated the middleware configuration to pass `slog.Default()` and the excluded paths (`/healthz` and `/metrics`) to `RequestLogger`.
- **Added `internal/middleware/request_test.go`**: Created new unit tests to confirm the logging behavior and path exclusion logic, ensuring they function as expected in `gin` environments.

Tests have been verified to pass using `go test`.
@gemini-pr-agent
Copy link
Copy Markdown
Contributor Author

The RequestLogger middleware has been refactored to support variadic exclude paths and now mirrors the implementation pattern from the requested repository. Unit tests have been added to verify that the middleware correctly logs standard requests and skips excluded paths.

Changes Summary

  • Refactored internal/middleware/request.go: Updated RequestLogger to accept a logger and variadic excludedPaths. It now correctly filters requests based on the provided paths and uses the slog logger with consistent formatting.
  • Updated cmd/api_server.go: Updated the middleware configuration to pass slog.Default() and the excluded paths (/healthz and /metrics) to RequestLogger.
  • Added internal/middleware/request_test.go: Created new unit tests to confirm the logging behavior and path exclusion logic, ensuring they function as expected in gin environments.

Tests have been verified to pass using go test.

@rm-hull
Copy link
Copy Markdown
Collaborator

rm-hull commented May 17, 2026

@gemini-pr-agent can you explain logger.go please?

@gemini-pr-agent
Copy link
Copy Markdown
Contributor Author

The pkg/logger/logger.go file provides a simple configuration for the application's logging system using Go's standard log/slog package.

Key Functionality:

  • SetupLogger(): This function initializes and configures the default global logger.
  • JSON Output: It sets up a slog.JSONHandler, which causes all log output to be structured as JSON. This is ideal for production environments where logs are ingested by log management systems (like ELK, Datadog, or CloudWatch).
  • Output Destination: The logs are directed to os.Stderr.
  • Log Level: It defaults the minimum logging level to slog.LevelInfo, meaning informational, warning, and error logs will be captured, while debug logs will be ignored.
  • Global Scope: By calling slog.SetDefault(logger), it ensures that any package in the application using the standard slog package (e.g., slog.Info(...)) will use this configured JSON logger.

This approach ensures consistent, structured logging across the entire API server.

- Moved `pkg/logger` to `internal/logger.go` to enforce internal usage.
- Standardized application initialization by returning the logger
instance.
- Updated `godx` diagnostics to consume the configured logger.
- Removed redundant Gin logger middleware to streamline output.

```mermaid
graph LR
    subgraph Initialization
    A[Entry Point] --> B[internal.SetupLogger]
    B --> C[godx.Diagnostics]
    end
```
@rm-hull rm-hull merged commit 3324c6c into main May 17, 2026
3 checks passed
@rm-hull rm-hull deleted the gemini/patch-issue-101-7b0c9e5 branch May 17, 2026 20:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-generated go Pull requests that update go code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

convert to JSON structured logging using log/slog

2 participants