Handle log rotation errors with corrupted files
This commit is contained in:
@@ -89,18 +89,24 @@ class SessionFileHandler(StreamHandler):
|
|||||||
|
|
||||||
# If uncompressed, compress
|
# If uncompressed, compress
|
||||||
if not path.name.endswith(".xz"):
|
if not path.name.endswith(".xz"):
|
||||||
|
try:
|
||||||
|
with open(path, "r", encoding="utf-8") as original_file:
|
||||||
|
original_data = original_file.read()
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
# If the file is corrupted, throw it away
|
||||||
|
path.unlink()
|
||||||
|
return
|
||||||
|
|
||||||
|
# Compress the file
|
||||||
compressed_path = path.with_suffix(path.suffix + ".xz")
|
compressed_path = path.with_suffix(path.suffix + ".xz")
|
||||||
with (
|
with lzma.open(
|
||||||
lzma.open(
|
compressed_path,
|
||||||
compressed_path,
|
"wt",
|
||||||
"wt",
|
format=FORMAT_XZ,
|
||||||
format=FORMAT_XZ,
|
preset=PRESET_DEFAULT,
|
||||||
preset=PRESET_DEFAULT,
|
encoding="utf-8",
|
||||||
encoding="utf-8",
|
) as lzma_file:
|
||||||
) as lzma_file,
|
lzma_file.write(original_data)
|
||||||
open(path, "r", encoding="utf-8") as original_file,
|
|
||||||
):
|
|
||||||
lzma_file.write(original_file.read())
|
|
||||||
path.unlink()
|
path.unlink()
|
||||||
path = compressed_path
|
path = compressed_path
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user