FileSink

Struct FileSink 

Source
pub struct FileSink { /* private fields */ }
Expand description

A sink with a file as the target.

It writes logs to a single file. If you want to automatically rotate into multiple files, see RotatingFileSink.

The file and directories will be created recursively if they do not exist.

§Examples

See ./examples directory.

Implementations§

Source§

impl FileSink

Source

pub fn builder() -> FileSinkBuilder<()>

Gets a builder of FileSink with default parameters:

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 new<P>(path: P, truncate: bool) -> Result<FileSink>
where P: AsRef<Path>,

👎Deprecated since 0.3.0: it may be removed in the future, use FileSink::builder() instead

Constructs a FileSink.

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

§Error

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

Trait Implementations§

Source§

impl Drop for FileSink

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl GetSinkProp for FileSink

Source§

fn prop(&self) -> &SinkProp

Gets the SinkProp from a sink.
Source§

impl Sink for FileSink

Source§

fn log(&self, record: &Record<'_>) -> Result<()>

Logs a record.
Source§

fn flush(&self) -> Result<()>

Flushes any buffered records.
Source§

fn should_log(&self, level: Level) -> bool

Determines if a log message with the specified level would be logged.
Source§

fn flush_on_exit(&self) -> Result<()>

Flushes any buffered records at program exit. Read more

Auto Trait Implementations§

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
Source§

impl<S> SinkPropAccess for S
where S: GetSinkProp,

Source§

fn level_filter(&self) -> LevelFilter

Gets the log level filter.
Source§

fn set_level_filter(&self, level_filter: LevelFilter)

Sets the log level filter.
Source§

fn set_formatter(&self, formatter: Box<dyn Formatter>)

Sets the formatter.
Source§

fn set_error_handler(&self, handler: ErrorHandler)

Sets a error handler. 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.