Skip to content

Instantly share code, notes, and snippets.

@Sorebit
Created May 18, 2023 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sorebit/b883b47fdf34936414ab7cf3edf8b1d5 to your computer and use it in GitHub Desktop.
Save Sorebit/b883b47fdf34936414ab7cf3edf8b1d5 to your computer and use it in GitHub Desktop.
Clear docker log files of given container of any invalid lines
"""
Usage: python3 clear_logs.py {{ full container ID }}
Clears the log file of any lines which are not valid JSON.
No error handling whatsoever. Use at own risk.
"""
import json
import pathlib
import shutil
import sys
template = '/var/lib/docker/containers/{}/{}-json.log'
out_dir = pathlib.Path('/tmp/cleared')
out_template = '{}-json.log'
container_id = sys.argv[1]
log_path = pathlib.Path(template.format(container_id, container_id))
out_path = pathlib.Path(out_dir / out_template.format(container_id))
out_dir.mkdir(exist_ok=True)
print(f'Attempting to clear {log_path}')
print(f'Output path: {out_path}')
skipped = 0
with open(log_path, 'r') as in_f, open(out_path, 'w') as out_f:
for lno, line in enumerate(in_f.readlines()):
try:
parsed = json.loads(line)
out_f.write(line)
except:
skipped += 1
print('Skipping line no', lno, repr(line))
if skipped > 0:
print('Copying cleared log file to original location')
shutil.copy(out_path, log_path)
print(f'Done. Skipped ({skipped}) lines in total.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment