Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Direct DEBUG and INFO messages to stdout #65

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
44 changes: 26 additions & 18 deletions cvdupdate/cvdupdate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="cvdupdate",
version="1.1.2",
version="1.1.3",
author="The ClamAV Team",
author_email="[email protected]",
copyright="Copyright (C) 2022 Cisco Systems, Inc. and/or its affiliates. All rights reserved.",
Expand Down