How to remove file path from `tracing::error!`?

Situation

I'm trying to create a wrapper that extends tracing::error! by triggering additional side-effects when there is something wrong anywhere in the code, and I'm using this wrapper as a replacement for tracing::error!.

If I call tracing_error! from the wrapper, it will give me the location where I defined the wrapper rather than the location where there is something wrong in the code. I partly solved it by using this setting in tracing_subscriber configuration:

.with(fmt::layer().without_time())`

The above reduces some of the verbosity in the error log by turning off the displaying of the line number where I defined the wrapper. But the filename of the wrapper is still being displayed. It now looks something like this:

ERROR <wrapper_file_path>: <problem_file_path>:<problem_line_number> <error_message>

I got the problem_file_path and problem_line_number from this crate (crates.io: Rust Package Registry)

Goals:

  • Completely remove wrapper_file_path from error log
  • Replace it with problem_filepath, or alternatively change color of problem_filepath in the error log to match the text color of problem_filepath

How do I do this?

tracing::error is a macro with very complex parsing rules, it is very hard to create a wrapper macro that is robust and can replace it. besides, the tracing::error macro is not the only way to record an "ERROR"-level event (e.g. using the tracing::event macro, or using the Dispatch::event() method directly), so you are not guaranteed to catch all the events by just replacing the error macro anyway.

if you want to hook into every "ERROR" event, the "proper" way is to utilize the global (thread-local, to be precise) Subscriber: you can either define your own full-blown Subscriber, or you can define a single Layer (configured with a suitable Filter, e.g. LevelFilter::ERROR).

you will receive the information of the recorded event in the Subscriber::event() method or the Layer::on_event() method. but the Layer apprach is easier because you have less required methods to implement.

5 Likes

I tried defining my own Layer for the subscriber. For this, I need an Event to insert as argument in on_event() method so that I can run the desired side-effects in response to an error event.

I found only two method to create an Event: new() and new_child_of(). Both of these methods require ValueSet as argument.

I found only one way to create ValueSet and that is via Attributes::values().

I found only three methods to create Attributes: Attributes::new(), Attributes::new_root() and Attributes::child_of(). Each of these methods require ValueSet as arguments.

So basically there seems to be a catch-22 between Attributes and ValueSet, and this catch-22 is a dependency of Event, which is a dependency of Layer::on_event().

How can I fix this situation?

Aren't you expected to receive the Events from tracing whenever you use logging macros?

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.