diff --git a/attachment_downloader/logging.py b/attachment_downloader/logging.py index db0446e..9d67093 100644 --- a/attachment_downloader/logging.py +++ b/attachment_downloader/logging.py @@ -2,16 +2,11 @@ import sys -class InfoFilter(logging.Filter): - def filter(self, record): - return record.levelno in (logging.DEBUG, logging.INFO) - class Logger: @staticmethod - def setup(): + def setup(level): std_out_stream_handler = logging.StreamHandler(sys.stdout) - std_out_stream_handler.setLevel(logging.DEBUG) - std_out_stream_handler.addFilter(InfoFilter()) + std_out_stream_handler.setLevel(logging.getLevelName(level)) std_out_stream_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')) std_err_stream_handler = logging.StreamHandler(sys.stderr) diff --git a/bin/attachment-downloader b/bin/attachment-downloader index edd6e8f..f9186eb 100755 --- a/bin/attachment-downloader +++ b/bin/attachment-downloader @@ -99,14 +99,10 @@ def process_message(filename_template, options, message): mail.delete(uid) if __name__ == '__main__': - Logger.setup() - logging.info(f'Attachment Downloader - Version: {Version.get()} {Version.get_env_info()}') - if sys.version_info[0] < 3: - logging.error("This application requires Python 3+, you are running version: %s", sys.version) + print("This application requires Python 3+, you are running version: " + sys.version) exit(1) - class AttachmentDownloaderOption(Option): TYPES = Option.TYPES + ('date',) TYPE_CHECKER = copy(Option.TYPE_CHECKER) @@ -135,9 +131,14 @@ if __name__ == '__main__': parser.add_option("--unsecure", dest="unsecure", action="store_true", help="disable encrypted connection (not recommended)") parser.add_option("--starttls", dest="starttls", action="store_true", help="enable STARTTLS (not recommended)") + parser.add_option("--log-level", dest="loglevel", default="INFO", + help="Set the log level to DEBUG, INFO, WARNING, ERROR, or CRITICAL (default is INFO)") (options, args) = parser.parse_args() + Logger.setup(options.loglevel) + logging.info(f'Attachment Downloader - Version: {Version.get()} {Version.get_env_info()}') + if not options.host: parser.error('--host parameter required') if not options.username: