[pbs-devel] [PATCH proxmox v2 1/4] log: rename/move init functions

Gabriel Goller g.goller at proxmox.com
Tue Feb 11 10:31:05 CET 2025


On 11.02.2025 10:22, Wolfgang Bumiller wrote:
>On Mon, Feb 10, 2025 at 05:42:35PM +0100, Gabriel Goller wrote:
>> On 10.02.2025 15:37, Wolfgang Bumiller wrote:
>> > On Mon, Dec 09, 2024 at 11:46:03AM +0100, Gabriel Goller wrote:
>> > > +/// Inits a new tracing logger that prints to stderr or tasklog with the logging level specified in the
>> > > +/// environment variable `env_var`.
>> > > +///
>> > > +/// This logger is task-aware, which means if we are in a PBS task, we will retrieve the task-file
>> > > +/// and write to it. We'll only write to stderr if we are not in a task. If `env_var` doesn't exist
>> > > +/// or can't be read, use the `default_log_level`. The output will be very plain: no ansi, no
>> > > +/// timestamp, no level, just the message and it's
>> > > +/// fields.
>> > > +pub fn stderr_or_tasklog(
>> > > +    env_var: &str,
>> > > +    default_log_level: LevelFilter,
>> > > +) -> Result<(), anyhow::Error> {
>> > > +    let log_level = get_env_variable(env_var, default_log_level);
>> > > +
>> > > +    let registry = tracing_subscriber::registry()
>> > > +        .with(
>> > > +            plain_stderr_layer()
>> > > +                .with_filter(filter_fn(|_metadata| !LogContext::exists()))
>> >
>> > ^ This condition misses the `Level::ERROR` comparison while being
>> > suggested as a replacement for `init_cli_logger` which had it (not
>> > visible in the patch context lines, but it's there).
>> > If this is done on purpose, please explain it in the commit message.
>>
>> Oops, yeah my bad, this should be
>>
>>     !LogContext::exists() || *metadata.level() == Level::ERROR
>>
>> What do you think about the rest of the patch? I tried to implement this
>> with a builder pattern as well, but it turned out to be quite tricky
>> moving the layers around so I just wrote a ton of functions with long
>> names :(
>
>The rest seems fine.
>It does look like it should be mostly a builder-pattern thing (as it
>kind of already is, with the final 2 lines being a kind of `.apply()`,
>but with the names being showing their intended use, it's fine for an
>`init` module to have specific common setups like this (`init_cli_…`,
>`…with_pve_format`, etc.)

Yep, maybe I'll make it more modular in the future...

>Perhaps the journal/tasklog one could be named "init_daemon_log" (or
>just have an alias under that name)...

I'd be fine with an alias, but why 'init_daemon_log' though?

>> > If this was an oversight, the functions in `lib.rs` could just call
>> > their recommended `init::` counterparts instead of reimplementing them.
>>
>> Yep, will do that as well!
>>
>> > > +                .with_filter(log_level),
>> > > +        )
>> > > +        .with(TasklogLayer {}.with_filter(log_level));
>> > > +
>> > > +    tracing::subscriber::set_global_default(registry)?;
>> > > +    LogTracer::init_with_filter(log_level.as_log())?;
>> > > +    Ok(())
>> > > +}
>>
>> Thanks for the review!




More information about the pbs-devel mailing list