victordomingos / optimize-images

A command-line interface (CLI) utility written in pure Python to help you reduce the file size of images.
https://no-title.victordomingos.com/projects/optimize-images
MIT License
272 stars 49 forks source link

Log #5

Open asgeek96 opened 5 years ago

asgeek96 commented 5 years ago

Hi Can a option be added to create a log file for the optimized images.

victordomingos commented 5 years ago

I will consider adding it in a future version. I am quite busy right now, so if someone reading this is willing to contribute I may accept a pull request as long as it does not break existing funtionality.

derenix commented 3 years ago

We may can do something like this with pythons logging framework, e.g like this.

import logging
import sys

configured_handlers = [logging.FileHandler(args.log_file), logging.StreamHandler(sys.stdout)]
logging.basicConfig(level=logging.INFO, format="%(message)s", handlers=configured_handlers)

This will log to args.log_file and at the same time to stdout, but I don't know if its that what @asgeek96 is trying to archieve here?

victordomingos commented 3 years ago

I agree about using the standard library's logging module, not adding new dependencies.

I would keep logging and screen output as separate operations, since the features needed tend to be a bit different in each case. With regards to logging, I think we should have the ability to specify the file path and, either now or in a future update, a few customisation options. For instance:

1. Default (full) report

Write the full report (files, size changes, summary and errors, similar to what we are already printing to screen) to the specified file:

optimize-images --report ./report.txt

2. Errors log

Write a smaller report including just any errors found to the specified file:

optimize-images --log-errors ./errorslog.txt 
optimize-images --le ./errorslog.txt 

3. Processed files log

Write a smaller report including the list of all processed (i.e. modified) images paths (one file path per line, without further details, in order to allow integration with other tools) to the specified file:

optimize-images --log-files ./logfiles.txt 
optimize-images --lf ./logfiles.txt 

By keeping these options separate, we can implement it one at a time, but do it in a way that makes it easy to add the other options. Also, it would allow the user to combine those options in a single command, for instance, getting an error log and a processed files list (or even the 3 kinds of reports, if needed), in separate files:

optimize-images --log-files ./logfiles.txt --log-errors ./errorslog.txt 
optimize-images --lf ./logfiles.txt --le ./errorslog.txt 

What do you think?

derenix commented 3 years ago

But this is something different, isnt it? From my understanding, you're talking about a to save what happend (reporting), not redirecting the output on the screen to a file (logging).

Don't get me wrong, I don't want to do nitpicking here, just want to be clear what the desired outcome should be.

That said, I agree and like the idea to have a reporting interface (maybe a simple csv file would work?) to have the ability for further post-processing the results. For this we probably don't need to have the logging stuff (also for your second and third point), so I would just go for it and write it the informations to the given files.

Let me know what you think :)

victordomingos commented 3 years ago

Right. I try to envision the project as a whole, in order to come up with an implementation strategy that fits the needs not only of a single issue, but for a number of common use cases. It is indeed a different feature, while vaguely related.

I can see how a csv (or json or other structured format) could be useful. But I would let those to a later stage, as format options for the report or log files. Right now, I would start with the approach I described above. I believe the logging module can still be useful at least for error reporting, but I could be wrong. For the rest, probably you are right, no need for it. And later we could extend this reporting feature with additional file format options (e.g., CSV).