16
16
17
17
import requests
18
18
from pydantic import BaseModel , ValidationError
19
+ from rich import print , print_json
19
20
from rich .console import Console
21
+ from rich .logging import RichHandler
20
22
from typer import Exit , Option , Typer
21
23
from typer .main import get_command_name
22
24
@@ -62,8 +64,9 @@ def __init__(
62
64
defaults : Settings | None = None ,
63
65
credentials : list [type [Credential ]] | None = None ,
64
66
):
67
+ self .logfile = Path ("job.log" ).absolute ()
68
+ self .console = Console (file = self .logfile .open ("a" , encoding = "utf-8" ), width = 135 )
65
69
self .app = Typer (add_completion = False )
66
- self .console = Console (width = 135 , force_interactive = False )
67
70
self .settings = settings
68
71
self .defaults = (
69
72
defaults if defaults else settings .model_construct ().model_dump ()
@@ -154,7 +157,7 @@ class Config(Configuration):
154
157
settings_model : type [Settings ] = self .settings
155
158
credential_models : list [type [Credential ]] = self .credentials
156
159
157
- self . console . print_json (
160
+ print_json (
158
161
Config (defaults = self .defaults ).model_dump_json (
159
162
include = ["settings" , "credentials" , "defaults" ]
160
163
),
@@ -165,7 +168,7 @@ def cmd_validate(self):
165
168
"""
166
169
Validate the connector settings and credentials
167
170
"""
168
- self . console . print ("Not yet implemented" )
171
+ print ("Not yet implemented" )
169
172
170
173
def cmd_run (
171
174
self ,
@@ -230,10 +233,23 @@ def cmd_run(
230
233
"""
231
234
logging .basicConfig (
232
235
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" ,
234
238
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
+ ],
235
250
)
236
251
self .log_env_vars ()
252
+ logger .info (f"Logging to { self .logfile } " )
237
253
status_code = 0
238
254
resp = None
239
255
@@ -246,8 +262,7 @@ def cmd_run(
246
262
logging .error (f"Invalid configuration presented: { e } " )
247
263
status_code = 2
248
264
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 )
251
266
status_code = 1
252
267
253
268
# Set the Callback payload to the job response if the response is a dictionary
@@ -272,7 +287,7 @@ def cmd_run(
272
287
logger .info (f"Called back to { callback_url = } with { job_id = } and { payload = } " )
273
288
else :
274
289
logger .warning ("Did not initiate a callback!" )
275
- self . console . print (f"callback={ payload } " )
290
+ logger . info (f"callback={ payload } " )
276
291
277
292
# Exit the connector with the status code from the runner.
278
293
raise Exit (code = status_code )
0 commit comments