From aa3d91a338a66b39774acdfdc092be5f1672ff61 Mon Sep 17 00:00:00 2001 From: jonaswinkler <17569239+jonaswinkler@users.noreply.github.com> Date: Mon, 22 Feb 2021 11:38:16 +0100 Subject: [PATCH] associate error messages with documents --- .../management/commands/document_archiver.py | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/documents/management/commands/document_archiver.py b/src/documents/management/commands/document_archiver.py index 6a6056ed7..497e8fe7e 100644 --- a/src/documents/management/commands/document_archiver.py +++ b/src/documents/management/commands/document_archiver.py @@ -4,7 +4,9 @@ import multiprocessing import logging import os import shutil +import sys import uuid +from io import TextIOBase import tqdm from django import db @@ -12,7 +14,6 @@ from django.conf import settings from django.core.management.base import BaseCommand from django.db import transaction from filelock import FileLock -from whoosh.writing import AsyncWriter from documents.models import Document from ... import index @@ -24,11 +25,28 @@ from ...parsers import get_parser_class_for_mime_type logger = logging.getLogger("paperless.management.archiver") +class LoggerWriter(TextIOBase): + + def __init__(self, doc: Document): + self.doc = doc + + def write(self, message): + # if statement reduces the amount of newlines that are + # printed to the logger + if message != '\n': + logger.error( + f"Document {self.doc} (ID: {self.doc.pk}): {message}" + ) + + def handle_document(document_id): document = Document.objects.get(id=document_id) mime_type = document.mime_type + # redirect errors to the log and associate them with the current document + sys.stderr = LoggerWriter(document) + parser_class = get_parser_class_for_mime_type(mime_type) if not parser_class: