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

subscriber: add with_binary_name and with_process_id options. #2655

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nicolasavru
Copy link

Motivation

These options are useful when multiple processes are logging to the same destination (stderr, in the common case). This can happen when a process launches a child process or when a user simply launches multiple processes in the same terminal.

Fixes #2447.

Solution

Implement these two options.

@nicolasavru nicolasavru requested review from hawkw, davidbarsky and a team as code owners July 16, 2023 05:16
@nicolasavru
Copy link
Author

I was going back and forth on whether to put the binary name and pid before or after the log level on the line. I think I'm leaning towards before (so right after the timestamp) currently, but what do you think?

@nicolasavru nicolasavru force-pushed the format-binary-name-pid branch 3 times, most recently from ebdcbcc to 5340233 Compare July 17, 2023 16:23
@aviramha
Copy link

I'd love to see this merged. Can I help promote this somehow?

@WindSoilder
Copy link

I think it's a useful feature to have

@gz
Copy link

gz commented Jul 5, 2024

Is this planned to be merged anytime soon? I'd like to switch to tracing but we also merge logs of multiple applications and need to see the application name as part of the log.

Copy link
Contributor

@kaffarell kaffarell left a comment

Choose a reason for hiding this comment

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

Looks quite good! About the output order, currently it looks like this:

2024-07-09T15:16:26.616426Z cool PID(456304)  INFO ThreadId(01) fmt: yak shaving completed. all_yaks_shaved=false

IMO it looks better with all the "os-stuff" before the level ("INFO"). So maybe we should move ThreadId back as well? @hawkw what do you think?

/// [argv\[0\]]: std::env::args
pub fn with_binary_name(self,
display_binary_name: bool,
binary_name: Option<&str>,
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not formatted correctly. Please run cargo fmt.

Copy link
Author

Choose a reason for hiding this comment

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

cargo fmt was missing this subdir, I'm guessing due to rust-lang/rustfmt#3253. Had to run rustfmt manually.

/// otherwise the spcified string will be used.
///
/// [argv\[0\]]: std::env::args
pub fn with_binary_name(
Copy link
Contributor

Choose a reason for hiding this comment

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

To be honest I would lean more towards application_name instead of binary_name what do you think?

Copy link
Author

Choose a reason for hiding this comment

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

To me, an application is the whole software package, so to speak, and could consist of multiple binaries working together. How about executable_name?

Copy link
Contributor

Choose a reason for hiding this comment

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

That should work as well, yes!

pub fn with_binary_name(
self,
display_binary_name: bool,
binary_name: Option<&str>,
Copy link
Contributor

Choose a reason for hiding this comment

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

To make the api more inclusive (and maybe avoid an additional allocation), we could use Option<impl Into<String>> here.

Copy link
Author

Choose a reason for hiding this comment

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

Done.

) -> Format<F, T> {
Format {
display_binary_name,
binary_name: binary_name.map(str::to_string),
Copy link
Contributor

Choose a reason for hiding this comment

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

This here could then be .map(Into::into).

Copy link
Author

Choose a reason for hiding this comment

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

Done.

@gz
Copy link

gz commented Jul 9, 2024

No strong opinion on the order, but if you are showing multiple application logs in one terminal IMHO it's nicer if the order is Timestamp Loglevel <anything else> because the loglevel and timestamp will always be the same length and aligned on every line, so it's easier on the eye to scan for error, warn etc.

@kaffarell
Copy link
Contributor

That's actually a good point @gz! Let's do it like that then.

@nicolasavru nicolasavru force-pushed the format-binary-name-pid branch 2 times, most recently from a338a1a to 12f69b5 Compare July 10, 2024 16:00
@nicolasavru
Copy link
Author

Addressed comments, went back to Timestamp Loglevel <anything else>, rebased. I assume I should backport this to the v0.1 branch in a followup PR?

@nicolasavru nicolasavru force-pushed the format-binary-name-pid branch 2 times, most recently from 35e6ec2 to 92e90a7 Compare July 10, 2024 16:08
These options are useful when multiple processes are logging to the same
destination (stderr, in the common case). This can happen when a process
launches a child process or when a user simply launches multiple
processes in the same terminal.

Fixes tokio-rs#2447.

Implement these two options.
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.

Add option to log PID:s
5 participants