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

Configurable log level for #[instrument(err)] #2330

Closed
nitnelave opened this issue Sep 30, 2022 · 2 comments · Fixed by #2335
Closed

Configurable log level for #[instrument(err)] #2330

nitnelave opened this issue Sep 30, 2022 · 2 comments · Fixed by #2335
Labels
crate/attributes Related to the `tracing-attributes` crate good first issue Good for newcomers kind/feature New feature or request

Comments

@nitnelave
Copy link
Contributor

Feature Request

I'd like to be able to specify the log level that Err returns are printed at.

Crates

tracing::instrument

Motivation

Currently it's hardcoded as error, but I have some functions that I'd like to instrument where returning an error is not especially unusual (think of for instance "is the user logged in yet?": any error eventually gets folded into "no, the user is not logged in"). In such cases, I'd like to be able to lower the level to WARN or INFO or even DEBUG.

Proposal

How about an additional attribute err_level?

#[instrument(level="debug", ret, err, err_level="info")]
fn my_fun() -> Result<int> { Ok(42) }

Alternatives

I could avoid using the instrument macro and annotate every single caller with an event, but that's not really the spirit of the crate :)

@hawkw
Copy link
Member

hawkw commented Sep 30, 2022

This is similar to the discussion #2328, which is about configuring the log level for the event generated by the ret argument to the attribute. As I mentioned in a comment on that discussion (#2328 (comment)), I think it definitely makes sense to make the level of ret events configurable, so I see no reason we shouldn't do it for err as well. I proposed a slightly different syntax, which would look like this:

#[instrument(level="debug", ret, err(level = "info"))]
fn my_fun() -> Result<int> { Ok(42) }

which i think might be a bit nicer, as it's slightly more concise, and introduces the possiblity to add additional configuration options for the ret and err events as "arguments" to those parameters. And, we wouldn't have to handle the case of "what happens if an #[instrument] attribute has err_level = "info" but no err?".

I think adding this should probably be fairly straightforward, at least for a contributor who's familiar with proc-macro implementation. If you're interested in opening a PR to add something like this, I'm happy to answer any questions you might have!

@hawkw hawkw added kind/feature New feature or request good first issue Good for newcomers crate/attributes Related to the `tracing-attributes` crate labels Sep 30, 2022
@nitnelave
Copy link
Contributor Author

Alright, that sounds good! In the spirit of the discussions you linked to, I'd probably add the same feature for ret.

I'll try to have a swing at it, though it's my first foray into procedural macros. But hey, there's a start to everything!

nitnelave added a commit to nitnelave/tracing that referenced this issue Oct 4, 2022
nitnelave added a commit to nitnelave/tracing that referenced this issue Oct 9, 2022
nitnelave added a commit to nitnelave/tracing that referenced this issue Oct 10, 2022
hawkw pushed a commit that referenced this issue Oct 10, 2022
This branch adds the ability to override the level of the events
generated by the `ret` and `err` arguments to `#[instrument]`. An
overridden level can be specified with:

```rust
#[instrument(ret(level = "info"))]
```
```rust
#[instrument(err(level = Level::Debug))]
```
and so on.

This syntax is fully backwards compatible with existing uses of the
attribute.

In addition, some refactoring was done to how levels are parsed and how
the tokens for a specified level is generated.

Fixes #2330
hawkw pushed a commit that referenced this issue Apr 21, 2023
This branch adds the ability to override the level of the events
generated by the `ret` and `err` arguments to `#[instrument]`. An
overridden level can be specified with:

```rust

```
```rust

```
and so on.

This syntax is fully backwards compatible with existing uses of the
attribute.

In addition, some refactoring was done to how levels are parsed and how
the tokens for a specified level is generated.

Fixes #2330
; Conflicts:
;	tracing-attributes/src/lib.rs
hawkw pushed a commit that referenced this issue Apr 21, 2023
This branch adds the ability to override the level of the events
generated by the `ret` and `err` arguments to `#[instrument]`. An
overridden level can be specified with:

```rust

```
```rust

```
and so on.

This syntax is fully backwards compatible with existing uses of the
attribute.

In addition, some refactoring was done to how levels are parsed and how
the tokens for a specified level is generated.

Fixes #2330
; Conflicts:
;	tracing-attributes/src/lib.rs
kaffarell pushed a commit to kaffarell/tracing that referenced this issue May 22, 2024
This branch adds the ability to override the level of the events
generated by the `ret` and `err` arguments to `#[instrument]`. An
overridden level can be specified with:

```rust

```
```rust

```
and so on.

This syntax is fully backwards compatible with existing uses of the
attribute.

In addition, some refactoring was done to how levels are parsed and how
the tokens for a specified level is generated.

Fixes tokio-rs#2330
; Conflicts:
;	tracing-attributes/src/lib.rs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crate/attributes Related to the `tracing-attributes` crate good first issue Good for newcomers kind/feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants