From 1c6e3874671fff5e2b3c9ef7295e882115b0bd27 Mon Sep 17 00:00:00 2001 From: Jennifer Power Date: Fri, 20 Oct 2023 16:21:39 -0400 Subject: [PATCH] feat: configure logger for module and control from actions files (#63) * feat: configure logger for module and control from actions files Adds configuration of trestlebot logger along with compliance-trestle logger Allows verbosity to be set from the actions files Signed-off-by: Jennifer Power * chore: adds formatting fixes Signed-off-by: Jennifer Power * chore: update logging debug statement bot.py --------- Signed-off-by: Jennifer Power --- actions/autosync/action.yml | 4 ++ actions/autosync/auto-sync-entrypoint.sh | 4 ++ actions/rules-transform/action.yml | 4 ++ .../rules-transform-entrypoint.sh | 4 ++ trestlebot/bot.py | 2 +- trestlebot/entrypoints/autosync.py | 5 +- trestlebot/entrypoints/entrypoint_base.py | 6 +- trestlebot/entrypoints/log.py | 59 +++++++++++++++++++ trestlebot/entrypoints/rule_transform.py | 5 +- 9 files changed, 85 insertions(+), 8 deletions(-) create mode 100644 trestlebot/entrypoints/log.py diff --git a/actions/autosync/action.yml b/actions/autosync/action.yml index 3353b3c9..76e19ece 100644 --- a/actions/autosync/action.yml +++ b/actions/autosync/action.yml @@ -70,6 +70,10 @@ inputs: description: Email address used for the commit author. Defaults to the email of whoever triggered this workflow run. required: false default: ${{ github.actor }}@users.noreply.github.com + verbose: + description: Enable verbose logging + required: false + default: "false" outputs: changes: diff --git a/actions/autosync/auto-sync-entrypoint.sh b/actions/autosync/auto-sync-entrypoint.sh index 48103923..bd16db6c 100644 --- a/actions/autosync/auto-sync-entrypoint.sh +++ b/actions/autosync/auto-sync-entrypoint.sh @@ -49,6 +49,10 @@ if [[ ${INPUT_CHECK_ONLY} == true ]]; then command+=" --check-only" fi +if [[ ${INPUT_VERBOSE} == true ]]; then + command+=" --verbose" +fi + # Only set the token value when is a target branch so pull requests can be created if [[ -n ${INPUT_TARGET_BRANCH} ]]; then if [[ -z ${GITHUB_TOKEN} ]]; then diff --git a/actions/rules-transform/action.yml b/actions/rules-transform/action.yml index 9728325b..efbad849 100644 --- a/actions/rules-transform/action.yml +++ b/actions/rules-transform/action.yml @@ -51,6 +51,10 @@ inputs: description: Email address used for the commit author. Defaults to the email of whoever triggered this workflow run. required: false default: ${{ github.actor }}@users.noreply.github.com + verbose: + description: Enable verbose logging + required: false + default: "false" outputs: changes: diff --git a/actions/rules-transform/rules-transform-entrypoint.sh b/actions/rules-transform/rules-transform-entrypoint.sh index 55444170..1a84f274 100644 --- a/actions/rules-transform/rules-transform-entrypoint.sh +++ b/actions/rules-transform/rules-transform-entrypoint.sh @@ -34,6 +34,10 @@ command="trestlebot-rules-transform \ --target-branch=\"${INPUT_TARGET_BRANCH}\" \ --skip-items=\"${INPUT_SKIP_ITEMS}\"" +# Conditionally include flags +if [[ ${INPUT_VERBOSE} == true ]]; then + command+=" --verbose" +fi # Only set the token value when is a target branch so pull requests can be created if [[ -n ${INPUT_TARGET_BRANCH} ]]; then diff --git a/trestlebot/bot.py b/trestlebot/bot.py index c7ef6d28..8d9d8bab 100644 --- a/trestlebot/bot.py +++ b/trestlebot/bot.py @@ -180,7 +180,7 @@ def run( ) # Parse remote url to get repository information for pull request namespace, repo_name = git_provider.parse_repository(remote.url) - logger.debug("Detected namespace {namespace} and {repo_name}") + logger.debug(f"Detected namespace {namespace} and {repo_name}") pr_number = git_provider.create_pull_request( ns=namespace, diff --git a/trestlebot/entrypoints/autosync.py b/trestlebot/entrypoints/autosync.py index bd4c8fba..ee85594d 100644 --- a/trestlebot/entrypoints/autosync.py +++ b/trestlebot/entrypoints/autosync.py @@ -27,10 +27,9 @@ import sys from typing import List -import trestle.common.log as log - from trestlebot import const from trestlebot.entrypoints.entrypoint_base import EntrypointBase, comma_sep_to_list +from trestlebot.entrypoints.log import set_log_level_from_args from trestlebot.tasks.assemble_task import AssembleTask from trestlebot.tasks.authored import types from trestlebot.tasks.base_task import TaskBase @@ -94,7 +93,7 @@ def setup_autosync_arguments(self) -> None: def run(self, args: argparse.Namespace) -> None: """Run the autosync entrypoint.""" - log.set_log_level_from_args(args=args) + set_log_level_from_args(args) pre_tasks: List[TaskBase] = [] diff --git a/trestlebot/entrypoints/entrypoint_base.py b/trestlebot/entrypoints/entrypoint_base.py index f63b8700..771f71da 100644 --- a/trestlebot/entrypoints/entrypoint_base.py +++ b/trestlebot/entrypoints/entrypoint_base.py @@ -52,7 +52,11 @@ def __init__(self, parser: argparse.ArgumentParser) -> None: def setup_common_arguments(self) -> None: """Setup arguments for the entrypoint.""" self.parser.add_argument( - "-v", "--verbose", action="store_true", help="Enable verbose mode" + "-v", + "--verbose", + help="Display verbose output", + action="count", + default=0, ) self.parser.add_argument( "--branch", diff --git a/trestlebot/entrypoints/log.py b/trestlebot/entrypoints/log.py new file mode 100644 index 00000000..e5e11da0 --- /dev/null +++ b/trestlebot/entrypoints/log.py @@ -0,0 +1,59 @@ +#!/usr/bin/python + +# Copyright 2023 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +"""Configure logger for trestlebot and trestle.""" + +import argparse +import logging +import sys + +import trestle.common.log as log + + +_logger = logging.getLogger("trestlebot") + + +def set_log_level_from_args(args: argparse.Namespace) -> None: + """Set the log level from the args for trestle and trestlebot.""" + + # Setup the trestle logger + log.set_log_level_from_args(args=args) + + if args.verbose == 1: + configure_logger(logging.DEBUG) + else: + configure_logger(logging.INFO) + + +def configure_logger(level: int = logging.INFO) -> None: + """Configure the logger.""" + _logger.setLevel(level=level) + + # Create a StreamHandler to send non-error logs to stdout + stdout_handler = logging.StreamHandler(sys.stdout) + stdout_handler.setLevel(logging.DEBUG) + + # Create a StreamHandler to send error logs to stderr + stderr_handler = logging.StreamHandler(sys.stderr) + stderr_handler.setLevel(logging.ERROR) + + # Create a formatter and set it on both handlers + log_formatter = logging.Formatter("%(name)s:%(lineno)d %(levelname)s: %(message)s") + stdout_handler.setFormatter(log_formatter) + stderr_handler.setFormatter(log_formatter) + + _logger.addHandler(stdout_handler) + _logger.addHandler(stderr_handler) diff --git a/trestlebot/entrypoints/rule_transform.py b/trestlebot/entrypoints/rule_transform.py index 767788e2..089c4d05 100644 --- a/trestlebot/entrypoints/rule_transform.py +++ b/trestlebot/entrypoints/rule_transform.py @@ -18,9 +18,8 @@ import logging from typing import List -import trestle.common.log as log - from trestlebot.entrypoints.entrypoint_base import EntrypointBase, comma_sep_to_list +from trestlebot.entrypoints.log import set_log_level_from_args from trestlebot.tasks.base_task import TaskBase from trestlebot.tasks.rule_transform_task import RuleTransformTask from trestlebot.transformers.validations import ValidationHandler, parameter_validation @@ -58,7 +57,7 @@ def setup_rules_transformation_arguments(self) -> None: def run(self, args: argparse.Namespace) -> None: """Run the rule transform entrypoint.""" - log.set_log_level_from_args(args=args) + set_log_level_from_args(args) # Configure the YAML Transformer for the task validation_handler: ValidationHandler = ValidationHandler(parameter_validation)