Skip to content

Commit

Permalink
chore: updates logger to ensure trestle and bot logs are standardized
Browse files Browse the repository at this point in the history
Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 committed Jul 10, 2023
1 parent 3cde7d3 commit d9be5c3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
6 changes: 2 additions & 4 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ else
git config --global --add safe.directory "$INPUT_REPOSITORY"
fi

exec 3>&1

trap exec 3>&- EXIT

# Initialize the command variable
command="python3.8 -m trestlebot \
Expand Down Expand Up @@ -60,7 +57,8 @@ else
command+=" --with-token <<<\"${GITHUB_TOKEN}\""
fi

output=$(eval "$command" 2>&1 > >(tee /dev/fd/3))
exec 3>&1
output=$(eval "$command" > >(tee /dev/fd/3) 2>&1)

commit=$(echo "$output" | grep "Commit Hash:" | sed 's/.*: //')

Expand Down
13 changes: 7 additions & 6 deletions trestlebot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from trestlebot.tasks.base_task import TaskBase, TaskException


logger = logging.getLogger(__name__)
logger = logging.getLogger("trestle")


class RepoException(Exception):
Expand Down Expand Up @@ -161,9 +161,12 @@ def run(
# Only create a pull request if a GitProvider is configured and
# a target branch is set.
if git_provider is not None and target_branch:
logger.info("Git provider detected, submitting pull request")
logger.info(
f"Git provider detected, submitting pull request to {target_branch}"
)
# 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}")

git_provider.create_pull_request(
ns=namespace,
Expand All @@ -177,11 +180,9 @@ def run(
return commit_sha

except GitCommandError as e:
raise RepoException(f"Git push to {branch} failed: {e}") from e
raise RepoException(f"Git push to {branch} failed: {e}")
except GitProviderException as e:
raise RepoException(
f"Git pull request to {target_branch} failed: {e}"
) from e
raise RepoException(f"Git pull request to {target_branch} failed: {e}")
else:
logger.info("Nothing to commit")
return commit_sha
Expand Down
13 changes: 10 additions & 3 deletions trestlebot/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from trestlebot.tasks.regenerate_task import RegenerateTask


logger = logging.getLogger("trestlebot")
logger = logging.getLogger("trestle")


def _parse_cli_arguments() -> argparse.Namespace:
Expand Down Expand Up @@ -148,23 +148,30 @@ def _parse_cli_arguments() -> argparse.Namespace:
help="Read token from standard input for authenticated requests with \
Git provider (e.g. create pull requests)",
)
parser.add_argument(
"--verbose",
required=False,
action="store_true",
help="Run in verbose mode",
)
return parser.parse_args()


def handle_exception(
exception: Exception, msg: str = "Exception occurred during execution"
) -> int:
"""Log the exception and return the exit code"""
logger.error(msg + f": {exception}")
logger.error(msg + f": {exception}", exc_info=True)

return 1


def run() -> None:
"""Trestle Bot entry point function."""
log.set_global_logging_levels()

args = _parse_cli_arguments()
log.set_log_level_from_args(args=args)

pre_tasks: List[TaskBase] = []
git_provider: Optional[GitProvider] = None

Expand Down
4 changes: 2 additions & 2 deletions trestlebot/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

# Singleton logger instance
# All other CLI sub module will inherit settings of this logger as long as
# sub-module instantiates a logger with a prefix 'trestlebot' or __name__
_logger = logging.getLogger("trestlebot")
# sub-module instantiates a logger with a prefix 'trestle' or __name__
_logger = logging.getLogger("trestle")


class SpecificLevelFilter(logging.Filter):
Expand Down

0 comments on commit d9be5c3

Please sign in to comment.