log file truncation and modification of log messages

This commit is contained in:
2024-09-18 11:49:34 +02:00
committed by Flo Greistorfer
parent 383dd59851
commit 961d79754e
5 changed files with 35 additions and 21 deletions

View File

@@ -212,7 +212,6 @@ def main() -> None:
icons(args)
if args.generate_webmanifest:
logger.info("generating webmanifest")
print("Generating webmanifest...")
webmanifest(args)

View File

@@ -89,11 +89,13 @@ def get_image_info(item: str, folder: str) -> Dict[str, Any]:
Returns:
Dict[str, Any]: A dictionary containing image width, height, and EXIF data.
"""
with Image.open(os.path.join(folder, item)) as img:
logger.info("extracting image information", extra={"file": item})
file = os.path.join(folder, item)
with Image.open(file) as img:
logger.info("extracting image information", extra={"file": file})
exif = img.getexif()
width, height = img.size
if exif:
logger.info("extracting EXIF data", extra={"file": file})
ifd = exif.get_ifd(ExifTags.IFD.Exif)
exifdatas = dict(exif.items()) | ifd
exifdata = {}
@@ -113,6 +115,7 @@ def get_image_info(item: str, folder: str) -> Dict[str, Any]:
content = newtuple
exifdata[tag] = content
if "Orientation" in exifdata and exifdata["Orientation"] in [6, 8]:
logger.info("image is rotated", extra={"file": file})
width, height = height, width
for key in ["PrintImageMatching", "UserComment", "MakerNote"]:
if key in exifdata:
@@ -155,12 +158,15 @@ def process_image(item: str, folder: str, _args: Args, baseurl: str, sizelist: D
thumbnails.append((folder, item, _args.root_directory))
for _raw in raw:
if os.path.exists(os.path.join(folder, extsplit[0] + _raw)):
url = urllib.parse.quote(extsplit[0]) + _raw
file = os.path.join(folder, extsplit[0] + _raw)
if os.path.exists(file):
url = f"{_args.web_root_url}{baseurl}{urllib.parse.quote(extsplit[0])}{_raw}"
if _raw in (".tif", ".tiff"):
image["tiff"] = f"{_args.web_root_url}{baseurl}{url}"
logger.info("tiff file found", extra={"file": file})
image["tiff"] = url
else:
image["raw"] = f"{_args.web_root_url}{baseurl}{url}"
logger.info("raw file found", extra={"file": file, "extension": _raw})
image["raw"] = url
return image

View File

@@ -48,14 +48,14 @@ def log_format(keys):
def rotate_log_file(compress=False):
"""
Renames the existing 'latest.jsonl' file to a timestamped file based on the first log entry's asctime.
Optionally compresses the old log file using gzip.
Truncates the 'latest.jsonl' file after optionally compressing its contents to a timestamped file.
The 'latest.jsonl' file is not deleted or moved, just emptied.
Args:
compress (bool): If True, compress the old log file using gzip.
"""
if os.path.exists(LATEST_LOG_FILE):
with open(LATEST_LOG_FILE, "r", encoding="utf-8") as f:
with open(LATEST_LOG_FILE, "r+", encoding="utf-8") as f:
first_line = f.readline()
try:
first_log = json.loads(first_line)
@@ -67,7 +67,10 @@ def rotate_log_file(compress=False):
safe_timestamp = first_timestamp.replace(":", "-").replace(" ", "_")
old_log_filename = os.path.join(LOG_DIR, f"{safe_timestamp}.jsonl")
os.rename(LATEST_LOG_FILE, old_log_filename)
# Write contents to the new file
with open(old_log_filename, "w", encoding="utf-8") as old_log_file:
f.seek(0) # Go back to the beginning of the file
shutil.copyfileobj(f, old_log_file)
if compress:
with open(old_log_filename, "rb") as f_in:
@@ -75,6 +78,10 @@ def rotate_log_file(compress=False):
shutil.copyfileobj(f_in, f_out)
os.remove(old_log_filename)
# Truncate the original file
f.seek(0)
f.truncate()
def setup_logger(level=logging.INFO):
"""

View File

@@ -178,8 +178,8 @@ def create_icons_from_svg(files: List[str], iconspath: str, _args: Args) -> List
List[Icon]
List of icons created from the SVG file.
"""
logger.info("creating icons for web application", extra={"iconspath": iconspath})
svg = [file for file in files if file.endswith(".svg")][0]
logger.info("creating icons for web application", extra={"iconspath": iconspath, "svg": svg})
icon_list = [
{"src": f"{_args.web_root_url}.static/icons/{svg}", "type": "image/svg+xml", "sizes": "512x512", "purpose": "maskable"},
{"src": f"{_args.web_root_url}.static/icons/{svg}", "type": "image/svg+xml", "sizes": "512x512", "purpose": "any"},
@@ -188,7 +188,7 @@ def create_icons_from_svg(files: List[str], iconspath: str, _args: Args) -> List
tmpimg = BytesIO()
sizes = size.split("x")
iconpath = os.path.join(iconspath, os.path.splitext(svg)[0] + "-" + size + ".png")
logger.info("converting svg to png", extra={"iconpath": iconpath, "size": size})
logger.info("converting svg to png", extra={"svg": svg, "size": size})
cairosvg.svg2png(
url=os.path.join(iconspath, svg),
write_to=tmpimg,
@@ -240,7 +240,7 @@ def create_icons_from_png(iconspath: str, web_root_url: str) -> List[Icon]:
continue
with Image.open(os.path.join(iconspath, icon)) as iconfile:
iconsize = f"{iconfile.size[0]}x{iconfile.size[1]}"
logger.info("using icon", extra={"icon": icon, "size": iconsize})
logger.info("using icon", extra={"iconspath": iconspath, "icon": icon, "size": iconsize})
icon_list.append({"src": f"{web_root_url}.static/icons/{icon}", "sizes": iconsize, "type": "image/png", "purpose": "maskable"})
icon_list.append({"src": f"{web_root_url}.static/icons/{icon}", "sizes": iconsize, "type": "image/png", "purpose": "any"})
return icon_list
@@ -255,6 +255,8 @@ def webmanifest(_args: Args) -> None:
_args : Args
Parsed command-line arguments.
"""
logger.info("generating webmanifest")
iconspath = os.path.join(_args.root_directory, ".static", "icons")
files = os.listdir(iconspath)
icon_list = create_icons_from_svg(files, iconspath, _args) if SVGSUPPORT and any(file.endswith(".svg") for file in files) else create_icons_from_png(iconspath, _args.web_root_url)

BIN
test/example/DSC03508.ARW Executable file

Binary file not shown.