Repository: alacritty/alacritty
File: alacritty/src/logging.rs (Rust)

Code before:
        }

        // Create log message for the given `record` and `target`.
        let message = create_log_message(record, target, self.tz_offset);

        if let Ok(mut logfile) = self.logfile.lock() {
            // Write to logfile.
            let _ = logfile.write_all(message.as_ref());

            // Log relevant entries to message bar.
            self.message_bar_log(record, &logfile.path.to_string_lossy());
        }

        // Write to stdout.
        if let Ok(mut stdout) = self.stdout.lock() {
            let _ = stdout.write_all(message.as_ref());
        }
    }

    fn flush(&self) {}
}

fn create_log_message(record: &log::Record<'_>, target: &str, tz_offset: UtcOffset) -> String {
    let now = OffsetDateTime::now_utc()
        .to_offset(tz_offset)
        .format(format_description!(
            "[year]-[month]-[day] [hour repr:24]:[minute]:[second].[subsecond digits:9]Z"
        ))
        .unwrap();
    let mut message = format!("[{}] [{:<5}] [{}] ", now, record.level(), target);

    // Alignment for the lines after the first new line character in the payload. We don't deal
    // with fullwidth/unicode chars here, so just `message.len()` is sufficient.
    let alignment = message.len();

    // Push lines with added extra padding on the next line, which is trimmed later.
    let lines = record.args().to_string();
    for line in lines.split('\n') {
        let line = format!("{}\n{:width$}", line, "", width = alignment);
        message.push_str(&line);
    }

    // Drop extra trailing alignment.
    message.truncate(message.len() - alignment);
    message
}

/// Check if log messages from a crate should be logged.
fn is_allowed_target(level: Level, target: &str) -> bool {
    match (level, log::max_level()) {
        (Level::Error, LevelFilter::Trace) | (Level::Warn, LevelFilter::Trace) => true,

Review comment (refactor):
If you move this `format_description!` out of the parameter, it'll clean stuff up:

```rust
    let time_format = format_description!(
        "[year]-[month]-[day] [hour repr:24]:[minute]:[second].[subsecond digits:9]"
    );
    let now = OffsetDateTime::now_utc().to_offset(tz_offset).format(time_format).unwrap();
```

Code after:
        }

        // Create log message for the given `record` and `target`.
        let message = create_log_message(record, target, self.tz_offset);

        if let Ok(mut logfile) = self.logfile.lock() {
            // Write to logfile.
            let _ = logfile.write_all(message.as_ref());

            // Log relevant entries to message bar.
            self.message_bar_log(record, &logfile.path.to_string_lossy());
        }

        // Write to stdout.
        if let Ok(mut stdout) = self.stdout.lock() {
            let _ = stdout.write_all(message.as_ref());
        }
    }

    fn flush(&self) {}
}

fn create_log_message(record: &log::Record<'_>, target: &str, tz_offset: UtcOffset) -> String {
    let time_format = format_description!(
        "[year]-[month]-[day] [hour repr:24]:[minute]:[second].[subsecond digits:9]"
    );
    let now = OffsetDateTime::now_utc().to_offset(tz_offset).format(time_format).unwrap();
    let mut message = format!("[{}] [{:<5}] [{}] ", now, record.level(), target);

    // Alignment for the lines after the first new line character in the payload. We don't deal
    // with fullwidth/unicode chars here, so just `message.len()` is sufficient.
    let alignment = message.len();

    // Push lines with added extra padding on the next line, which is trimmed later.
    let lines = record.args().to_string();
    for line in lines.split('\n') {
        let line = format!("{}\n{:width$}", line, "", width = alignment);
        message.push_str(&line);
    }

    // Drop extra trailing alignment.
    message.truncate(message.len() - alignment);
    message
}

/// Check if log messages from a crate should be logged.
fn is_allowed_target(level: Level, target: &str) -> bool {
    match (level, log::max_level()) {
        (Level::Error, LevelFilter::Trace) | (Level::Warn, LevelFilter::Trace) => true,
        _ => ALLOWED_TARGETS.contains(&target),
    }