Skip to content

Commit

Permalink
feat: configure logger for module and control from actions files (#63)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* chore: adds formatting fixes

Signed-off-by: Jennifer Power <[email protected]>

* chore: update logging debug statement bot.py

---------

Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 committed Oct 20, 2023
1 parent 60ecd41 commit 1c6e387
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 8 deletions.
4 changes: 4 additions & 0 deletions actions/autosync/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions actions/autosync/auto-sync-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions actions/rules-transform/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions actions/rules-transform/rules-transform-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion trestlebot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions trestlebot/entrypoints/autosync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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] = []

Expand Down
6 changes: 5 additions & 1 deletion trestlebot/entrypoints/entrypoint_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
59 changes: 59 additions & 0 deletions trestlebot/entrypoints/log.py
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 2 additions & 3 deletions trestlebot/entrypoints/rule_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1c6e387

Please sign in to comment.