Send the last 1MB of error log as a file.

This commit is contained in:
Ivaylo Novakov 2020-08-26 11:32:58 +03:00
parent 3b0c597516
commit 2126115df4
No known key found for this signature in database
GPG Key ID: 06B9354AB08BE9C6
1 changed files with 8 additions and 1 deletions

View File

@ -77,13 +77,20 @@ async def check_docker_logs():
std_out, std_err = proc.communicate() std_out, std_err = proc.communicate()
if len(std_err) > 0: if len(std_err) > 0:
# Trim the error log to under 1MB.
one_mb = 1024*1024
if len(std_err) > one_mb:
pos = std_err.find("\n", -one_mb)
std_err = std_err[pos+1:]
upload_name = "{}-{}-{}-{}-{}:{}:{}_err.log".format(container_name, time.year, time.month, time.day, time.hour, time.minute, time.second)
await send_msg(client, "Error(s) found in log!", file=discord.File(io.BytesIO(std_err.encode()), filename=upload_name), force_notify=True)
# Send at most 1900 characters of logs, rounded down to the nearest new line. # Send at most 1900 characters of logs, rounded down to the nearest new line.
# This is a limitation in the size of Discord messages - they can be at most # This is a limitation in the size of Discord messages - they can be at most
# 2000 characters long (and we send some extra characters before the error log). # 2000 characters long (and we send some extra characters before the error log).
if len(std_err) > 1900: if len(std_err) > 1900:
pos = std_err.find("\n", -1900) pos = std_err.find("\n", -1900)
std_err = std_err[pos+1:] std_err = std_err[pos+1:]
await send_msg(client, "Error(s) found in log:\n{}".format(std_err), force_notify=True) await send_msg(client, "Error(s) preview:\n{}".format(std_err), force_notify=True)
return return
# If there are any critical errors. upload the whole log file. # If there are any critical errors. upload the whole log file.