Simplelog--a Simple and Easy-to-use logging facility for Rust ' s log crate.
The goal of Simplelog is not to provide a very rich feature, nor to provide the best logging solution, but to provide easy-to-use log solutions for small and medium sized projects.
Simplelog does not aim to provide a rich set of features, nor to provide the best logging solution. It aims to being a maintainable, easy to integrate facility for small to medium sized Mewhat lacking in features. In those cases simplelog should provide a easy alternative.
GitHub Address
Simplelog provides some logging facilities as follows (also the most important concept in Simplelog): Simplelogger--very basic logger that logs to Stderr/out, Should never fail termlogger--advanced terminal logger, that splits to stderr/out and has color support (can excluded On unsupported platforms) Writelogger--logs to a given struct implementing Write. e.g. a file combinedlogger--can be used to form combinations of the above loggers
Corresponds to the 4 important structs in Simplelog:
parsing Simplelogger Source code is as follows:
//!
Module providing the Simplelogger implementation use Std::io::{stderr, stdout};
Use Log::{loglevel, Loglevelfilter, Logmetadata, LogRecord, Setloggererror, Set_logger, log};
Use:: {Config, sharedlogger};
Use Super::logging::try_log; The Simplelogger struct. Provides a very basic Logger implementation pub struct Simplelogger {level:loglevelfilter, config:config,} im PL Simplelogger {///init function.
Globally initializes the Simplelogger as the one and only used log facility. Takes the desired ' loglevel ' and ' Config ' as arguments.
They cannot is changed later on.
Fails if another Logger was already initialized. PUB FN init (Log_level:loglevelfilter, Config:config)-> result< (), setloggererror> {Set_logger (|max_lo g_level|
{Max_log_level.set (Log_level.clone ()); Simplelogger::new (log_level, config)})}///allows to create a new logger, which can be independently us Ed, no MAtter what is globally set. No macros are provided for this and your probably///dont want to the use this function, but ' init () ', if
You are dont want to build a ' combinedlogger '. Takes the desired ' loglevel ' and ' Config ' as arguments.
They cannot is changed later on. PUB fn New (Log_level:loglevelfilter, config:config)-> box<simplelogger> {box::new (Simplelogger {leve L:log_level, Config:config})}//implementation log Trait Impl log for Simplelogger {fn enabled (&self, metadata: &am P
Logmetadata)-> bool {metadata.level () <= Self.level} fn log (&self, record: &logrecord) {
If Self.enabled (Record.metadata ()) {match record.level () {loglevel::error => {
Let stderr = stderr ();
Let Mut stderr_lock = Stderr.lock ();
Let _ = Try_log (&self.config, record, &mut Stderr_lock);
}, _ => {Let stdout = stdout ();
Let Mut stdout_lock = Stdout.lock ();
Let _ = Try_log (&self.config, record, &mut Stdout_lock); //Implementation Sharedlogger Trait Impl Sharedlogger for Simplelogger {fn level (&self)-& Gt Loglevelfilter {self.level} fn config (&self)-> option<&config> {Some (&am P;self.config)} fn As_log (self:box<self>)-> box<log> {box::new (*self)}}
Simplelogger is the simplest of the code, and other structs source code refer to: Https://github.com/Drakulix/simplelog.rs/tree/master/src/loggers
In addition , because the following usage example uses Combinedlogger, so list its source code and learn about it:
//! Module providing the Combinedlogger implementation use Log::{loglevelfilter, Logmetadata, LogRecord, SetLoggerError, set
_logger, Log};
Use:: {sharedlogger, Config}; The Combinedlogger struct.
Provides a Logger implementation that proxies multiple loggers as one.
The purpose is to allow multiple loggers to be set globally pub struct Combinedlogger {level:loglevelfilter, Logger:vec<box<sharedlogger>>,} impl combinedlogger {///init function.
Globally initializes the Combinedlogger as the one and only used log facility. Takes all used loggers as a Vector argument.
None of those loggers should already be set globally. All loggers need to implement ' Log::log ' and ' logger::sharedlogger ' and need to provide a way to is///in itialized without calling ' Set_logger '. All loggers the This library provide a ' new (..)
' Method///to that purpose.
Fails if another logger is already set globally. PubFN Init (logger:vec<box<sharedlogger>>)-> result< (), setloggererror> {Set_logger (|max_log_l evel|
{Let result = combinedlogger::new (logger);
Max_log_level.set (Result.level ()); Result})}///allows to create a new logger, which can be independently used, no matter whats globally s
Et. No macros are provided for this and your probably///dont want to the use this function, but ' init () ', if
You are dont want to build a ' combinedlogger '. Takes all used loggers as a Vector argument.
The log level was automatically determined by the///lowest log level used by the given loggers.
All loggers need to implement Log::log. PUB fn New (logger:vec<box<sharedlogger>>)-> box<combinedlogger> {let mut log_level = LogLe
Velfilter::off; For log in &logger {if log_level < Log.level () {Log_leveL = Log.level (); } box::new (Combinedlogger {level:log_level, logger:logger})}//implementation log Trait Impl log for Com Binedlogger {fn enabled (&self, metadata: &logmetadata)-> bool {metadata.level () <= Self.leve L} fn log (&self, record: &logrecord) {if self.enabled (Record.metadata ()) {for log
In &self.logger {log.log (record); //Implementation Sharedlogger Trait Impl Sharedlogger for Combinedlogger {fn level (&self)-> ilter {self.level} fn config (&self)-> option<&config> {None} f n As_log (self:box<self>)-> box<log> {box::new (*self)}}
There is a config structs in it, the corresponding source code is as follows:
Use Log::loglevel; Configuration for the "loggers//////all loggers print the" following form:///' 00:00:00 [level] Crate
:: module: [lib.rs::100] your_message '///Every space delimited part except the actual message is optional. Pass this struct to your logger to the change when these information shall///are logged.
Every part can being enabled for a specific loglevel and are then///automatically enable for all lower levels as.
The "result is" that "logging gets more detailed" verbose it gets. e.g. to have one part shown always use ' loglevel::error '.
But If you are///want to show the source line only in ' Trace ' use that.
Passing ' None ' would completely disable the part. #[derive (Debug, Clone, Copy, Partialeq, Eq)] pub-struct Config {///at which level and below the ' current time ' shall be Logged pub Time:option<loglevel>,///at which level and below the level itself shall is logged pub Lev El:option<loglevel>,///at which level and below the target shall is logged pub target:option<loglevel>///at which level
And below a source code reference shall is logged pub location:option<loglevel>, Impl Default for Config { fn default ()-> Config {config {time:some (loglevel::error), Level:some (loglevel :: Error), Target:some (loglevel::D ebug), Location:some (Loglevel::trace),}}}
Usage examples:
#[macro_use]
extern crate log;
extern crate Simplelog;
fn Main () {
simplelog_fn ();
}
#[warn (dead_code)]
fn SIMPLELOG_FN () {use
std::fs::file;
Use Simplelog::*;
Combinedlogger::init (
vec![
Termlogger::new (Loglevelfilter::warn, Config::d efault ()). Unwrap (),//terminal logger writelogger::new
( Loglevelfilter::info, Config::d efault (), File::create ("My_rust_binary.log"), Unwrap ()),//Logging to "*.log" file
]
). Unwrap ();
error! ("Bright red Error");
Info! ("This is appears in the log File");
debug! (' This ' is currently ' not ' enabled for any logger ');
}
Run Result:
The terminal appears as follows:
The contents of the log file are as follows: