Skip to content
This repository has been archived by the owner on Feb 19, 2021. It is now read-only.

WIP: Logging configuration #496

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ django-filter = "*"
djangorestframework = "*"
factory-boy = "*"
filemagic = "*"
fuzzywuzzy = {extras = ["speedup"], version = "==0.15.0"}
fuzzywuzzy = {extras = ["speedup"],version = "==0.15.0"}
gunicorn = "*"
inotify-simple = "*"
langdetect = "*"
Expand All @@ -36,6 +36,7 @@ pytest-env = "*"
pytest-xdist = "*"
psycopg2 = "*"
djangoql = "*"
pyyaml = "*"

[dev-packages]
ipython = "*"
45 changes: 31 additions & 14 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Contents
setup
consumption
api
logging
utilities
guesswork
migrating
Expand Down
100 changes: 100 additions & 0 deletions docs/logging.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
.. _logging:

Logging
=======

By default paperless logs to standard output and to the database. Logs can be browsed from the paperless web interface in the ``Log`` section.

Sentry
------

Paperless can be configured to send errors to the `sentry.io`_ error tracking software by setting the ``PAPERLESS_SENTRY_DSN`` value in ``paperless.conf``.

.. _sentry.io: https://sentry.io/welcome/

Custom logger
-------------

Paperless support customizing the logging configuration by using a ``/etc/paperless/logging.yml`` file. This file should be in the yaml format and is passed without modification to the python logging ``dictConfig``. For more information
about the format see:

- `Django logging documentation`_
- `Python logging dictConfig documentation`_

.. _Django logging documentation: https://docs.djangoproject.com/en/2.1/topics/logging/
.. _Python logging dictConfig documentation: https://docs.python.org/3/library/logging.config.html#logging-config-dictschema

Examples
--------

Default
+++++++

.. code-block:: yaml

version: 1
disable_existing_loggers: False
handlers:
consumer:
class: documents.loggers.PaperlessLogger
loggers:
documents:
handlers:
- consumer
level: INFO

Send errors by email
++++++++++++++++++++

.. code-block:: yaml

version: 1
disable_existing_loggers: False
handlers:
consumer:
class: documents.loggers.PaperlessLogger
email:
class: logging.handlers.SMTPHandler
mailhost: localhost
fromaddr: [email protected]
toaddrs:
- [email protected]
subject: oops!
credentials:
- root
- hunter2
loggers:
documents:
propagate: true
handlers:
- consumer
level: INFO
root:
handlers:
- email
level: ERROR

Log debug to a file
+++++++++++++++++++

.. code-block:: yaml

version: 1
disable_existing_loggers: False
handlers:
consumer:
class: documents.loggers.PaperlessLogger
file:
class: logging.handlers.RotatingFileHandler
filename: paperless.log
maxBytes: 100000000
loggers:
documents:
propagate: true
handlers:
- consumer
level: INFO
root:
handlers:
- file
level: DEBUG
2 changes: 1 addition & 1 deletion docs/migrating.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ files, the ``migrate`` step may not update anything. This is totally normal.
Additionally, as new features are added, the ability to control those features
is typically added by way of an environment variable set in ``paperless.conf``.
You may want to take a look at the ``paperless.conf.example`` file to see if
there's anything new in there compared to what you've got int ``/etc``.
there's anything new in there compared to what you've got in ``/etc``.

If you are :ref:`using Docker <setup-installation-docker>` the update process
is similar:
Expand Down
55 changes: 43 additions & 12 deletions src/paperless/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@
"""

import os
import yaml

from dotenv import load_dotenv


# Tap paperless.conf if it's available
if os.path.exists("/etc/paperless.conf"):
load_dotenv("/etc/paperless.conf")
elif os.path.exists("/etc/paperless/paperless.conf"):
load_dotenv("/etc/paperless/paperless.conf")
elif os.path.exists("/usr/local/etc/paperless.conf"):
load_dotenv("/usr/local/etc/paperless.conf")
elif os.path.exists("/usr/local/etc/paperless/paperless.conf"):
load_dotenv("/usr/local/etc/paperless/paperless.conf")


def __get_boolean(key, default="NO"):
Expand Down Expand Up @@ -222,22 +227,48 @@ def __get_boolean(key, default="NO"):

# Logging

LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"consumer": {
"class": "documents.loggers.PaperlessLogger",
}
},
"loggers": {
"documents": {
"handlers": ["consumer"],
"level": os.getenv("PAPERLESS_CONSUMER_LOG_LEVEL", "INFO"),
# Django use the python dictConfig format for logging. For more info See:
# * https://docs.djangoproject.com/en/2.1/topics/logging/
# * https://docs.python.org/3/library/logging.config.html#logging-config-dictschema
#
# Removing the default logging configuration will disable logging into the database
# and the log view from the interface

if os.path.exists("/etc/paperless/logging.yml"):
with open("/etc/paperless/logging.yml", 'r') as config:
LOGGING = yaml.load(config)
elif os.path.exists("/usr/local/etc/paperless/logging.yml"):
with open("/usr/local/etc/paperless/logging.yml", 'r') as config:
LOGGING = yaml.load(config)
else:
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"consumer": {
"class": "documents.loggers.PaperlessLogger",
}
},
"loggers": {
"documents": {
"handlers": ["consumer"],
"level": os.getenv("PAPERLESS_CONSUMER_LOG_LEVEL", "INFO"),
},
},
}

if os.getenv("PAPERLESS_SENTRY_DSN"):
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration

sentry_logging = LoggingIntegration(
level=logging.INFO, # Capture info and above as breadcrumbs
event_level=logging.ERROR # Send errors as events
)
sentry_sdk.init(
dsn=os.getenv("PAPERLESS_SENTRY_DSN"),
integrations=[sentry_logging, DjangoIntegration()]
)

# The default language that tesseract will attempt to use when parsing
# documents. It should be a 3-letter language code consistent with ISO 639.
Expand Down