From b3e77b905e953e08add5a00d4e35caaab63fb6cc Mon Sep 17 00:00:00 2001 From: Micah Snyder Date: Tue, 12 Nov 2024 10:57:46 -0500 Subject: [PATCH 1/2] Direct DEBUG and INFO messages to stdout Currently all log messages are going to stderr. Fixes: https://github.com/Cisco-Talos/cvdupdate/issues/43 Patch courtesy of GitHub user backbord. --- cvdupdate/cvdupdate.py | 44 +++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/cvdupdate/cvdupdate.py b/cvdupdate/cvdupdate.py index 1d10e22..fe8f159 100644 --- a/cvdupdate/cvdupdate.py +++ b/cvdupdate/cvdupdate.py @@ -130,24 +130,12 @@ def _init_logging(self) -> None: """ Initializes the logging parameters. """ - self.logger = logging.getLogger(f"cvdupdate-{self.version}") - - if self.verbose: - self.logger.setLevel(logging.DEBUG) - else: - self.logger.setLevel(logging.INFO) - - formatter = logging.Formatter( - fmt="%(asctime)s - %(levelname)s: %(message)s", - datefmt="%Y-%m-%d %I:%M:%S %p", - ) - today = datetime.datetime.now() - self.log_file = self.log_dir / f"{today.strftime('%Y-%m-%d')}.log" + log_file = self.log_dir / f"{today:%Y-%m-%d}.log" if not self.log_dir.exists(): # Make a new log directory - os.makedirs(os.path.split(self.log_file)[0]) + os.makedirs(log_file.parent) else: # Log dir already exists, lets check if we need to prune old logs logs = self.log_dir.glob('*.log') @@ -158,11 +146,31 @@ def _init_logging(self) -> None: # Log is too old, delete! os.remove(str(log)) - self.filehandler = logging.FileHandler(filename=self.log_file) - self.filehandler.setLevel(self.logger.level) - self.filehandler.setFormatter(formatter) + stderr_level = logging.WARNING - self.logger.addHandler(self.filehandler) + stderr_handler = logging.StreamHandler(sys.stderr) + stderr_handler.setLevel(stderr_level) + + class FilterNotStdErr(logging.Filter): + def filter(self, record: logging.LogRecord) -> bool: + return record.levelno < stderr_level + + stdout_handler = logging.StreamHandler(sys.stdout) + stdout_handler.addFilter(FilterNotStdErr()) + + logging.basicConfig( + level=logging.DEBUG if self.verbose else logging.INFO, + format="%(asctime)s - %(levelname)s: %(message)s", + datefmt="%Y-%m-%d %I:%M:%S %p", + force=True, # an import might already have the root logger configured + handlers=[ + stderr_handler, + stdout_handler, + logging.FileHandler(log_file), + ], + ) + + self.logger = logging.getLogger(f"cvdupdate-{self.version}") # Also set the log level for urllib3, because it's "DEBUG" by default, # and we may not like that. From 8c1616b98ea57a7fc26d26b9e34a66c6b2dadecb Mon Sep 17 00:00:00 2001 From: Micah Snyder Date: Tue, 12 Nov 2024 11:30:23 -0500 Subject: [PATCH 2/2] Bump version to 1.1.3 Add release notes. --- CHANGES.md | 6 ++++++ setup.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index e9adbd6..f98647b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,6 +8,12 @@ > - Fixed: 🐛 > - Security: 🛡 +## Version 1.1.3 + +🌌 Info-level and Debug-level messages will now go to stdout instead of stderr. + Work courtesy of GitHub user backbord. + - GitHub Pull-Request: https://github.com/Cisco-Talos/cvdupdate/pull/65 + ## Version 1.1.2 ➕ Added a Docker Compose file to make it easier to host a private mirror. diff --git a/setup.py b/setup.py index f2d9f20..f17cebf 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="cvdupdate", - version="1.1.2", + version="1.1.3", author="The ClamAV Team", author_email="clamav-bugs@external.cisco.com", copyright="Copyright (C) 2022 Cisco Systems, Inc. and/or its affiliates. All rights reserved.",