|
| 1 | +//! Example showing how to use logging with a rolling trigger based on time |
| 2 | +//! |
| 3 | +//! NB: The interval used in the example is intentionally short so multiple file |
| 4 | +//! will be created in the 6 seconds that the example is set to run and is not |
| 5 | +//! intended for practical for usage |
| 6 | +
|
| 7 | +/// This is the time config after which a new file should be created. For the demo |
| 8 | +/// it is set to 2s which is very short and only for demo purposes |
| 9 | +const TIME_TRIGGER_CONFIG: TimeTriggerConfig = TimeTriggerConfig { |
| 10 | + interval: TimeTriggerInterval::Second(2), |
| 11 | + max_random_delay: 0, |
| 12 | + modulate: false, |
| 13 | +}; |
| 14 | + |
| 15 | +/// Delay between log messages for demo purposes |
| 16 | +const TIME_BETWEEN_LOG_MESSAGES: Duration = Duration::from_millis(10); |
| 17 | + |
| 18 | +/// Number of archive log files to keep |
| 19 | +const LOG_FILE_COUNT: u32 = 3; |
| 20 | + |
| 21 | +/// Time demo is set to run for (Set to be long enough for multiple files to be created) |
| 22 | +const RUN_TIME: Duration = Duration::from_secs(6); |
| 23 | + |
| 24 | +/// Location where logs will be written to |
| 25 | +const FILE_PATH: &str = "/tmp/foo.log"; |
| 26 | + |
| 27 | +/// Location where log archives will be moved to |
| 28 | +/// For Pattern info See: |
| 29 | +/// https://docs.rs/log4rs/*/log4rs/append/rolling_file/policy/compound/roll/fixed_window/struct.FixedWindowRollerBuilder.html#method.build |
| 30 | +const ARCHIVE_PATTERN: &str = "/tmp/archive/foo.{}.log"; |
| 31 | + |
| 32 | +use std::{ |
| 33 | + thread::sleep, |
| 34 | + time::{Duration, Instant}, |
| 35 | +}; |
| 36 | + |
| 37 | +use log::{debug, error, info, trace, warn, LevelFilter, SetLoggerError}; |
| 38 | +use log4rs::{ |
| 39 | + append::{ |
| 40 | + console::{ConsoleAppender, Target}, |
| 41 | + rolling_file::policy::compound::{ |
| 42 | + roll::fixed_window::FixedWindowRoller, |
| 43 | + trigger::time::{TimeTrigger, TimeTriggerConfig, TimeTriggerInterval}, |
| 44 | + CompoundPolicy, |
| 45 | + }, |
| 46 | + }, |
| 47 | + config::{Appender, Config, Root}, |
| 48 | + encode::pattern::PatternEncoder, |
| 49 | + filter::threshold::ThresholdFilter, |
| 50 | +}; |
| 51 | + |
| 52 | +fn main() -> Result<(), SetLoggerError> { |
| 53 | + let level = log::LevelFilter::Info; |
| 54 | + |
| 55 | + // Build a stderr logger. |
| 56 | + let stderr = ConsoleAppender::builder().target(Target::Stderr).build(); |
| 57 | + |
| 58 | + let trigger = TimeTrigger::new(TIME_TRIGGER_CONFIG); |
| 59 | + let roller = FixedWindowRoller::builder() |
| 60 | + .base(0) // Default Value (line not needed unless you want to change from 0 (only here for demo purposes) |
| 61 | + .build(ARCHIVE_PATTERN, LOG_FILE_COUNT) // Roll based on pattern and max 3 archive files |
| 62 | + .unwrap(); |
| 63 | + let policy = CompoundPolicy::new(Box::new(trigger), Box::new(roller)); |
| 64 | + |
| 65 | + // Logging to log file. (with rolling) |
| 66 | + let logfile = log4rs::append::rolling_file::RollingFileAppender::builder() |
| 67 | + // Pattern: https://docs.rs/log4rs/*/log4rs/encode/pattern/index.html |
| 68 | + .encoder(Box::new(PatternEncoder::new("{l} - {m}\n"))) |
| 69 | + .build(FILE_PATH, Box::new(policy)) |
| 70 | + .unwrap(); |
| 71 | + |
| 72 | + // Log Trace level output to file where trace is the default level |
| 73 | + // and the programmatically specified level to stderr. |
| 74 | + let config = Config::builder() |
| 75 | + .appender(Appender::builder().build("logfile", Box::new(logfile))) |
| 76 | + .appender( |
| 77 | + Appender::builder() |
| 78 | + .filter(Box::new(ThresholdFilter::new(level))) |
| 79 | + .build("stderr", Box::new(stderr)), |
| 80 | + ) |
| 81 | + .build( |
| 82 | + Root::builder() |
| 83 | + .appender("logfile") |
| 84 | + .appender("stderr") |
| 85 | + .build(LevelFilter::Trace), |
| 86 | + ) |
| 87 | + .unwrap(); |
| 88 | + |
| 89 | + // Use this to change log levels at runtime. |
| 90 | + // This means you can change the default log level to trace |
| 91 | + // if you are trying to debug an issue and need more logs on then turn it off |
| 92 | + // once you are done. |
| 93 | + let _handle = log4rs::init_config(config)?; |
| 94 | + |
| 95 | + error!("Goes to stderr and file"); |
| 96 | + warn!("Goes to stderr and file"); |
| 97 | + info!("Goes to stderr and file"); |
| 98 | + debug!("Goes to file only"); |
| 99 | + trace!("Goes to file only"); |
| 100 | + |
| 101 | + // Generate some log messages to trigger rolling |
| 102 | + let instant = Instant::now(); |
| 103 | + while instant.elapsed() < RUN_TIME { |
| 104 | + info!("Running for {:?}", instant.elapsed()); |
| 105 | + sleep(TIME_BETWEEN_LOG_MESSAGES); |
| 106 | + } |
| 107 | + info!( |
| 108 | + "See '{}' for log and '{}' for archived logs", |
| 109 | + FILE_PATH, ARCHIVE_PATTERN |
| 110 | + ); |
| 111 | + |
| 112 | + Ok(()) |
| 113 | +} |
0 commit comments