Comments (6)
At the very least we should simplify the procedure to create new loggers based on the default logger providing a different name. It's a very common practice for library authors.
from spdlog-rs.
Interesting suggestion, which involves some details to consider.
First, there is actually a simpler way to do this than your first example.
let mut new_logger = spdlog::default_logger().deref().clone();
new_logger.set_name(Some("new name"));
(Logger::set_name
here does not currently exist, I just forgot it XD, and implement it is easy)
Based on this, we need to reconsider the need for a shorthand method. I don't completely reject your proposal, but we need to figure out the following questions:
-
In the current implementation, in order to implement periodic flushing, the receiver of
set_flush_period
is&Arc<Self>
(code).LoggerBuilder
returnsLogger
instead ofArc<Logger>
, so it's not possible forLogger::new_from_logger
to exactly replicate all the arguments of a logger (periodic flushing will not work on the new logger, if the old logger set it).Neither can my example above, which would cause a panic at runtime if a flush period is set (code). So along with
set_name
, atry_clone
may also need to be implemented. -
So it should be possible to implement a
Logger::clone_with(self: &Arc<Self>, modifier: impl FnOnce(&mut Logger)) -> Arc<Logger>
, and it will be used in this way:
let new_logger = spdlog::default_logger().clone_with(|new| new.set_name("new logger"));
Compared to the above code, it is just one less line. However, since it serves only
Arc<Logger>
, it should be able to replicate all arguments (although users cannot modify the flush period in the closure). -
Further, I think that if we can find a way to implement the receiver of
set_flush_period
as&Logger
instead of the current&Arc<Logger>
, and it doesn't cost much (e.g. performance, interoperability, etc.), then my first example is short enough and should be able to replicate all the arguments, so we don't need aclone_with
shorthand, which is probably the best.
from spdlog-rs.
from spdlog-rs.
clone_with_name
should not be too different from clone_with
, its function signature also requires the same receiver &Arc<Logger>
and the same return type Arc<Logger>
. I prefer clone_with
because it provides more interoperability, so that in addition to downstream library code, downstream binary code will also benefit, as downstream binary code is particularly interested in properties such as sinks
.
from spdlog-rs.
I suggest we add both of clone_with
and clone_with_name
, and make clone_with_name
a convenient wrapper around clone_with
.
from spdlog-rs.
I suggest we add both of
clone_with
andclone_with_name
, and makeclone_with_name
a convenient wrapper aroundclone_with
.
Agree with that, I will open a PR later :)
from spdlog-rs.
Related Issues (15)
- Tracking issue for pattern formatters HOT 1
- [Bug Report] Source data is not displayed when using log crate HOT 5
- Tracking issue for config (deserialization) HOT 15
- pattern {tid} not shows the source threadid by async sink HOT 1
- Can't build with 0.3.9 - undefined reference to `gettid' HOT 5
- Failed to build for iOS HOT 7
- [Bug Report] Unnecessary recompiling spdlog-rs HOT 4
- how about upgrade syn package from 1 to 2 HOT 2
- [Feature Request] Event handlers for `FileSink` and `RotatingFileSink` HOT 5
- Tracking issue for `runtime_pattern!` macro HOT 1
- [Feature Request] Support formatting parameters in pattern placeholders
- [Feature Request] Allow calling other macros in logging macros HOT 3
- [Bug Report] fails to build on Linux with latest nightly Rust HOT 2
- [Bug Report] `AsyncPoolSink` may lose the last log records and panic on program exit 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 spdlog-rs.