Skip to content

Conversation

@bobheadxi
Copy link
Member

@bobheadxi bobheadxi commented Feb 2, 2023

A use case for this came up before (https://github.com/sourcegraph/sourcegraph/pull/45229), and @thenamankumar inquired about similar capabilities today for tracking log entries into a database. I think this may be a valid use case to support and simple enough to provide:

var output bytes.Buffer
hookedLogger := hook.Writer(logger, &output, log.LevelWarn, encoders.OutputJSON)

// write log entries
hookedLogger.Info("foobar")

// get the rendered log entry
println(output.String())
// {"SeverityText":"INFO","Timestamp":1675380599980612000,"InstrumentationScope":"TestWriter","Caller":"hook/writer_test.go:24","Function":"github.com/sourcegraph/log/hook_test.TestWriter","Body":"foobar"}

This makes it easy for consumers to record log output in a format consistent with what gets rendered by the library.

For convenience, log output format is now exported via log/output.Format.

I also think it makes sense for this to exist separately from logtest.CaptureLog - logtest exports log entries in a structured manner for assertions, but we don't really want production code to be treating log entries as structured data (we could, but that might be a larger debate) - this serves simply to tee output elsewhere for recordkeeping.

@codecov-commenter
Copy link

codecov-commenter commented Feb 2, 2023

Codecov Report

❌ Patch coverage is 61.29032% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 60.16%. Comparing base (acdde86) to head (4373c5e).
⚠️ Report is 18 commits behind head on main.

Files with missing lines Patch % Lines
logtest/logtest.go 45.45% 6 Missing ⚠️
hook/writer.go 73.33% 2 Missing and 2 partials ⚠️
internal/globallogger/globallogger.go 0.00% 1 Missing ⚠️
sinks_output.go 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #55      +/-   ##
==========================================
+ Coverage   59.83%   60.16%   +0.32%     
==========================================
  Files          15       16       +1     
  Lines         600      615      +15     
==========================================
+ Hits          359      370      +11     
- Misses        220      222       +2     
- Partials       21       23       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

@sashaostrikov sashaostrikov left a comment

Choose a reason for hiding this comment

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

LGTM! :shipit:

hook/writer.go Outdated

// Writer hooks receiver to rendered log output at level in the requested format,
// typically one of 'json' or 'console'.
func Writer(logger log.Logger, receiver io.Writer, level log.Level, format string) log.Logger {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: we can create a type and consts for format and use it instead of string.

However, I can see that you had a type and changed it to string :) But I think having our own type (aliasing string) shouldn't cause any problems?

Copy link
Member Author

Choose a reason for hiding this comment

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

The type was internal - let me try and move it around :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Took a bit of shuffling but here we go: 4373c5e

bobheadxi and others added 2 commits February 3, 2023 09:56
Co-authored-by: Alex Ostrikov <alexander.ostrikov.jr@gmail.com>
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.

6 participants