Comments (8)
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.
Without trying it out, I'd say: with
Logger::log_to_writer
you can provide a writer that realises your logic, using itself instances ofFileLogWriter
.
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.
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.
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.
Without trying it out, I'd say: with
Logger::log_to_writer
you can provide a writer that realises your logic, using itself instances ofFileLogWriter
.
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.
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.
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.
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)
- How to reopen the output file of a FileLogWriter after Logger::add_writer took it over? HOT 2
- Dynamic logging for parent/daemon HOT 1
- bufsize in std_writer::AsyncHandle HOT 2
- Missing `regex` and `log` dependencies - fail to compile HOT 1
- Method on LoggerHandle for manually forcing rotation?
- Remove remnants of the external_rotation feature
- Rotation may overwrite existing log file HOT 9
- Different logger instances per thread? HOT 1
- GlobError unwrap happens HOT 3
- [trc] Improve usability by getting values from `Logger` or integrating with it HOT 1
- `kv` support from log crate. Json formatter. HOT 2
- Does this library support multiple log files and/or multiple named/filtered outputs? HOT 1
- creating vs modification time HOT 1
- TimestampsCustomFormat with empty current_infix HOT 1
- FileLogWriter does not follow its max_level HOT 1
- Custom logging level HOT 1
- Rotate files was not triggered HOT 1
- Custom formatter for syslog not working? HOT 4
- Logging for different logging levels HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flexi_logger.