Skip to content

Commit

Permalink
Add infrahub.tasks as built-in logger to InfrahubGenerator class (#…
Browse files Browse the repository at this point in the history
…274)

* Test building in logger to InfrahubGenerator class to allow logs to be easily propagated via CTL and CI pipeline logs.

* Remove unused logging import.

* Add changelog to describe change.
  • Loading branch information
FragmentedPacket authored and dgarros committed Feb 23, 2025
1 parent d49b11c commit 41912d5
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/+add-logger-to-generator-class.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added logger to `InfrahubGenerator` class to allow users use built-in logging (`self.logger`) to show logging within Infrahub CI pipeline.
6 changes: 3 additions & 3 deletions infrahub_sdk/ctl/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from ..ctl import config
from ..ctl.client import initialize_client
from ..ctl.repository import get_repository_config
from ..ctl.utils import execute_graphql_query, parse_cli_vars
from ..ctl.utils import execute_graphql_query, init_logging, parse_cli_vars
from ..exceptions import ModuleImportError
from ..node import InfrahubNode

Expand All @@ -20,11 +20,12 @@
async def run(
generator_name: str,
path: str, # noqa: ARG001
debug: bool, # noqa: ARG001
debug: bool,
list_available: bool,
branch: str | None = None,
variables: list[str] | None = None,
) -> None:
init_logging(debug=debug)
repository_config = get_repository_config(Path(config.INFRAHUB_REPO_CONFIG_FILE))

if list_available or not generator_name:
Expand All @@ -34,7 +35,6 @@ async def run(
generator_config = repository_config.get_generator_definition(name=generator_name)

console = Console()

relative_path = str(generator_config.file_path.parent) if generator_config.file_path.parent != Path() else None

try:
Expand Down
3 changes: 3 additions & 0 deletions infrahub_sdk/generator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import logging
import os
from abc import abstractmethod
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -27,6 +28,7 @@ def __init__(
generator_instance: str = "",
params: dict | None = None,
convert_query_response: bool = False,
logger: logging.Logger | None = None,
) -> None:
self.query = query
self.branch = branch
Expand All @@ -41,6 +43,7 @@ def __init__(
self._related_nodes: list[InfrahubNode] = []
self.infrahub_node = infrahub_node
self.convert_query_response = convert_query_response
self.logger = logger if logger else logging.getLogger("infrahub.tasks")

@property
def store(self) -> NodeStore:
Expand Down

0 comments on commit 41912d5

Please sign in to comment.