Skip to content

Commit

Permalink
feat: support ISOLATE_LOG_LABELS
Browse files Browse the repository at this point in the history
  • Loading branch information
efiop committed Aug 21, 2024
1 parent 286f10a commit 8170b6c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/isolate/logger.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
import json
import os


# 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 __init__(self):
self.log_labels = {}
raw = os.getenv("ISOLATE_LOG_LABELS")
if raw:
labels = json.loads(raw)
for key, value in labels.items():
if value.startswith("$"):
expanded = os.getenv(value[1:])
else:
expanded = value
self.log_labels[key] = expanded

def log(self, level, message, source):
record = {
"isolate_source": source.name,
"level": level.name,
"message": message,
**self.log_labels,
}
print(json.dumps(record))

Expand Down
22 changes: 22 additions & 0 deletions tests/test_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import json

from isolate.logger import IsolateLogger


def test_logger(monkeypatch):
labels = {
"foo": "$MYENVVAR1",
"bar": "$MYENVVAR2",
"baz": "baz",
"qux": "qux",
}
monkeypatch.setenv("MYENVVAR1", "myenvvar1")
monkeypatch.setenv("MYENVVAR2", "myenvvar2")
monkeypatch.setenv("ISOLATE_LOG_LABELS", json.dumps(labels))
logger = IsolateLogger()
assert logger.log_labels == {
"foo": "myenvvar1",
"bar": "myenvvar2",
"baz": "baz",
"qux": "qux",
}

0 comments on commit 8170b6c

Please sign in to comment.