Git Product home page Git Product logo

Comments (8)

emabee avatar emabee commented on August 29, 2024

Without trying it out, I'd say: with Logger::log_to_writer you can provide a writer that realises your logic, using itself instances of FileLogWriter.

Let me know if something is missing, or if you think it would make sense to add (parts of) your code to flexi_logger.

from flexi_logger.

jb-alvarado avatar jb-alvarado commented on August 29, 2024

Without trying it out, I'd say: with Logger::log_to_writer you can provide a writer that realises your logic, using itself instances of FileLogWriter.

You mean I should implement my own writer? I was hoping that I don't need to do this 🙂. Because of rotation etc.

Let me know if something is missing, or if you think it would make sense to add (parts of) your code to flexi_logger.

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.

from flexi_logger.

emabee avatar emabee commented on August 29, 2024

The rotation etc is implemented within the FileLogWriter, no worries 😀. You just would add the logic to "add/and remove file logger".

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?

from flexi_logger.

jb-alvarado avatar jb-alvarado commented on August 29, 2024

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 less, but opening a logfile in VS Code would be ugly. In my program I open the log files in a web frontend and there I replace the controls with html elements and color the strings.
I could imagine several options:

  • color only terminal output
  • format tags with paris also in logfiles
  • remove every color tag
  • don't format the color tags with paris and leave everything untouched so the tags end up in the file.

from flexi_logger.

jb-alvarado avatar jb-alvarado commented on August 29, 2024

Without trying it out, I'd say: with Logger::log_to_writer you can provide a writer that realises your logic, using itself instances of FileLogWriter.

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.

from flexi_logger.

emabee avatar emabee commented on August 29, 2024

You would provide your own class that implements LogWriter and manages FileLogWriter instances and dispatches the incoming calls to them according to your needs.

from flexi_logger.

jb-alvarado avatar jb-alvarado commented on August 29, 2024

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 _rCURRENT and the rotation only with date (YYYY-mm-dd)? The simpler date format would make it more simple to read the log files in other programs.

from flexi_logger.

emabee avatar emabee commented on August 29, 2024

Yes, that's a pretty starter for what I had in mind. LogWriter has some provided methods, which you might want to look at and maybe implement explicitly.

Is there also a way log name the files without _rCURRENT?

Yes, with Naming::TimestampsDirect instead of Naming::Timestamps.

and the rotation only with date (YYYY-mm-dd)?

That does not yet exist, but could be offered as an option. Would you want to ask chatGPT to provide a PR for that? 🤩

from flexi_logger.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.