Skip to content

[FEAT] Console Log#40

Merged
bbengfort merged 18 commits intomainfrom
console
Mar 29, 2026
Merged

[FEAT] Console Log#40
bbengfort merged 18 commits intomainfrom
console

Conversation

@bbengfort
Copy link
Copy Markdown
Contributor

@bbengfort bbengfort commented Mar 27, 2026

Scope of changes

Clearly I'm procrastinating …

So I implemented simplified and colorized logs for slog.

@chris-okuda says: I am bored, so I rewrote it all to pass the slogtest.TestHandler tests and also added a few features.

Screenshot of examples (go run ./rlog/console/cmd/):

image

Type of change

  • new feature
  • bug fix
  • documentation
  • testing
  • technical debt
  • other (describe)

Author checklist

  • I have manually tested the change and/or added automation in the form of unit tests or integration tests
  • I have added new test fixtures as needed to support added tests
  • Check this box if a reviewer can merge this pull request after approval (leave it unchecked if you want to do it yourself)
  • I have moved the associated Shortcut story to "Ready for Review"

Reviewer(s) checklist

  • Any new user-facing content that has been added for this PR has been QA'ed to ensure correct grammar, spelling, and understandability.
  • Are there any TODOs in this PR that should be turned into stories?

Note

Low Risk
Low risk: additive logging handler code only, with no changes to core log level semantics or data paths; main concerns are formatting/ANSI compatibility and limited test coverage.

Overview
Adds a new rlog/console package providing a development-focused slog.Handler that prints timestamp/level/message with ANSI colors and (optionally) a JSON-encoded attribute map.

Introduces console.Options to toggle coloring, JSON emission, and JSON indentation, and ensures concurrent-safe writes by buffering each record then writing under a shared mutex across handler clones (WithAttrs/WithGroup).

Written by Cursor Bugbot for commit f8d173b. Configure here.

cursor[bot]

This comment was marked as resolved.

@chris-okuda
Copy link
Copy Markdown
Contributor

@cursor bugbot run

@cursor

This comment was marked as resolved.

Copy link
Copy Markdown
Contributor

@chris-okuda chris-okuda left a comment

Choose a reason for hiding this comment

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

comments for @bbengfort on how I modified things

Comment on lines +1 to +2
// Command console demonstrates [console.Handler] and every [console.Options] field.
package main
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a demo; I wanted to see the output more easily for verification that it looked right

… way to merge rlog custom levels into a console.Options and optimised that merge
…mentation

- Introduced `benchmark_test.go` to compare performance between `rlog.Logger` and `slog.Logger` with various logging scenarios.
- Updated `README.md` to clarify module features, including new sections for `rlog.Logger`, subpackages, and usage examples.
- Enhanced `rlog.go` with improved source attribution in logs.
- Added tests to ensure correct source attribution in log records.
Copy link
Copy Markdown
Contributor

@chris-okuda chris-okuda left a comment

Choose a reason for hiding this comment

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

notes about the fixes for the pc value to make the AddSource work properly

Comment on lines +226 to +228
var pcs [1]uintptr
runtime.Callers(2, pcs[:])
l.emit(ctx, level, msg, pcs[0], args...)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These all just do a similar thing to how it's implemented in the slog library: https://cs.opensource.google/go/go/+/master:src/log/slog/logger.go;l=91

Only thing is, we can't access the underlying slog.Logger's options to see if AddSource was enabled or not, so we do this every time. We could add an option to disable this, but the benchmark shows it's not a performance impact.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

And this shouldn't be used for performance code anyway -- just development.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah - I was actually planning on doing this and had forgotten - thank you!

const timeFormat = "[15:04:05.000]"

// Handler writes a text prefix ([basename:line] when AddSource, bracketed time, level, message) and optional JSON attributes.
type Handler struct {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Appreciate you doing the full implementation of the Handler here. I was being lazy; I looked at the slog implementation, and both the TextHandler and the JSONHandler use the same commonLogger and I didn't know if I wanted to deal with that or not -- hence the embedding.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yea, nearly at the beginning I noticed that you were sending the embedded TextHandler the With and WithGroup calls, which basically loses those attributes to the ether since we can't access the internals of that handler, so I had to add those functions, and the topAttrs and segments internals, and once I did that the only thing left that the embedded handler was doing was the Enabled function, so I just dropped it.

Copy link
Copy Markdown
Contributor Author

@bbengfort bbengfort left a comment

Choose a reason for hiding this comment

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

Your implementation was far better than mine! Thank you for adding it!

@bbengfort bbengfort merged commit e13d66e into main Mar 29, 2026
4 checks passed
@bbengfort bbengfort deleted the console branch March 29, 2026 17:07
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.

2 participants