1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use chrono::prelude::*;
use env_logger::fmt::Formatter;
use log::LevelFilter;
use log::Record;
use std::io;
use std::io::prelude::*;
use std::str::FromStr;

/// Initialize logging
///
/// Precedence: LOG_LEVEL > RUST_LOG > 'verbosity' argument.
///
/// Env:
///
/// LOG_LEVEL: Used to control levels for modules. Overrides RUST_LOG
/// This is useful to debug app only when executed through other tools
/// like `cargo run`. Falls back to RUST_LOG if not provided.
///
/// LOG_UTC: 1 for UTC, 0 for local. UTC by default
///  
/// Colors
///
/// Colors are automatic and will be disabled on pipes, or when TERM=dumb
/// is passed along.
///
pub fn init() {
    init_with_verbosity(Verbosity::Info as u8);
}

pub fn init_with_verbosity(verbosity_level: u8) {
    use env_logger::*;
    use std::env;

    const LOG_LEVEL_ENV: &str = "LOG_LEVEL";
    const LOG_UTC_ENV: &str = "LOG_UTC";

    let mut env = Env::new();
    let mut has_opts = false;
    // Use default RUST_LOG env
    let env_level = env::var(DEFAULT_FILTER_ENV);
    if env_level.is_ok() {
        has_opts = true;
    }

    // Use app specific LOG_LEVEL env that overrides RUST_LOG
    let env_level = env::var(LOG_LEVEL_ENV);
    if env_level.is_ok() {
        env = env.filter(LOG_LEVEL_ENV);
        has_opts = true;
    }

    let mut builder = Builder::from_env(env);
    if !has_opts {
        // set default log level
        builder.filter_level(Verbosity::from_occurrence(verbosity_level).log_level_filter());
    }

    let ts_utc = match env::var(LOG_UTC_ENV) {
        Ok(v) => i32::from_str(&v).unwrap_or(1) > 0,
        Err(_) => false,
    };

    builder.format(get_formatter(ts_utc));
    builder.init();
}

fn get_formatter(
    ts_utc: bool,
) -> impl Fn(&mut Formatter, &Record<'_>) -> io::Result<()> + Sync + Send + 'static {
    move |buf: &mut Formatter, record: &Record<'_>| {
        use env_logger::fmt::Color;

        let l = record.level();
        let lvl_style = buf.default_level_style(l);
        let mut ts_style = buf.style();
        let ts_style = ts_style.set_color(Color::Rgb(140, 140, 140));
        let mut path_style = buf.style();
        let path_style = path_style.set_color(Color::Cyan);

        let ts = if ts_utc {
            Utc::now().to_rfc3339_opts(SecondsFormat::Millis, true)
        } else {
            Local::now().to_rfc3339_opts(SecondsFormat::Millis, false)
        };
        writeln!(
            buf,
            "{} {} [{}] {}",
            lvl_style.value(l),
            ts_style.value(ts),
            path_style.value(record.target()),
            record.args()
        )
    }
}

#[derive(Copy, Clone, Debug)]
#[repr(u8)]
pub enum Verbosity {
    Warn = 0,
    Info,
    Debug,
    Trace,
}

impl Verbosity {
    /// Convert u8 to verbosity
    /// Anything other invalid value returns Info, as that's usually the safest
    /// not over-burdening log systems, while still providing some verbosity.
    ///
    /// Note: This is log verbosity, not log control, so doesn't support levels like
    /// Off. For that LOG_LEVEL can be used.
    pub fn from_occurrence(val: u8) -> Verbosity {
        use self::Verbosity::*;
        match val {
            0 => Warn,
            1 => Info,
            2 => Debug,
            3 => Trace,
            _ => Info,
        }
    }

    pub fn from_signed_occurrence(val: i8) -> Verbosity {
        let v = if val < 0 { 0u8 } else { val as u8 };
        Verbosity::from_occurrence(v)
    }

    fn log_level_filter(self) -> LevelFilter {
        use log::LevelFilter::*;
        match self as u8 {
            0 => Warn,
            1 => Info,
            2 => Debug,
            3 => Trace,
            _ => Info,
        }
    }
}

impl From<u8> for Verbosity {
    fn from(val: u8) -> Verbosity {
        Verbosity::from_occurrence(val)
    }
}