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

core: add as_str() for Level #1413

Merged
merged 2 commits into from
May 26, 2021
Merged

core: add as_str() for Level #1413

merged 2 commits into from
May 26, 2021

Conversation

Folyd
Copy link
Contributor

@Folyd Folyd commented May 26, 2021

Motivation

Get the string representation of the Level is quite a common usecase.
Without this method, I normally need to implement it by myself, which is fairly noisy.

#[inline]
fn level_to_str(level: &tracing::Level) -> &'static str {
    match *level {
        tracing::Level::TRACE => "TRACE",
        tracing::Level::DEBUG => "DEBUG",
        tracing::Level::INFO => "INFO",
        tracing::Level::WARN => "WARN",
        tracing::Level::ERROR => "ERROR"
    }
}

Solution

Add an as_str() method for Level. Similar to log::Level::as_str().

@Folyd Folyd requested review from carllerche and hawkw as code owners May 26, 2021 03:55
Copy link
Member

@hawkw hawkw left a comment

Choose a reason for hiding this comment

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

i'm not totally sure what need this meets that isn't already met by the fmt::Display implementation, but i'm fine with merging it, especially since the log crate does this.

i had a couple small suggestions, let me know what you think?

tracing-core/src/metadata.rs Outdated Show resolved Hide resolved
tracing-core/src/metadata.rs Outdated Show resolved Hide resolved
@hawkw hawkw merged commit f2abc8d into tokio-rs:master May 26, 2021
Folyd added a commit to Folyd/tracing that referenced this pull request May 27, 2021
## Motivation

Get the string representation of the `Level` is quite a common usecase.
Without this method, I normally need to implement it by myself, which
is fairly noisy.

```rust
#[inline]
fn level_to_str(level: &tracing::Level) -> &'static str {
    match *level {
        tracing::Level::TRACE => "TRACE",
        tracing::Level::DEBUG => "DEBUG",
        tracing::Level::INFO => "INFO",
        tracing::Level::WARN => "WARN",
        tracing::Level::ERROR => "ERROR"
    }
}
```

## Solution

Add an `as_str()` method for `Level`. Similar to [log::Level::as_str()][1].

[1] https://docs.rs/log/0.4.14/log/enum.Level.html#method.as_str
@Folyd Folyd deleted the level-as-str branch May 27, 2021 02:59
@Folyd
Copy link
Contributor Author

Folyd commented May 27, 2021

Thanks, @hawkw.

The main reason I need as_str() is that I don't wanna pay for an owned string via level.to_string().as_str() in order to get the plain string representation of a level.

I also backported this PR to v0.1.x, see #1416.

hawkw pushed a commit that referenced this pull request May 27, 2021
## Motivation

Get the string representation of the `Level` is quite a common usecase.
Without this method, I normally need to implement it by myself, which
is fairly noisy.

```rust
#[inline]
fn level_to_str(level: &tracing::Level) -> &'static str {
    match *level {
        tracing::Level::TRACE => "TRACE",
        tracing::Level::DEBUG => "DEBUG",
        tracing::Level::INFO => "INFO",
        tracing::Level::WARN => "WARN",
        tracing::Level::ERROR => "ERROR"
    }
}
```

## Solution

Add an `as_str()` method for `Level`. Similar to [log::Level::as_str()][1].

[1]: https://docs.rs/log/0.4.14/log/enum.Level.html#method.as_str
hawkw added a commit that referenced this pull request Jun 26, 2021
# 0.2.19 (June 25, 2021)

### Deprecated

- **registry**: `SpanRef::parents`, `SpanRef::from_root`, and
  `Context::scope` iterators, which are replaced by new `SpanRef::scope`
  and `Scope::from_root` iterators (#1413)

### Added

- **registry**: `SpanRef::scope` method, which returns a leaf-to-root
  `Iterator` including the leaf span (#1413)
- **registry**: `Scope::from_root` method, which reverses the `scope`
  iterator to iterate root-to-leaf (#1413)
- **registry**: `Context::event_span` method, which looks up the parent
  span of an event (#1434)
- **registry**: `Context::event_scope` method, returning a `Scope`
  iterator over the span scope of an event (#1434)
- **fmt**: `MakeWriter::make_writer_for` method, which allows returning
  a different writer based on a span or event's metadata (#1141)
- **fmt**: `MakeWriterExt` trait, with `with_max_level`,
  `with_min_level`, `with_filter`, `and`, and `or_else` combinators
  (#1274)
- **fmt**: `MakeWriter` implementation for `Arc<W> where &W: io::Write`
  (#1274)

Thanks to @teozkr and @Folyd for contributing to this release!
hawkw added a commit that referenced this pull request Jun 26, 2021
# 0.2.19 (June 25, 2021)

### Deprecated

- **registry**: `SpanRef::parents`, `SpanRef::from_root`, and
  `Context::scope` iterators, which are replaced by new `SpanRef::scope`
  and `Scope::from_root` iterators (#1413)

### Added

- **registry**: `SpanRef::scope` method, which returns a leaf-to-root
  `Iterator` including the leaf span (#1413)
- **registry**: `Scope::from_root` method, which reverses the `scope`
  iterator to iterate root-to-leaf (#1413)
- **registry**: `Context::event_span` method, which looks up the parent
  span of an event (#1434)
- **registry**: `Context::event_scope` method, returning a `Scope`
  iterator over the span scope of an event (#1434)
- **fmt**: `MakeWriter::make_writer_for` method, which allows returning
  a different writer based on a span or event's metadata (#1141)
- **fmt**: `MakeWriterExt` trait, with `with_max_level`,
  `with_min_level`, `with_filter`, `and`, and `or_else` combinators
  (#1274)
- **fmt**: `MakeWriter` implementation for `Arc<W> where &W: io::Write`
  (#1274)

Thanks to @teozkr and @Folyd for contributing to this release!
hawkw added a commit that referenced this pull request Aug 17, 2021
# 0.1.19 (August 17, 2021)
### Added

- `Level::as_str` ([#1413])
- `Hash` implementation for `Level` and `LevelFilter` ([#1456])
- `Value` implementation for `&mut T where T: Value` ([#1385])
- Multiple documentation fixes and improvements ([#1435], [#1446])

Thanks to @Folyd, @teozkr, and @dvdplm for contributing to this release!

[#1413]: #1413
[#1456]: #1456
[#1385]: #1385
[#1435]: #1435
[#1446]: #1446

Signed-off-by: Eliza Weisman <[email protected]>
hawkw added a commit that referenced this pull request Aug 17, 2021
# 0.1.19 (August 17, 2021)
### Added

- `Level::as_str` ([#1413])
- `Hash` implementation for `Level` and `LevelFilter` ([#1456])
- `Value` implementation for `&mut T where T: Value` ([#1385])
- Multiple documentation fixes and improvements ([#1435], [#1446])

Thanks to @Folyd, @teozkr, and @dvdplm for contributing to this release!

[#1413]: #1413
[#1456]: #1456
[#1385]: #1385
[#1435]: #1435
[#1446]: #1446

Signed-off-by: Eliza Weisman <[email protected]>
hawkw added a commit that referenced this pull request Aug 17, 2021
# 0.1.19 (August 17, 2021)
### Added

- `Level::as_str` ([#1413])
- `Hash` implementation for `Level` and `LevelFilter` ([#1456])
- `Value` implementation for `&mut T where T: Value` ([#1385])
- Multiple documentation fixes and improvements ([#1435], [#1446])

Thanks to @Folyd, @teozkr, and @dvdplm for contributing to this release!

[#1413]: #1413
[#1456]: #1456
[#1385]: #1385
[#1435]: #1435
[#1446]: #1446

Signed-off-by: Eliza Weisman <[email protected]>
kaffarell pushed a commit to kaffarell/tracing that referenced this pull request May 22, 2024
# 0.2.19 (June 25, 2021)

### Deprecated

- **registry**: `SpanRef::parents`, `SpanRef::from_root`, and
  `Context::scope` iterators, which are replaced by new `SpanRef::scope`
  and `Scope::from_root` iterators (tokio-rs#1413)

### Added

- **registry**: `SpanRef::scope` method, which returns a leaf-to-root
  `Iterator` including the leaf span (tokio-rs#1413)
- **registry**: `Scope::from_root` method, which reverses the `scope`
  iterator to iterate root-to-leaf (tokio-rs#1413)
- **registry**: `Context::event_span` method, which looks up the parent
  span of an event (tokio-rs#1434)
- **registry**: `Context::event_scope` method, returning a `Scope`
  iterator over the span scope of an event (tokio-rs#1434)
- **fmt**: `MakeWriter::make_writer_for` method, which allows returning
  a different writer based on a span or event's metadata (tokio-rs#1141)
- **fmt**: `MakeWriterExt` trait, with `with_max_level`,
  `with_min_level`, `with_filter`, `and`, and `or_else` combinators
  (tokio-rs#1274)
- **fmt**: `MakeWriter` implementation for `Arc<W> where &W: io::Write`
  (tokio-rs#1274)

Thanks to @teozkr and @Folyd for contributing to this release!
kaffarell pushed a commit to kaffarell/tracing that referenced this pull request May 22, 2024
# 0.1.19 (August 17, 2021)
### Added

- `Level::as_str` ([tokio-rs#1413])
- `Hash` implementation for `Level` and `LevelFilter` ([tokio-rs#1456])
- `Value` implementation for `&mut T where T: Value` ([tokio-rs#1385])
- Multiple documentation fixes and improvements ([tokio-rs#1435], [tokio-rs#1446])

Thanks to @Folyd, @teozkr, and @dvdplm for contributing to this release!

[tokio-rs#1413]: tokio-rs#1413
[tokio-rs#1456]: tokio-rs#1456
[tokio-rs#1385]: tokio-rs#1385
[tokio-rs#1435]: tokio-rs#1435
[tokio-rs#1446]: tokio-rs#1446

Signed-off-by: Eliza Weisman <[email protected]>
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