Skip to content

Commit

Permalink
Merge pull request #1519 from camptocamp/better-logs
Browse files Browse the repository at this point in the history
Better returned logs
  • Loading branch information
sbrunner authored Mar 11, 2022
2 parents 92d2234 + 5099cc9 commit 11b0b78
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions tilecloud_chain/views/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


import json
import logging
import os
import re
Expand Down Expand Up @@ -169,11 +170,11 @@ def run(self) -> pyramid.response.Response:
)

return {
"stdout": _limit_length(
"stdout": _format_output(
completed_process.stdout.decode(),
int(os.environ.get("TILECLOUD_CHAIN_MAX_OUTPUT_LENGTH", 1000)),
),
"stderr": _limit_length(
"stderr": _format_output(
completed_process.stderr.decode(),
int(os.environ.get("TILECLOUD_CHAIN_MAX_OUTPUT_LENGTH", 1000)),
),
Expand Down Expand Up @@ -204,7 +205,45 @@ def admin_test(self) -> Dict[str, Any]:
}


def _limit_length(string: str, max_length: int = 1000) -> str:
def _format_output(string: str, max_length: int = 1000) -> str:
result = ""
for line in string.splitlines():
if len(string) > max_length:
break
if line.startswith("{"):
try:
parsed = json.loads(line)
if "source_facility" in parsed:
if not parsed.startswith("tilecloud"):
continue

if result:
result += "\n"

if (
"level_name" in parsed
and "source_facility" in parsed
and "line" in parsed
and "msg" in parsed
):
if parsed.startswith("tilecloud"):
result += (
f"[{parsed['level_name']}] {parsed['source_facility']}:{parsed['line']} "
f"{parsed['msg']}"
)
elif "msg" in parsed:
result += parsed["msg"]
else:
result += line
except json.decoder.JSONDecodeError:
if result:
result += "\n"
result += line
else:
if result:
result += "\n"
result += line

if len(string) > max_length:
return string[: max_length - 3] + "\n..."
return string

0 comments on commit 11b0b78

Please sign in to comment.