Skip to content

Commit 0c34fce

Browse files
committed
Added Job log handler using RichHandler.
1 parent 653fcdf commit 0c34fce

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

tenint/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
from .models.configuration import Configuration, Settings # noqa F401
33
from .connector import Connector # noqa F401
44

5-
__version__ = "1.0.1"
5+
__version__ = "1.0.2"

tenint/connector.py

+22-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
import requests
1818
from pydantic import BaseModel, ValidationError
19+
from rich import print, print_json
1920
from rich.console import Console
21+
from rich.logging import RichHandler
2022
from typer import Exit, Option, Typer
2123
from typer.main import get_command_name
2224

@@ -62,8 +64,9 @@ def __init__(
6264
defaults: Settings | None = None,
6365
credentials: list[type[Credential]] | None = None,
6466
):
67+
self.logfile = Path("job.log").absolute()
68+
self.console = Console(file=self.logfile.open("a", encoding="utf-8"), width=135)
6569
self.app = Typer(add_completion=False)
66-
self.console = Console(width=135, force_interactive=False)
6770
self.settings = settings
6871
self.defaults = (
6972
defaults if defaults else settings.model_construct().model_dump()
@@ -154,7 +157,7 @@ class Config(Configuration):
154157
settings_model: type[Settings] = self.settings
155158
credential_models: list[type[Credential]] = self.credentials
156159

157-
self.console.print_json(
160+
print_json(
158161
Config(defaults=self.defaults).model_dump_json(
159162
include=["settings", "credentials", "defaults"]
160163
),
@@ -165,7 +168,7 @@ def cmd_validate(self):
165168
"""
166169
Validate the connector settings and credentials
167170
"""
168-
self.console.print("Not yet implemented")
171+
print("Not yet implemented")
169172

170173
def cmd_run(
171174
self,
@@ -230,10 +233,23 @@ def cmd_run(
230233
"""
231234
logging.basicConfig(
232235
level=log_level.upper(),
233-
format="%(asctime)s %(name)s(%(filename)s:%(lineno)d): %(message)s",
236+
# format="%(asctime) %(name)s(%(filename)s:%(lineno)d): %(message)s",
237+
format="%(name)s: %(message)s",
234238
datefmt="%Y-%m-%d %H:%M:%S",
239+
handlers=[
240+
# logging.FileHandler("job.log"),
241+
RichHandler(
242+
console=self.console,
243+
show_path=False,
244+
rich_tracebacks=True,
245+
tracebacks_show_locals=True,
246+
omit_repeated_times=False,
247+
),
248+
logging.StreamHandler(),
249+
],
235250
)
236251
self.log_env_vars()
252+
logger.info(f"Logging to {self.logfile}")
237253
status_code = 0
238254
resp = None
239255

@@ -246,8 +262,7 @@ def cmd_run(
246262
logging.error(f"Invalid configuration presented: {e}")
247263
status_code = 2
248264
except Exception as _:
249-
logging.error("Job run failed with an error")
250-
self.console.print_exception(show_locals=log_level == "debug")
265+
logging.exception("Job run failed with error", stack_info=2)
251266
status_code = 1
252267

253268
# Set the Callback payload to the job response if the response is a dictionary
@@ -272,7 +287,7 @@ def cmd_run(
272287
logger.info(f"Called back to {callback_url=} with {job_id=} and {payload=}")
273288
else:
274289
logger.warning("Did not initiate a callback!")
275-
self.console.print(f"callback={payload}")
290+
logger.info(f"callback={payload}")
276291

277292
# Exit the connector with the status code from the runner.
278293
raise Exit(code=status_code)

tests/test_connector.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def test_connector_run(AppSettings, main, caplog):
113113
assert "This is an error test" in caplog.text
114114
assert "Job response format is invalid" in caplog.text
115115
assert "Did not initiate a callback!" in caplog.text
116-
assert "callback={" in result.stdout
116+
assert "callback={" in caplog.text
117117

118118

119119
def test_connector_run_config_fail(AppSettings, main):
@@ -133,7 +133,7 @@ def failmain(config: dict, since: None = None):
133133
with caplog.at_level(logging.INFO):
134134
result = runner.invoke(connector.app, ["run", "-j", '{"is_bool": true}'])
135135
assert result.exit_code == 1
136-
assert "I have failed" in result.stdout
136+
assert "I have failed" in caplog.text
137137

138138

139139
@responses.activate

0 commit comments

Comments
 (0)