Skip to content

Commit

Permalink
Customise log level via --log-level CLI argument
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesridgway committed Oct 7, 2023
1 parent cee3cb2 commit a60e4fc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
9 changes: 2 additions & 7 deletions attachment_downloader/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 6 additions & 5 deletions bin/attachment-downloader
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit a60e4fc

Please sign in to comment.