Skip to content

Commit

Permalink
Adjust logging configuration
Browse files Browse the repository at this point in the history
- All messages are written to a log file ("cbi.log")
- Only errors are written to the terminal by default
- Messages written to terminal are based on -q and -v flags

Signed-off-by: John Pennycook <john.pennycook@intel.com>
  • Loading branch information
Pennycook committed Sep 19, 2024
1 parent 7d10f9a commit 2323761
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions codebasin/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,24 @@ def main():

args = parser.parse_args()

stdout_log = logging.StreamHandler(sys.stdout)
stdout_log.setFormatter(logging.Formatter("[%(levelname)-8s] %(message)s"))
log.addHandler(stdout_log)
log.setLevel(
max(1, logging.WARNING - 10 * (args.verbose - args.quiet)),
)
# Configure logging such that:
# - All messages are written to a log file
# - Only errors are written to the terminal by default
# - Messages written to terminal are based on -q and -v flags
formatter = logging.Formatter("[%(levelname)-8s] %(message)s")

file_handler = logging.FileHandler("cbi.log", mode="w")
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(formatter)
log.addHandler(file_handler)
log_path = os.path.realpath("cbi.log")
print(f"Log file created at {log_path}")

log_level = max(1, logging.ERROR - 10 * (args.verbose - args.quiet))
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setLevel(log_level)
stdout_handler.setFormatter(formatter)
log.addHandler(stdout_handler)

# If no specific report was specified, generate all reports.
# Handled here to prevent "all" always being in the list.
Expand Down

0 comments on commit 2323761

Please sign in to comment.