FileSinkBuilder

Struct FileSinkBuilder 

Source
pub struct FileSinkBuilder<ArgPath> { /* private fields */ }
Expand description

§

§Note

The generics here are designed to check for required fields at compile time, users should not specify them manually and/or depend on them. If the generic concrete types or the number of generic types are changed in the future, it may not be considered as a breaking change.

Implementations§

Source§

impl<ArgPath> FileSinkBuilder<ArgPath>

Source

pub fn path<P>(self, path: P) -> FileSinkBuilder<PathBuf>
where P: Into<PathBuf>,

The path of the log file.

This parameter is required.

Examples found in repository?
examples/02-file.rs (line 21)
18fn configure_file_logger() -> Result<(), Box<dyn std::error::Error>> {
19    let path = env::current_exe()?.with_file_name("file.log");
20
21    let file_sink = FileSink::builder().path(path).build_arc()?;
22    let new_logger = Logger::builder().sink(file_sink).build_arc()?;
23    spdlog::set_default_logger(new_logger);
24
25    info!("this log will be written to the file `all.log`");
26
27    Ok(())
28}
More examples
Hide additional examples
examples/07-async.rs (line 10)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let path = env::current_exe()?.with_file_name("async.log");
10    let file_sink = FileSink::builder().path(path).build_arc()?;
11
12    // AsyncPoolSink is a combined sink which wraps other sinks
13    let async_pool_sink = AsyncPoolSink::builder().sink(file_sink).build_arc()?;
14
15    let async_logger = Logger::builder()
16        .sink(async_pool_sink)
17        .flush_level_filter(LevelFilter::All)
18        .build_arc()?;
19
20    info!(logger: async_logger, "Hello, async!");
21
22    Ok(())
23}
examples/03-logger.rs (line 16)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    // `spdlog-rs` has a global default logger and logs will be processed by it
10    // by default, You can configure it.
11    let default_logger = spdlog::default_logger();
12    default_logger.set_level_filter(LevelFilter::All);
13
14    // Or completely replace it with a new one.
15    let path = env::current_exe()?.with_file_name("all.log");
16    let file_sink = FileSink::builder().path(path).build_arc()?;
17
18    let new_logger = Logger::builder()
19        .level_filter(LevelFilter::All)
20        .flush_level_filter(LevelFilter::MoreSevereEqual(Level::Warn))
21        .sink(file_sink.clone())
22        .build_arc()?;
23    new_logger.set_flush_period(Some(Duration::from_secs(3)));
24    spdlog::set_default_logger(new_logger);
25
26    info!("this log will be written to the file `all.log`");
27
28    // In addition to having the global default logger, more loggers are allowed to
29    // be configured, stored and used independently.
30    let db = AppDatabase::new(file_sink)?;
31    db.write_i32(114514);
32
33    Ok(())
34}
35
36struct AppDatabase {
37    logger: Logger,
38}
39
40impl AppDatabase {
41    fn new(all_log_sink: Arc<dyn Sink>) -> Result<Self, Box<dyn std::error::Error>> {
42        let path = env::current_exe()?.with_file_name("db.log");
43        let db_file_sink = FileSink::builder().path(path).build_arc()?;
44
45        let logger = Logger::builder()
46            .name("database")
47            .level_filter(LevelFilter::All)
48            .flush_level_filter(LevelFilter::MoreSevereEqual(Level::Warn))
49            .sinks([all_log_sink, db_file_sink])
50            .build()?;
51        Ok(Self { logger })
52    }
Source

pub fn truncate(self, truncate: bool) -> Self

Truncates the contents when opening an existing file.

If it is true, the existing contents of the file will be discarded.

This parameter is optional, and defaults to false.

Source

pub fn capacity(self, capacity: usize) -> Self

Specifies the internal buffer capacity.

This parameter is optional, and defaults to the value consistent with std.

Source

pub fn level_filter(self, level_filter: LevelFilter) -> Self

Specifies a log level filter.

This parameter is optional, and defaults to LevelFilter::All.

Source

pub fn formatter<F>(self, formatter: F) -> Self
where F: Formatter + 'static,

Specifies a formatter.

This parameter is optional, and defaults to FullFormatter.

Source

pub fn error_handler<F: Into<ErrorHandler>>(self, handler: F) -> Self

Specifies an error handler.

This parameter is optional, and defaults to ErrorHandler::default().

Source§

impl FileSinkBuilder<PathBuf>

Source

pub fn build(self) -> Result<FileSink>

Builds a FileSink.

§Error

If an error occurs opening the file, Error::CreateDirectory or Error::OpenFile will be returned.

Source

pub fn build_arc(self) -> Result<Arc<FileSink>>

Builds a Arc<FileSink>.

This is a shorthand method for .build().map(Arc::new).

Examples found in repository?
examples/02-file.rs (line 21)
18fn configure_file_logger() -> Result<(), Box<dyn std::error::Error>> {
19    let path = env::current_exe()?.with_file_name("file.log");
20
21    let file_sink = FileSink::builder().path(path).build_arc()?;
22    let new_logger = Logger::builder().sink(file_sink).build_arc()?;
23    spdlog::set_default_logger(new_logger);
24
25    info!("this log will be written to the file `all.log`");
26
27    Ok(())
28}
More examples
Hide additional examples
examples/07-async.rs (line 10)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let path = env::current_exe()?.with_file_name("async.log");
10    let file_sink = FileSink::builder().path(path).build_arc()?;
11
12    // AsyncPoolSink is a combined sink which wraps other sinks
13    let async_pool_sink = AsyncPoolSink::builder().sink(file_sink).build_arc()?;
14
15    let async_logger = Logger::builder()
16        .sink(async_pool_sink)
17        .flush_level_filter(LevelFilter::All)
18        .build_arc()?;
19
20    info!(logger: async_logger, "Hello, async!");
21
22    Ok(())
23}
examples/03-logger.rs (line 16)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    // `spdlog-rs` has a global default logger and logs will be processed by it
10    // by default, You can configure it.
11    let default_logger = spdlog::default_logger();
12    default_logger.set_level_filter(LevelFilter::All);
13
14    // Or completely replace it with a new one.
15    let path = env::current_exe()?.with_file_name("all.log");
16    let file_sink = FileSink::builder().path(path).build_arc()?;
17
18    let new_logger = Logger::builder()
19        .level_filter(LevelFilter::All)
20        .flush_level_filter(LevelFilter::MoreSevereEqual(Level::Warn))
21        .sink(file_sink.clone())
22        .build_arc()?;
23    new_logger.set_flush_period(Some(Duration::from_secs(3)));
24    spdlog::set_default_logger(new_logger);
25
26    info!("this log will be written to the file `all.log`");
27
28    // In addition to having the global default logger, more loggers are allowed to
29    // be configured, stored and used independently.
30    let db = AppDatabase::new(file_sink)?;
31    db.write_i32(114514);
32
33    Ok(())
34}
35
36struct AppDatabase {
37    logger: Logger,
38}
39
40impl AppDatabase {
41    fn new(all_log_sink: Arc<dyn Sink>) -> Result<Self, Box<dyn std::error::Error>> {
42        let path = env::current_exe()?.with_file_name("db.log");
43        let db_file_sink = FileSink::builder().path(path).build_arc()?;
44
45        let logger = Logger::builder()
46            .name("database")
47            .level_filter(LevelFilter::All)
48            .flush_level_filter(LevelFilter::MoreSevereEqual(Level::Warn))
49            .sinks([all_log_sink, db_file_sink])
50            .build()?;
51        Ok(Self { logger })
52    }

Auto Trait Implementations§

§

impl<ArgPath> !Freeze for FileSinkBuilder<ArgPath>

§

impl<ArgPath> !RefUnwindSafe for FileSinkBuilder<ArgPath>

§

impl<ArgPath> Send for FileSinkBuilder<ArgPath>
where ArgPath: Send,

§

impl<ArgPath> Sync for FileSinkBuilder<ArgPath>
where ArgPath: Sync,

§

impl<ArgPath> Unpin for FileSinkBuilder<ArgPath>
where ArgPath: Unpin,

§

impl<ArgPath> !UnwindSafe for FileSinkBuilder<ArgPath>

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.