Struct session_log::Logger

source ·
pub struct Logger(/* private fields */);

Implementations§

source§

impl Logger

source

pub fn with_options( name: impl Into<String>, level: Level, directory: impl Into<String> ) -> Result<Logger, ErrorKind>

Create a new logging entry with the given name & options.
OR
Retrieve an existing logging entry if the entry already exists and have same options.

§Parameters
  • name - The name of the logging entry.
  • level - The level of the logging entry.
  • directory - The directory where the log files will be stored. (Relative to the current working directory)
§Errors
  • ErrorKind::DifferentLevel if the existing logger has different level than the given options.
  • ErrorKind::DifferentDirectory if the existing logger has different directory than the given options.
  • ErrorKind::FailedToCreateFolder if the directory cannot be created.
§Examples
use session_log::{Logger, Level};

fn main() {
  let logger = Logger::with_options(
    "main", Level::Debug, "logs/main");

  let logger2 = Logger::with_options(
    "main", Level::Debug, "logs/main");  // Retrieve the existing logger

  let logger3 = Logger::with_options(
    "main", Level::Info , "logs/main");  // Error: Different level

  let logger4 = Logger::with_options(
    "main", Level::Debug, "logs/other"); // Error: Different directory
}
source

pub fn try_with_option( name: impl Into<String>, level: Level, directory: impl Into<String> ) -> Logger

Create a new logging entry with the given name & options.
OR
Retrieve an existing logging entry if the entry already exists.

Unlike Logger::with_options, this method doesn’t return an error if the existing logger has different options. Instead, it will just ignore the given options and return the existing logger.

§Parameters
  • name - The name of the logging entry.
  • level - The level of the logging entry.
  • directory - The directory where the log files will be stored. (Relative to the current working directory)
§Examples
use session_log::{Logger, Level};

fn main() {
  let logger = Logger::try_with_option(
    "main", Level::Debug, "logs/main");

  let logger2 = Logger::try_with_option(
    "main", Level::Debug, "logs/main");  // Retrieve the existing logger

  let logger3 = Logger::try_with_option(
    "main", Level::Info , "logs/main");  // Retrieve the existing logger

  let logger4 = Logger::try_with_option(
    "main", Level::Debug, "logs/other"); // Retrieve the existing logger
}
source

pub fn new(name: impl Into<String>) -> Logger

Create a new logging entry with the given name. Level and directory is defaulted to Info and “logs”
OR
Retrieve an existing logging entry if the entry already exists.

Unlike Logger::with_options, this method doesn’t return an error if the existing logger has different options. Instead, it will just ignore the given options and return the existing logger.

§Parameters
  • name - The name of the logging entry.
§Examples
use session_log::Logger;

fn main() {
  let logger  = Logger::new("main");
  let logger2 = Logger::new("main"); // Retrieve the existing logger
}
source

pub fn get_level(&self) -> Level

Get logging level for this entry.

§Examples
use session_log::{Logger, Level};

fn main() {
  let logger = Logger::new("main");

  assert_eq!(logger.get_level(), Level::Info);
}
source

pub fn set_level(&mut self, level: Level)

Set logging level for this entry

§Examples
use session_log::{Logger, Level};

fn main() {
  let mut logger = Logger::new("main");

  logger.set_level(Level::Debug);
  assert_eq!(logger.get_level(), Level::Debug);

  logger.set_level(Level::Info);
  assert_eq!(logger.get_level(), Level::Info);
}
source

pub fn get_directory(&self) -> String

Get logging directory for this entry.

§Examples
use session_log::Logger;

fn main() {
  let logger = Logger::new("main");

  assert_eq!(logger.get_directory(), "logs");
}
source

pub fn set_directory(&mut self, directory: impl Into<String>) -> Result<()>

Set logging directory for this entry and create the directory if it doesn’t exist. the result of creating the directory is returned.

§Examples
use session_log::Logger;

fn main() {
  let mut logger = Logger::new("main");

  logger.set_directory("logs/main");
  assert_eq!(logger.get_directory(), "logs/main");

  logger.set_directory("logs/other");
  assert_eq!(logger.get_directory(), "logs/other");
}
source

pub fn session(&self, name: &str) -> Session

Create a new session with the given name under this logging entry.

Trait Implementations§

source§

impl Clone for Logger

source§

fn clone(&self) -> Logger

Returns a copy 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 Logger

source§

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

Formats the value using the given formatter. Read more
source§

impl Loggable for Logger

source§

fn log(&self, level: Level, message: &str)

Log a message with the dynamic level.
source§

fn debug(&self, message: &str)

Log a message at the debug level with caller position.
source§

fn verbose(&self, message: &str)

Log a message at the verbose level with caller position.
source§

fn info(&self, message: &str)

Log a message at the info level with caller position.
source§

fn warning(&self, message: &str)

Log a message at the warning level with caller position.
source§

fn critical(&self, message: &str)

Log a message at the critical level with caller position.
source§

fn error(&self, message: &str)

Log a message at the error level with caller position.
source§

fn fatal(&self, message: &str) -> !

Log a message at the fatal level with caller position then panic. Read more

Auto Trait Implementations§

§

impl Freeze for Logger

§

impl RefUnwindSafe for Logger

§

impl Send for Logger

§

impl Sync for Logger

§

impl Unpin for Logger

§

impl UnwindSafe for Logger

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> 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,

§

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>,

§

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>,

§

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.