[][src]Struct easylog::log_file::LogFile

pub struct LogFile { /* fields omitted */ }

Struct that represents the LogFile and its config

Methods

impl LogFile[src]

pub fn new(config: LogFileConfig) -> Result<LogFile, Box<dyn Error>>[src]

Creates a new Logfile.

Example 1

extern crate easylog;

use easylog::{LogFile, LogFileConfig, LogLevel};

fn main() {
    let default = LogFileConfig::new();
    let logfile = match LogFile::new(default) {
        Ok(file) => file,

        Err(error) => {
            panic!("Error: `{}`", error);
        }
    };
}

You can also modify the config-struct

Example 2

extern crate easylog;

use easylog::{LogFile, LogFileConfig, LogLevel};

fn main() {
    let mut custom_config = LogFileConfig::new();

    custom_config.max_size_in_mb = 2;
    custom_config.path = String::from("./path/to/logfile/");
    custom_config.name = String::from("my_logfile");
    custom_config.extension = String::from(".txt");
    custom_config.num_files_to_keep = 2;

    let logfile = match LogFile::new(custom_config) {
        Ok(file) => file,

        Err(error) => {
            panic!("Error: `{}`", error);
        }
    };
}

Example 3

extern crate easylog;

use easylog::{LogFile, LogFileConfig, LogLevel};

fn main() {
    let mut custom_config = LogFileConfig::new();

    custom_config.max_size_in_mb = 2;
    custom_config.path = String::from("./path/to/logfile/");
    custom_config.name = String::from("my_logfile");
    custom_config.extension = String::from(".txt");
    custom_config.overwrite = false;
    custom_config.num_files_to_keep = 1337; // has no effect, because overwrite is false

    let logfile = match LogFile::new(custom_config) {
        Ok(file) => file,

        Err(error) => {
            panic!("Error: `{}`", error);
        }
    };
}

Details

At first this function checks, if already a logfile (specified by the config argument) exist. If a logfile exist the size is checked. If the size is okay (actual size < max size) the file will be opened. When the size is not okay (actual size > max size) a new file will be created.

If max_size_in_mb is set to 0 (zero) it will be set to 1. So 1 Megabyte (1 * 1024 * 1024) is the smallest size for a Logfile.

If the overwrite param is set to true (default) and num_files_to_keep (default value is 5) is reached, the first logfile will be overwritten instead of creating a new one. So you will always have only num_files_to_keep of logfiles. If overwrite is set to false then you will get as much files as your machine allows.

If num_files_to_keep is set to 0 (zero) it will be set to 1. So 1 Logfile is the smallest amount of Logfiles.

pub fn write(&mut self, level: LogLevel, msg: &str)[src]

Write the given message to the logfile.

Example

extern crate easylog;

use easylog::{LogFile, LogFileConfig, LogLevel};

fn main() {
    let default = LogFileConfig::new();
    let mut logfile = match LogFile::new(default) {
        Ok(file) => file,

        Err(error) => {
            panic!("Error: `{}`", error);
        }
    };

    logfile.write(LogLevel::DEBUG, "Insert your logmessage here...");
}

Example Output

2018-06-08 20:23:44.278165 [DEBUG ] Insert your logmessage here...

Details

The function will append a newline at the end of the message and insert a timestamp and the given loglevel at the beginning of the message.

This function also check if the actual logfilesize is less then the allowed maximum size.

If the actual logfilesize is bigger as the allowed maximum, the actual file will be closed, the filecounter will be incresaesed by 1 and a new file will be opened.

After writing the message to the file flush will be called to ensure that all is written to file.

pub fn clone(&mut self) -> LogFile[src]

Returns a clone of the actual object state

Auto Trait Implementations

impl Send for LogFile

impl Sync for LogFile

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Any for T where
    T: 'static + ?Sized
[src]