-
Notifications
You must be signed in to change notification settings - Fork 56
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
Add new file logged on the fly #158
Comments
Without trying it out, I'd say: with Let me know if something is missing, or if you think it would make sense to add (parts of) your code to |
You mean I should implement my own writer? I was hoping that I don't need to do this 🙂. Because of rotation etc.
If you are willing to integrate the Paris crate, that would be awesome! For my project I will also implement a mail logging function (with a queue, so not every message alone will send but all two minutes a bunch), but I think that is to specific to integrate in a logging crate. |
The rotation etc is implemented within the After a first glance, I wouldn't know how a decent integration could look like. Using Paris as an alternative backend (rather than writing to stdout directly) would allow using the colouring style of Paris, but it would only work with stdout going to a terminal -- if the programs output is redirected to a file, then you would get color control bytes in there, right? |
Yes, by default the color controls would go to the file. It can be useful for someone who uses
|
Sorry, I have no idea how this could work. Can you give me some more hints? When I use log_to_writer with a FileLogWriter, then this would be configured static. |
You would provide your own class that implements |
Ok, with the help of chatGPT I got this result: use flexi_logger::writers::{FileLogWriter, LogWriter};
use flexi_logger::{Age, Cleanup, Criterion, DeferredNow, FileSpec, Naming, Record};
use log::{debug, error, info, trace, warn};
use std::collections::HashMap;
use std::io;
use std::sync::{Arc, Mutex};
struct MultiFileLogger {
writers: Arc<Mutex<HashMap<String, Arc<Mutex<FileLogWriter>>>>>,
}
impl MultiFileLogger {
pub fn new() -> Self {
MultiFileLogger {
writers: Arc::new(Mutex::new(HashMap::new())),
}
}
fn get_writer(&self, target: &str) -> io::Result<Arc<Mutex<FileLogWriter>>> {
let mut writers = self.writers.lock().unwrap();
if !writers.contains_key(target) {
let writer = FileLogWriter::builder(
FileSpec::default()
.suppress_timestamp()
.basename("ffplayout")
.discriminant(target),
)
.append()
.rotate(
Criterion::Age(Age::Day),
Naming::Timestamps,
Cleanup::KeepLogFiles(7),
)
.print_message()
.try_build()
.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?;
writers.insert(target.to_string(), Arc::new(Mutex::new(writer)));
}
Ok(writers.get(target).unwrap().clone())
}
}
impl LogWriter for MultiFileLogger {
fn write(&self, now: &mut DeferredNow, record: &Record) -> io::Result<()> {
let target = record.target();
let writer = self.get_writer(target);
let w = writer?.lock().unwrap().write(now, record);
w
}
fn flush(&self) -> io::Result<()> {
let writers = self.writers.lock().unwrap();
for writer in writers.values() {
writer.lock().unwrap().flush()?;
}
Ok(())
}
}
fn main() {
let logger = MultiFileLogger::new();
flexi_logger::Logger::try_with_str("trace")
.expect("LogSpecification String has errors")
.print_message()
.log_to_writer(Box::new(logger))
.start()
.unwrap();
trace!(target: "channel1", "This is a trace message for file1");
debug!(target: "channel1", "This is a debug message for file1");
info!(target: "channel2", "This is an info message for file2");
warn!(target: "channel1", "This is a warning for file1");
error!(target: "channel2", "This is an error message for file2");
} Was your idea in the same direction? I have not made any longer runs, but just writing to multiple files works. Hopefully also the rotation :-). Is there also a way log name the files without |
Yes, that's a pretty starter for what I had in mind.
Yes, with
That does not yet exist, but could be offered as an option. Would you want to ask chatGPT to provide a PR for that? 🤩 |
Hello,
I'm testing your crate at the moment and I would like to know if it is possible to add/remove file loggers on the fly.
I need a option where I can add/and remove file logger dynamically and use then like:
Do you have any suggestions?
The text was updated successfully, but these errors were encountered: