WeasyPrint icon indicating copy to clipboard operation
WeasyPrint copied to clipboard

In logs, add filename of problematic CSS

Open whinee opened this issue 1 year ago • 2 comments

Hallo! I use weasyprint programmatically. I have followed the recommended way to log in the documentations, formatted it, and redirected it to stdout. Here is the relevant code.

formatter = logging.Formatter("%(name)s - %(levelname)s - %(message)s")

handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.WARNING)

logger = logging.getLogger("weasyprint")
logger.setLevel(logging.WARNING)

handler.setFormatter(formatter)
logger.addHandler(handler)

But I think its not relevant. In any case, when it logs a problematic CSS rule, it says something like this:

weasyprint - WARNING - Ignored `height: 50vh` at 243:5, invalid value.

Now, my suggestion is to make it so that the filename is mentioned in the logs. This is due to the fact that an API user can use multiple stylesheets, and knowing the filename of the problematic CSS is super helpful.

I have suggestions on how to implement this, but I literally do not know what I am doing, so yea, just some wild suggestions:

  • I suggest that this feature will work when a filename or url argument is passed to weasyprint.CSS. such as that it looks like the following:

    for filename:

    easyprint - WARNING - Ignored `height: 50vh` at file [filename]:243:5, invalid value.
    

    or for url:

    easyprint - WARNING - Ignored `height: 50vh` at url [url] 243:5, invalid value.
    
  • the suggestion above would not work when the passed argument is a file_obj or string, but logging the hash of the contents of the file_obj or the str would probably make it easier to find where that problematic CSS rule is. But yeah... this is probably a bad suggestion, so feel free to ignore this, or this whole issue for that matter, lol.

Thank you, have a great day!

whinee avatar Mar 29 '23 02:03 whinee

Hi!

Now, my suggestion is to make it so that the filename is mentioned in the logs. This is due to the fact that an API user can use multiple stylesheets, and knowing the filename of the problematic CSS is super helpful.

I agree, that would be useful. We would probably have to save the information in each CSS Node in tinycss2, that would make sense.

Until then, you can set the level to INFO. You’ll get some noise, but you’ll at least get the filenames of the related stylesheets.

liZe avatar Mar 29 '23 06:03 liZe

Until then, you can set the level to INFO. You’ll get some noise, but you’ll at least get the filenames of the related stylesheets.

Ooh, thank you!

whinee avatar Mar 29 '23 07:03 whinee