Skip to content

feat: adopt structured logging (log/slog) for observability#50

Merged
mrhillsman merged 3 commits into
mainfrom
feat/structured-logging
May 22, 2026
Merged

feat: adopt structured logging (log/slog) for observability#50
mrhillsman merged 3 commits into
mainfrom
feat/structured-logging

Conversation

@mrhillsman

Copy link
Copy Markdown
Contributor

Summary

Adopts Go's standard log/slog package for structured logging throughout the codebase, replacing fmt.Printf and fmt.Fprintf calls with structured JSON output optimized for containerized/OpenShift environments.

Closes #8

Changes

New Components

  • internal/logging package: Provides NewLogger() function with configurable log levels
  • JSON output format for log aggregation systems
  • Debug level when --verbose flag is enabled
  • Info level by default

Updated Files

  • cmd/virtwork/main.go:
    • Initialize logger based on --verbose flag in runE and cleanupE
    • Replaced ~15 fmt.Fprintf calls with structured logging
    • Updated printSummary() and printDryRun() functions
  • internal/wait/wait.go:
    • Added logger parameter to WaitForVMReady() and WaitForAllVMsReady()
    • Replaced fmt.Printf retry messages with debug-level structured logs

Structured Fields

Logs now include contextual fields for filtering and analysis:

  • vm_name, namespace, workload, component
  • cpu_cores, timeout, vm_count
  • run_id, service_name, secret_name
  • error details for failures

Testing

  • All existing tests pass
  • New unit tests for internal/logging package
  • Updated test fixtures to use logger

Example Output

{"time":"2026-05-20T10:30:45Z","level":"INFO","msg":"vm created","vm_name":"virtwork-cpu-0","namespace":"virtwork","workload":"cpu"}
{"time":"2026-05-20T10:31:02Z","level":"INFO","msg":"all VMs ready","vm_count":3}

With --verbose:

{"time":"2026-05-20T10:30:50Z","level":"DEBUG","msg":"VMI not yet created, retrying","vm_name":"virtwork-cpu-0","namespace":"virtwork"}

Benefits

  • Structured logs integrate with log aggregation systems (ELK, Splunk, CloudWatch)
  • JSON format enables efficient filtering and querying
  • Contextual fields improve debugging in multi-VM deployments
  • --verbose flag provides granular control over log verbosity

@exe-prow-github-app exe-prow-github-app Bot requested review from jomkz and komish May 20, 2026 21:53
@exe-prow-github-app

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: mrhillsman

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@exe-prow-github-app exe-prow-github-app Bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label May 20, 2026
@mrhillsman mrhillsman removed request for jomkz and komish May 21, 2026 03:15
- Add internal/logging package with NewLogger function
- Logger supports debug/info levels via verbose flag
- Uses JSON output for container/OpenShift environments
- Update wait package to accept and use logger
- Replace fmt.Printf with structured logging in wait.go
- Add tests for logging configuration
- Update wait tests to use logger

Signed-off-by: Melvin Hillsman <mhillsma@redhat.com>
- Initialize logger in runE and cleanupE based on verbose flag
- Replace all fmt.Fprintf calls with slog calls
- Use structured fields for VM names, namespaces, counts, durations
- Update printSummary and printDryRun to use logger
- Maintain JSON output for container environments
- Pass logger to wait.WaitForAllVMsReady

Signed-off-by: Melvin Hillsman <mhillsma@redhat.com>
@mrhillsman mrhillsman force-pushed the feat/structured-logging branch from 6c03ab8 to 27aa01e Compare May 21, 2026 23:54
- Remove unused strings import from main.go
- Update main_test.go to pass logger to wait.WaitForAllVMsReady
- All tests now pass with structured logging

Signed-off-by: Melvin Hillsman <mhillsma@redhat.com>
@mrhillsman mrhillsman force-pushed the feat/structured-logging branch from 27aa01e to 0a7c6a8 Compare May 22, 2026 00:18
@mrhillsman mrhillsman merged commit 111dc44 into main May 22, 2026
3 of 4 checks passed
@mrhillsman mrhillsman deleted the feat/structured-logging branch May 22, 2026 06:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Adopt structured logging (log/slog) for all user-facing output

1 participant