Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send standard logger's output to logrus #1912

Merged
merged 4 commits into from
Feb 5, 2024

Conversation

mikesep
Copy link
Contributor

@mikesep mikesep commented Jan 4, 2024

What is the problem I am trying to address?

Some packages like httputil's ReverseProxy use the standard log.Logger to emit log traces. This means that their output won't match the format of Athens's logrus output.

How is the fix applied?

logrus.Logger.Writer's documentation states that it "can be used to override the standard library logger easily." It spawns a go routine, so instead of doing that each time we need to make logrus look like a (standard) log.Logger (and then remembering to Close() it right away), this calls logrus.Writer() once in main, defers the Close(), and sets it into the global log.Logger.

What GitHub issue(s) does this PR fix or close?

Related to the work done in #1819 to unify logging.

@mikesep mikesep requested a review from a team as a code owner January 4, 2024 15:38
@mikesep
Copy link
Contributor Author

mikesep commented Jan 4, 2024

If maintainers would rather I figure out how to pass this down through the call chain to the sumdb ReverseProxy, I can try that approach, but it occurred to me that this global approach would catch anything else using the standard log too, and I wanted to avoid repeatedly spawning and closing the pipe-reading goroutine.

Copy link
Contributor

@nrwiersma nrwiersma left a comment

Choose a reason for hiding this comment

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

IMO, global loggers are not great, but given the cost (A go routine to mimic an io.Writer seems a little insane), this might be OK in this case.

cmd/proxy/main.go Outdated Show resolved Hide resolved
Copy link
Contributor

@nrwiersma nrwiersma left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines 50 to 59
{
logrusWriter := logger.WriterLevel(logrus.ErrorLevel)
defer func() {
if err := logrusWriter.Close(); err != nil {
logger.WithError(err).Warn("Could not close logrus writer pipe")
}
}()
stdlog.SetOutput(logrusWriter)
stdlog.SetFlags(stdlog.Flags() &^ (stdlog.Ldate | stdlog.Ltime))
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Scoping the variables here seems unnecessary, unless I am missing something that isn't described by the comment.

Suggested change
{
logrusWriter := logger.WriterLevel(logrus.ErrorLevel)
defer func() {
if err := logrusWriter.Close(); err != nil {
logger.WithError(err).Warn("Could not close logrus writer pipe")
}
}()
stdlog.SetOutput(logrusWriter)
stdlog.SetFlags(stdlog.Flags() &^ (stdlog.Ldate | stdlog.Ltime))
}
logrusWriter := logger.WriterLevel(logrus.ErrorLevel)
defer func() {
if err := logrusWriter.Close(); err != nil {
logger.WithError(err).Warn("Could not close logrus writer pipe")
}
}()
stdlog.SetOutput(logrusWriter)
stdlog.SetFlags(stdlog.Flags() &^ (stdlog.Ldate | stdlog.Ltime))

WDYT of simply removing the scope?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did it just to logically group things together, and also to discourage/prevent using logrusWriter later on. I'm fine removing it if this feels too awkward/unusual to others.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed scope in 6eea0a6.

Comment on lines 47 to 49
// logrus.Writer() spawns a go routine, so instead of doing that each time
// we need to make logrus look like a (standard) log.Logger (and then Close()ing it),
// we can do it once in main and have the global log.Logger use it.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// logrus.Writer() spawns a go routine, so instead of doing that each time
// we need to make logrus look like a (standard) log.Logger (and then Close()ing it),
// we can do it once in main and have the global log.Logger use it.
// Use the logrus writer as the standard logger's output in order to avoid starting goroutines whenever logrus.Writer() is called.

WDYT of this simplification of the comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's fair to say that this was too long a comment for code and better off in the PR/commit explanation. I went with something even shorter in 6eea0a6.

Copy link
Collaborator

@Ullaakut Ullaakut left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@nrwiersma nrwiersma merged commit cffffc0 into gomods:main Feb 5, 2024
11 checks passed
@mikesep mikesep deleted the send-standard-log-to-logrus branch February 5, 2024 20:39
@DrPsychick DrPsychick added this to the 0.13.2 milestone Feb 10, 2024
DrPsychick referenced this pull request in gomods/athens-charts Apr 16, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [gomods/athens](https://togithub.com/gomods/athens) | patch |
`v0.13.1` -> `v0.13.3` |

---

### Release Notes

<details>
<summary>gomods/athens (gomods/athens)</summary>

### [`v0.13.3`](https://togithub.com/gomods/athens/releases/tag/v0.13.3)

[Compare
Source](https://togithub.com/gomods/athens/compare/v0.13.2...v0.13.3)

#### What's Changed

- Update README.md by
[@&#8203;computerscienceiscool](https://togithub.com/computerscienceiscool)
in
[https://github.com/gomods/athens/pull/1932](https://togithub.com/gomods/athens/pull/1932)
- update-go-pkg(deps): bump github.com/stretchr/testify from 1.8.4 to
1.9.0 by [@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/gomods/athens/pull/1933](https://togithub.com/gomods/athens/pull/1933)
- Upgrade logrus from 1.7.0 => 1.9.3 by
[@&#8203;matt0x6F](https://togithub.com/matt0x6F) in
[https://github.com/gomods/athens/pull/1934](https://togithub.com/gomods/athens/pull/1934)
- should use errors.AsErr to extract and detect errors.Error by
[@&#8203;kkHAIKE](https://togithub.com/kkHAIKE) in
[https://github.com/gomods/athens/pull/1936](https://togithub.com/gomods/athens/pull/1936)
- correcting the misuse of the context in the copyContextWithCustomTime…
by [@&#8203;kkHAIKE](https://togithub.com/kkHAIKE) in
[https://github.com/gomods/athens/pull/1941](https://togithub.com/gomods/athens/pull/1941)
- remove hardcoded rootPath values by
[@&#8203;Jeansen](https://togithub.com/Jeansen) in
[https://github.com/gomods/athens/pull/1874](https://togithub.com/gomods/athens/pull/1874)

#### New Contributors

-
[@&#8203;computerscienceiscool](https://togithub.com/computerscienceiscool)
made their first contribution in
[https://github.com/gomods/athens/pull/1932](https://togithub.com/gomods/athens/pull/1932)
- [@&#8203;kkHAIKE](https://togithub.com/kkHAIKE) made their first
contribution in
[https://github.com/gomods/athens/pull/1936](https://togithub.com/gomods/athens/pull/1936)
- [@&#8203;Jeansen](https://togithub.com/Jeansen) made their first
contribution in
[https://github.com/gomods/athens/pull/1874](https://togithub.com/gomods/athens/pull/1874)

**Full Changelog**:
gomods/athens@v0.13.2...v0.13.3

### [`v0.13.2`](https://togithub.com/gomods/athens/releases/tag/v0.13.2)

[Compare
Source](https://togithub.com/gomods/athens/compare/v0.13.1...v0.13.2)

#### What's Changed

- Send standard logger's output to logrus by
[@&#8203;mikesep](https://togithub.com/mikesep) in
[https://github.com/gomods/athens/pull/1912](https://togithub.com/gomods/athens/pull/1912)
- chore: fix broken links to 'absolutely everybody' blog post by
[@&#8203;darrylblake](https://togithub.com/darrylblake) in
[https://github.com/gomods/athens/pull/1914](https://togithub.com/gomods/athens/pull/1914)
- update-github-action(deps): bump golangci/golangci-lint-action from 3
to 4 by [@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/gomods/athens/pull/1915](https://togithub.com/gomods/athens/pull/1915)
- update-go-pkg(deps): bump github.com/gorilla/mux from 1.6.2 to 1.8.1
by [@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/gomods/athens/pull/1917](https://togithub.com/gomods/athens/pull/1917)
- update-go-pkg(deps): bump github.com/stretchr/testify from 1.8.1 to
1.8.4 by [@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/gomods/athens/pull/1918](https://togithub.com/gomods/athens/pull/1918)
- update-go-pkg(deps): bump go.etcd.io/etcd/api/v3 from 3.5.9 to 3.5.12
by [@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/gomods/athens/pull/1919](https://togithub.com/gomods/athens/pull/1919)
- Fix Markdown link in Storage docs by
[@&#8203;chriskuehl](https://togithub.com/chriskuehl) in
[https://github.com/gomods/athens/pull/1922](https://togithub.com/gomods/athens/pull/1922)
- Use quotes for args by
[@&#8203;matt0x6F](https://togithub.com/matt0x6F) in
[https://github.com/gomods/athens/pull/1925](https://togithub.com/gomods/athens/pull/1925)
- Add log formatting settings by
[@&#8203;matt0x6F](https://togithub.com/matt0x6F) in
[https://github.com/gomods/athens/pull/1926](https://togithub.com/gomods/athens/pull/1926)
- upgrade mongodb driver by
[@&#8203;xytan0056](https://togithub.com/xytan0056) in
[https://github.com/gomods/athens/pull/1928](https://togithub.com/gomods/athens/pull/1928)
- update-go-pkg(deps): bump github.com/lib/pq from 1.10.7 to 1.10.9 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/gomods/athens/pull/1923](https://togithub.com/gomods/athens/pull/1923)
- Rework logging defaults by
[@&#8203;matt0x6F](https://togithub.com/matt0x6F) in
[https://github.com/gomods/athens/pull/1927](https://togithub.com/gomods/athens/pull/1927)

#### New Contributors

- [@&#8203;darrylblake](https://togithub.com/darrylblake) made their
first contribution in
[https://github.com/gomods/athens/pull/1914](https://togithub.com/gomods/athens/pull/1914)
- [@&#8203;chriskuehl](https://togithub.com/chriskuehl) made their first
contribution in
[https://github.com/gomods/athens/pull/1922](https://togithub.com/gomods/athens/pull/1922)
- [@&#8203;matt0x6F](https://togithub.com/matt0x6F) made their first
contribution in
[https://github.com/gomods/athens/pull/1925](https://togithub.com/gomods/athens/pull/1925)

**Full Changelog**:
gomods/athens@v0.13.1...v0.13.2

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/gomods/athens-charts).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4zMDEuNCIsInVwZGF0ZWRJblZlciI6IjM3LjMwMS40IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.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.

4 participants