FlexiConfig

Struct FlexiConfig 

Source
pub struct FlexiConfig {
Show 14 fields pub project: String, pub dependencies: BTreeMap<String, Option<Priority>>, pub target: Target, pub stderr_format: LogFormat, pub disk_format: LogFormat, pub disk_dir: Option<String>, pub disk_dup_err: bool, pub disk_dup_info: bool, pub disk_filename_ext: String, pub disk_filename_time: bool, pub disk_file_size: Option<u32>, pub disk_filename_suffix: Option<String>, pub app_priority: Priority, pub issues: Option<Issues>,
}
Expand description

Main structure for the crate. This object is used to create the configuration file, load the configuration file, and apply the settings to flexi_logger.

This object will initialize flexi_logger to start logging messages. It can be created programatically, from a Cargo.toml file, or from a configuration TOML file. Once the settings are correct the configuration can be applied to flexi_logger and logging can start.

Fields§

§project: String

Defines the name of the application.

§dependencies: BTreeMap<String, Option<Priority>>

Defines the list of modules the application depends on and the lowest level log message

§target: Target

Defines where the log messages should be written to, disk or stderr? By default this is set to stderr.

§stderr_format: LogFormat

Defines the log format when writing to stderr. By default this will use the flexi_logger default format.

§disk_format: LogFormat

Defines the log format when writing to disk. By default this will use the flexi_logger detailed format.

§disk_dir: Option<String>

If logging to disk is enabled, this sets the directory to write to. By default (None) it will write to the current directory.

§disk_dup_err: bool

When logging to disk, flexi_logger can duplicate any error messages to stdout. The default is to duplicate the error messages.

§disk_dup_info: bool

When logging to disk, flexi_logger can duplicate any info messages to stdout. The default is to not duplicate the messages.

§disk_filename_ext: String

The extension for the log file. By default this is “log”.

§disk_filename_time: bool

Flag to tell flexi_logger to include the timestamp when a log file is created. This is set to true by default. If the file_limit_size is set however this value will be ignored.

§disk_file_size: Option<u32>

If set, limits the size of the log file in megabytes. When this size is reached a new file is created. If this value is set the log files will have a sequence number rather than a time stamp.

§disk_filename_suffix: Option<String>

Adds a suffix to the log file names This will be included after the program name but

§app_priority: Priority

Sets the lowest priority to report for you application. The default priority is Warn.

§issues: Option<Issues>

If the object was loaded from a TOML configuration file, this object will contain any issues encountered while loading the file.

Implementations§

Source§

impl FlexiConfig

Source

pub fn from_cargo_file<P: AsRef<Path>>(file: P) -> Result<FlexiConfig, Issues>

Construct a new instance from a Cargo.toml file located on disk. This can be used during development to create the initial configuration file to ship with the application.

§Examples
use flexi_config::FlexiConfig;

let config = FlexiConfig::from_cargo_file("Cargo.toml").unwrap();

config.to_toml_file("log.toml").unwrap();

You can also change some of the default values to values that work better for your application.

use flexi_config::{FlexiConfig, Target};

let mut config = FlexiConfig::from_cargo_file("Cargo.toml").unwrap();

config.target   = Target::Disk;
config.disk_dir = Some(String::from("logs"));

config.to_toml_file("log.toml").unwrap();
Source

pub fn from_cargo(file: &str) -> Result<FlexiConfig, Issues>

Construct a new object from a Cargo.toml file that has already been loaded. Use this method if you want to load the Cargo.toml file yourself. Or you could provide a static string for the Cargo file instead.

§Examples
use flexi_config::FlexiConfig;

let mut config = FlexiConfig::from_cargo(r#"
    [package]
    name="foo"
    [dependencies]
    rand = "*"
    time = "*"
    "#).unwrap();

config.to_toml_file("log.toml").unwrap();
Source§

impl FlexiConfig

Source

pub fn from_toml_file<P: AsRef<Path>>(file: P) -> Result<FlexiConfig, Issues>

Creates an instance of the object by loading a configuration file that has been previously created.

§Examples
use flexi_config::FlexiConfig;

let mut config = FlexiConfig::from_toml_file("log.toml").unwrap();

config.apply().unwrap()
Source

pub fn from_toml_string(input: &str) -> Result<FlexiConfig, Issues>

Creates an instance from a file already loaded into memory. Use this method if you want to load the configuration file yourself and pass it to FlexiConfig. Or you could use it to contruct from a static string.

§Examples
use flexi_config::FlexiConfig;

let config = FlexiConfig::from_toml_string(r#"
    [common]
    application="foo"
    priority="warn"
    target="stderr"
    stderr_format="detailed"
    [depends]
    rand = "warn"
    time = "trace"
    "#).unwrap();

config.apply().unwrap();
Source§

impl FlexiConfig

Source

pub fn to_toml_string(self) -> String

Construct the configuration TOML file into a string. Use this method is you would like to save the configuration file yourself.

§Examples
use flexi_config::FlexiConfig;

let config = FlexiConfig::from_cargo_file("Cargo.toml").unwrap();

println!("{}", config.to_toml_string());
Source

pub fn to_toml_file<P: AsRef<Path>>(self, file: P) -> Result<(), String>

Saves the log configuration to a TOML file on disk.

§Examples
use flexi_config::FlexiConfig;

let config = FlexiConfig::from_cargo_file("Cargo.toml").unwrap();

config.to_toml_file("log.toml").unwrap();
Source§

impl FlexiConfig

Source

pub fn project(self, project: &str) -> FlexiConfig

Convenience method to set the project.

Source

pub fn dependencies(self, name: &str, priority: Option<Priority>) -> FlexiConfig

Convenience method to set the list of dependencies.

Source

pub fn all_dependencies(self, priority: Option<Priority>)

Set the priority for all dependencies to the same value.

Source

pub fn target(self, target: Target) -> FlexiConfig

Convenience method to set the target.

Source

pub fn stderr_format(self, format: LogFormat) -> FlexiConfig

Convenience method to set the stderr log format.

Source

pub fn disk_format(self, format: LogFormat) -> FlexiConfig

Convenience method to set the disk log format.

Source

pub fn disk_dir(self, dir: &str) -> FlexiConfig

Convenience method to set the log directory.

Source

pub fn disk_dup_err(self, duplicate: bool) -> FlexiConfig

Convenience method to set the duplicate error flag.

Source

pub fn disk_dup_info(self, duplicate: bool) -> FlexiConfig

Convenience method to set the duplicate info flag.

Source

pub fn disk_filename_ext(self, ext: &str) -> FlexiConfig

Convenience method to set the extension for the log files.

Source

pub fn disk_filename_time(self, enable: bool) -> FlexiConfig

Convenience method to enable/disable a timestamp in the log file names.

Source

pub fn disk_file_size(self, size: u32) -> FlexiConfig

Convenience method to set a file size limit. Setting a size of 0 will disable the file size limit.

Source

pub fn disk_filename_suffix(self, suffix: &str) -> FlexiConfig

Convenience method to set the log file name suffix.

Source

pub fn app_priority(self, level: Priority) -> FlexiConfig

Convenience method to set the level for logging messages from the application.

Source§

impl FlexiConfig

Source

pub fn new( project: &str, dependencies: BTreeMap<String, Option<Priority>>, ) -> FlexiConfig

Creates an instance of the object from the values passed in.

This could be used to create a configuration programatically.

§Examples

Create an initial configuration file that can be modified by a human.

use std::collections::BTreeMap;
use flexi_config::{FlexiConfig, Priority};

let mut depends =  BTreeMap::new();

depends.insert(String::from("time"), None);
depends.insert(String::from("rand"), Some(Priority::Debug));

let config = FlexiConfig::new("foo", depends);

config.to_toml_file("log.toml");

Create a configuration and apply it to flexi_config immediately.

use std::collections::BTreeMap;
use flexi_config::{FlexiConfig, Priority};

let mut depends =  BTreeMap::new();

depends.insert(String::from("time"), Some(Priority::Warn));
depends.insert(String::from("rand"), Some(Priority::Debug));

let config = FlexiConfig::new("foo", depends);

config.apply().unwrap();
Source

pub fn apply(self) -> Result<(), String>

Applies the configuration to flexi_logger.

Any log messages generated before this call will be lost. This method should only ever be called once, the logger configuration cannot change after it is set.

§Examples

Apply a programatic configuration that doesn’t allow the user to change anything.

use std::collections::BTreeMap;
use flexi_config::{FlexiConfig, Priority};

let mut depends =  BTreeMap::new();

depends.insert(String::from("time"), Some(Priority::Warn));
depends.insert(String::from("rand"), Some(Priority::Debug));

let config = FlexiConfig::new("foo", depends);

config.apply().unwrap();

Load a configuration file from disk and apply it.

use flexi_config::FlexiConfig;

let config = FlexiConfig::from_toml_file("log.toml").unwrap();

config.apply().unwrap();

Trait Implementations§

Source§

impl Clone for FlexiConfig

Source§

fn clone(&self) -> FlexiConfig

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FlexiConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.