J-F-Liu / lopdf

A Rust library for PDF document manipulation.
MIT License
1.59k stars 163 forks source link

Reduce log verbosity #260

Open louis030195 opened 7 months ago

louis030195 commented 7 months ago

Hey, I'm using lopdf in https://github.com/stellar-amenities/assistants and it's polluting my logs with thousands of:

[lopdf::document] StandardEncoding

Any recommendation to reduce verbosity?

I saw

    pub fn decode_text(encoding: Option<&str>, bytes: &[u8]) -> String {
        if let Some(encoding) = encoding {
            info!("{}", encoding);
            match encoding {
                "StandardEncoding" => bytes_to_string(encodings::STANDARD_ENCODING, bytes),
                "MacRomanEncoding" => bytes_to_string(encodings::MAC_ROMAN_ENCODING, bytes),
                "MacExpertEncoding" => bytes_to_string(encodings::MAC_EXPERT_ENCODING, bytes),
                "WinAnsiEncoding" => bytes_to_string(encodings::WIN_ANSI_ENCODING, bytes),
                "UniGB-UCS2-H" | "UniGB−UTF16−H" => UTF_16BE.decode(bytes).0.to_string(),
                "Identity-H" => "?Identity-H Unimplemented?".to_string(), // Unimplemented
                _ => String::from_utf8_lossy(bytes).to_string(),
            }
        } else {
            bytes_to_string(encodings::STANDARD_ENCODING, bytes)
        }
    }

Not sure what's the best practice here with the info

maxpowel commented 7 months ago

I had the same issue with other libraries and my workaround is to configure the log library to filter some kind of messages.

I use fern https://docs.rs/fern/latest/fern/struct.Dispatch.html#method.level_for and with this you can set a custom level for lopdf::document

arifd commented 1 month ago

I have the same problem so I always put lopdf=off in my env filter.

Heinenen commented 3 weeks ago

I'm not very experienced with logging, but if I understand the docs of log correctly, it is not the libraries (lopdf's) job to filter the logs. This job falls to the logging implementation, which in your case is probably env_logger. Setting RUST_LOG=info,lopdf=warn or even RUST_LOG=info,lopdf=off should filter/disable any logging from lopdf.

Maybe we could also lower the log level in this particular instance from info to debug, IMO it isn't that interesting to know which encoding was used, especially without even knowing in which object.