emabee/flexi_logger

change configuration on handle or recreate logger

Closed this issue ยท 12 comments

type: question / feature request

is there currently a way to change the configuration of an logger after being started? (options like directory, suffix, rotate (and disable logging to an file until directory is set))
or is there currently an way to replace an logger after being shutdown with an new one?

my use case is that i setup an logger on the beginning of my program, and want to change the configuration after having read the config (user definable log directory path and rotation options (like size / age))

currently i dont see any function / way to change the configuration on an LoggerHandle, and after having shutdown the first logger (prelogger.shutdown()) and trying to start a new logger, it errors with attempted to set a logger after the logging system was already initialized

flexi_logger is usually configured as backend for the log crate, which provides the log macros and offers a plugin-interface for the loggers. The log crate does not support reconfiguring, but of course a logger could do.

flexi_logger has the LoggerHandle, which so far allows reconfiguring the log specification in various ways.
It shouldn't be too difficult to offer further, more fundamental flexibility here.

@emabee thanks for implementing this, so if i understand correctly, now existingLogger::reset(&newBuilderVariable) needs to be called to reset configuration on existingLogger with the new configuration from newBuilderVariable?

Damn ;-) I didn't intend to close the issue directly.
You caught the idea correctly. The change is not yet published in crates.io, because I'm currently working on another extension that might have impact on this API extension.

Now version 0.18 is published!

so if i see it right, now with 0.18 the function to reset is reset_flw (existingLogger::reset_flw(&newBuilderVariable))

Correct

i tried to use it, but found that i could not actually create an new FileLogWriterBuilder, because all functions that are available outside the defining crate require an instance already, and new is private to the crate, and from what i can tell, default does not exist

am i using the wrong struct? do you maybe have an example for this?
edit: nvm, there is FileLogWriter::builder, didnt see this before, because reset_flw requires FileLogWriterBuilder and not FileLogWriter, so i didnt even look into this

btw, is it right that reset_flw wants FileLogWriterBuilder instead of FileLogWriter?

using version 0.18.0 from crates.io

PS: maybe adding an how-to-use example to FileLogWriterBuilder would be good

Luckily, the access to the FileLogWriterBuilder is possible and did not change: FileLogWriter::builder().

is it right that reset_flw wants FileLogWriterBuilder instead of FileLogWriter?
Yes, due to limitations of what can be exchanged without adding unwanted and performance-destroying locking this seemed to be more appropriate.

I should add some code_example...

btw, is there an built-in way to activate file logger, but not actually write to any file before reconfigured?

because the only way i see is to .log_to_file(FileSpec::default().directory(std::env::temp_dir())) (im also duplicating to stderr)

The easiest would be to start with logspec = "Off" and reset the logspec (with LoggerHandle::set_new_spec or LoggerHandle::parse_new_spec) after the call to LoggerHandle::reset_flw.

so, if i would start with off, but use all in duplicate (to either stderr or stdout), does it still duplicate or will it be off to the console output?

edit: just tested, it will be off as well, so i guess i will just default to tmpdir and start with off when not debugging before reset

PS: thanks for the help