Skip to content

Commit

Permalink
GitHub Actions logging/warnings/error support
Browse files Browse the repository at this point in the history
  • Loading branch information
tttapa committed Aug 20, 2023
1 parent 509dc12 commit 2cc30e0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/py_build_cmake/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging

from .common import (
logformat,
util,
Config,
ComponentConfig,
Expand Down Expand Up @@ -133,7 +134,7 @@ def is_verbose_enabled(config_settings: Optional[dict]):
k: v for k, v in config_settings.items() if k in verbose_keys
}
if verbose_opts:
last_val = next(reversed(verbose_opts.values()))
last_val = next(reversed(list(verbose_opts.values())))
return truthy(last_val)
env_verbose = os.environ.get("PY_BUILD_CMAKE_VERBOSE")
if env_verbose is not None:
Expand All @@ -152,7 +153,7 @@ def parse_log_level(loglevel: str) -> int:
log_keys = {"loglevel", "--loglevel"}
log_opts = {k: v for k, v in config_settings.items() if k in log_keys}
if log_opts:
last_val = next(reversed(log_opts.values()))
last_val = next(reversed(list(log_opts.values())))
return parse_log_level(last_val)
env_log = os.environ.get("PY_BUILD_CMAKE_LOGLEVEL")
if env_log is not None:
Expand All @@ -162,7 +163,13 @@ def parse_log_level(loglevel: str) -> int:
def parse_config_settings(self, config_settings: Optional[Dict]):
try:
level = self.get_log_level(config_settings)
logging.basicConfig(level=level)
if "GITHUB_ACTIONS" in os.environ:
formatter = logformat.GitHubActionsFormatter()
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logging.basicConfig(level=level, handlers=[handler])
else:
logging.basicConfig(level=level)
except ValueError as e:
logger.error("Invalid log level specified", exc_info=e)
self.runner.verbose = self.is_verbose_enabled(config_settings)
Expand Down
20 changes: 20 additions & 0 deletions src/py_build_cmake/common/logformat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import logging


class GitHubActionsFormatter(logging.Formatter):
"""Formats warnings etc. for GitHub Actions: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-a-notice-message"""

def __init__(self):
super().__init__(fmt="%(name)s:%(message)s")

def format(self, record: logging.LogRecord):
s = super().format(record)
prefix = {
logging.INFO:
"::notice file=%(pathname)s,line=%(lineno)d::" % record.__dict__,
logging.WARNING:
"::warning file=%(pathname)s,line=%(lineno)d::" % record.__dict__,
logging.ERROR:
"::error file=%(pathname)s,line=%(lineno)d::" % record.__dict__
}.get(record.levelno, record.levelname + ":")
return prefix + s

0 comments on commit 2cc30e0

Please sign in to comment.