18 from typing
import Optional, List
24 from .upload_worker
import LoggerUploadWorker
25 from ...logging
import Log, LogSeverity
26 from ...networking
import networkManager, RequestFailedError
29 def exceptionToString(exception: BaseException) -> str:
30 tb = traceback.format_tb(exception.__traceback__)
31 tb.append(str(exception))
35 def uploadTaskRunLogs(taskRunId: int, logs: List[Log]) -> bool:
38 "model_queue_id": taskRunId,
39 "logs": [log.encode()
for log
in logs]
42 response = networkManager.post(
"model-queue/add-console-log", params)
43 return not response.hasFailed()
44 except RequestFailedError
as ex:
45 logging.getLogger(
"coretexpylib").error(f
">> Failed to upload console logs to Coretex. Reason: {ex}")
46 logging.getLogger(
"coretexpylib").debug(f
">> Failed to upload console logs to Coretex. Reason: {ex}", exc_info = ex)
55 def __init__(self) -> None:
56 self._logger = logging.getLogger(RunLogger.NAME)
57 self._uploadWorker: Optional[LoggerUploadWorker] =
None
59 def attach(self, taskRunId: int) ->
None:
60 if self._uploadWorker
is not None:
61 raise ValueError(
"TaskRun is already attached to logger")
63 self._uploadWorker = LoggerUploadWorker(functools.partial(uploadTaskRunLogs, taskRunId))
64 self._uploadWorker.start()
66 def reset(self) -> None:
67 if self._uploadWorker
is not None:
68 self._uploadWorker.stop()
69 self._uploadWorker.join()
71 self._uploadWorker =
None
73 def flushLogs(self) -> bool:
74 if self._uploadWorker
is None:
77 return self._uploadWorker.uploadLogs()
79 def logProcessOutput(self, line: str, severity: Optional[LogSeverity] =
None) ->
None:
80 if line.strip() ==
"":
83 log, _ = Log.parse(line)
85 if severity
is not None:
86 log.severity = severity
88 self._logger.log(log.severity.getLevel(), log.message)
89 if self._uploadWorker
is not None:
90 self._uploadWorker.add(log)
92 def log(self, severity: LogSeverity, message: str, exception: Optional[BaseException] =
None) ->
None:
93 log = Log(severity, message)
94 self._logger.log(severity.getLevel(), message)
96 if self._uploadWorker
is not None:
97 self._uploadWorker.add(log)
99 if exception
is not None:
100 self.log(severity, exceptionToString(exception))
102 def debug(self, message: str, exception: Optional[BaseException] =
None) ->
None:
103 self.log(LogSeverity.debug, message, exception)
105 def info(self, message: str, exception: Optional[BaseException] =
None) ->
None:
106 self.log(LogSeverity.info, message, exception)
108 def warning(self, message: str, exception: Optional[BaseException] =
None) ->
None:
109 self.log(LogSeverity.warning, message, exception)
111 def error(self, message: str, exception: Optional[BaseException] =
None) ->
None:
112 self.log(LogSeverity.error, message, exception)
114 def critical(self, message: str, exception: Optional[BaseException] =
None) ->
None:
115 self.log(LogSeverity.fatal, message, exception)
121 runLogger = RunLogger()