Skip to content

Commit 78b8c10

Browse files
authored
dev: Add Line Breaks to worker logs (#407)
* mostly there, need to figure out a way to get array list items on single line * update list logging to concat if multiple items * fix uts * lint
1 parent a89553d commit 78b8c10

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

helpers/logging_config.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
from copy import deepcopy
23

34
from celery._state import get_current_task
@@ -17,6 +18,19 @@ def add_fields(self, log_record, record, message_dict) -> None:
1718
log_record["task_name"] = "???"
1819
log_record["task_id"] = "???"
1920

21+
def format_json_on_new_lines(self, json_str):
22+
# Parse the input JSON string
23+
data = json.loads(json_str)
24+
25+
for key, value in data.items():
26+
if isinstance(value, list) and len(value) > 10:
27+
# If more than 10 elements in a list, concat to single line
28+
data[key] = ", ".join(map(str, value))
29+
30+
# Convert the parsed JSON data back to a formatted JSON string
31+
formatted_json = json.dumps(data, indent=4)
32+
return formatted_json
33+
2034

2135
class CustomLocalJsonFormatter(BaseLogger):
2236
def jsonify_log_record(self, log_record) -> str:
@@ -25,9 +39,10 @@ def jsonify_log_record(self, log_record) -> str:
2539
message = log_record.pop("message")
2640
exc_info = log_record.pop("exc_info", "")
2741
content = super().jsonify_log_record(log_record)
42+
formatted = super().format_json_on_new_lines(content) if content else None
2843
if exc_info:
29-
return f"{levelname}: {message} --- {content}\n{exc_info}"
30-
return f"{levelname}: {message} --- {content}"
44+
return f"{levelname}: {message} \n {formatted}\n{exc_info}"
45+
return f"{levelname}: {message} \n {formatted}"
3146

3247

3348
class CustomDatadogJsonFormatter(BaseLogger):

helpers/tests/unit/test_logging_config.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def test_local_formatter(self):
1313
log_record = {"levelname": "weird_level", "message": "This is a message"}
1414
cljf = CustomLocalJsonFormatter()
1515
res = cljf.jsonify_log_record(log_record)
16-
assert "weird_level: This is a message --- {}" == res
16+
assert "weird_level: This is a message \n {}" == res
1717

1818
def test_local_formatter_with_exc_info(self):
1919
log_record = {
@@ -23,7 +23,7 @@ def test_local_formatter_with_exc_info(self):
2323
}
2424
cljf = CustomLocalJsonFormatter()
2525
res = cljf.jsonify_log_record(log_record)
26-
assert "weird_level: This is a message --- {}\nLine\nWith\nbreaks" == res
26+
assert "weird_level: This is a message \n {}\nLine\nWith\nbreaks" == res
2727

2828
def test_get_logging_config_dict(self, mocker):
2929
get_current_env = mocker.patch("helpers.logging_config.get_current_env")

0 commit comments

Comments
 (0)