Skip to content

Commit

Permalink
chore: print logs in json format (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
efiop authored Jun 4, 2024
1 parent e98d620 commit 0e27df9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/isolate/connections/grpc/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from isolate.connections.grpc import agent, definitions
from isolate.connections.grpc.configuration import get_default_options
from isolate.connections.grpc.interface import from_grpc
from isolate.logger import logger
from isolate.logs import LogLevel, LogSource


Expand Down Expand Up @@ -148,5 +149,5 @@ def get_python_cmd(
]

def handle_agent_log(self, line: str, level: LogLevel, source: LogSource) -> None:
print(f"[{source}] [{level}] {line}")
logger.log(level, line, source)
self.log(line, level=level, source=source)
4 changes: 3 additions & 1 deletion src/isolate/connections/grpc/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from isolate.connections.grpc import definitions
from isolate.connections.grpc.configuration import get_default_options
from isolate.connections.grpc.interface import from_grpc
from isolate.logger import logger
from isolate.logs import LogLevel, LogSource


@dataclass
Expand Down Expand Up @@ -149,7 +151,7 @@ def send_object(
definition = serialize_object(serialization_method, result)
except SerializationError:
if stringized_tb:
print(stringized_tb, file=sys.stderr)
logger.log(LogLevel.ERROR, stringized_tb, LogSource.BRIDGE)
raise AbortException(
"Error while serializing the execution result "
f"(object of type {type(result)})."
Expand Down
17 changes: 17 additions & 0 deletions src/isolate/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import json


# NOTE: we probably should've created a proper `logging.getLogger` here,
# but it handling `source` would be not trivial, so we are better off
# just keeping it simple for now.
class IsolateLogger:
def log(self, level, message, source):
record = {
"isolate_source": source.name,
"level": level.name,
"message": message,
}
print(json.dumps(record))


logger = IsolateLogger()

0 comments on commit 0e27df9

Please sign in to comment.