emabee / flexi_logger

A flexible logger for rust programs that can write to stderr, stdout, and/or to log files
Apache License 2.0
315 stars 55 forks source link

When using suppress_basename(), the log file still starts with a '_' prefix #176

Closed CoolLoong closed 1 month ago

CoolLoong commented 1 month ago

flexi_logger version

flexi_logger = { version = "0.29.0", features = [
    "compress",
    "colors",
    "async",
] }

code example

let log_dir = PathBuf::from("logs");
let file_spec = FileSpec::default()
    .directory(log_dir)
    .suppress_basename()
    .suppress_timestamp()
    .o_suffix(Option::<String>::None)
    .suffix("log");

let log_to_console_format =
    |write: &mut dyn std::io::Write, now: &mut DeferredNow, record: &Record| {
        let time_prefix = now.format("%H:%M:%S");
        let binding = std::thread::current();
        let thread_name = binding.name().unwrap_or("Unknown thread");
        let level = record.level();
        let file_name = record
            .file()
            .map(|file_path| {
                Path::new(file_path)
                    .file_name()
                    .unwrap_or_else(|| std::ffi::OsStr::new("<unnamed>"))
                    .to_string_lossy()
                    .into_owned()
            })
            .unwrap_or_else(|| "<unnamed>".to_string());
        write!(
            write,
            "[{}] [{}/{}]: [{}] {}",
            style(level).paint(time_prefix.to_string()),
            style(level).paint(thread_name),
            style(level).paint(level.to_string()),
            style(level).paint(file_name),
            &record.args()
        )
    };
let log_to_file_format =
    |write: &mut dyn std::io::Write, now: &mut DeferredNow, record: &Record| {
        let time_prefix = now.format("%H:%M:%S");
        let binding = std::thread::current();
        let thread_name = binding.name().unwrap_or("Unknown thread");
        let level = record.level();
        let file_name = record
            .file()
            .map(|file_path| {
                Path::new(file_path)
                    .file_name()
                    .unwrap_or_else(|| std::ffi::OsStr::new("<unnamed>"))
                    .to_string_lossy()
                    .into_owned()
            })
            .unwrap_or_else(|| "<unnamed>".to_string());
        write!(
            write,
            "[{}] [{}/{}]: [{}] {}",
            time_prefix.to_string(),
            thread_name,
            level.to_string(),
            file_name,
            &record.args()
        )
    };

Logger::with(LogSpecification::from(config.log_level))
    .log_to_file(file_spec)
    .rotate(
        flexi_logger::Criterion::Size(10 * 1024 * 1024),
        flexi_logger::Naming::TimestampsDirect,
        flexi_logger::Cleanup::KeepLogAndCompressedFiles(0, 5),
    )
    .set_palette("b1;3;2;4;6".to_string())
    .format_for_files(log_to_file_format)
    .format_for_stderr(log_to_console_format)
    .duplicate_to_stderr(flexi_logger::Duplicate::Info)
    .write_mode(flexi_logger::WriteMode::Async)
    .start()
    .unwrap()

It outputs _r2024-09-24_16-21-18.log, but I expect is r2024-09-24_16-21-18.log

emabee commented 1 month ago

Fixed with v0.29.1