Setting logging handlers in Jupyterhub

Want to send application logs to a rolling file - file handler – instead of stdout and stderr. Not sure how to configure Jupyterhub to use a file handler of my choice (e.g., rolling file appender).

Spent considerable time through the docs and code.

All help regarding this matter is appreciated.

You can add logging handlers to the default logger in jupyterhub_config.py:

import logging

import traitlets.log

# create your handler
h = logging.handlers.RotatingFileHandler("jupyterhub.log", backupCount=2, maxBytes=1024 * 1024)
formatter = logging.Formatter(
    "[%(levelname)1.1s %(asctime)s %(name)s %(module)s:%(lineno)d] %(message)s"
)
h.setFormatter(formatter)

# get the logger
root_log = traitlets.log.get_logger()
# connect your handler to the logger
root_log.addHandler(h)

However, there aren’t a lot of docs for doing this because I don’t really recommend doing it this way. Using a Python logging handler means you won’t get all the logs. You will only get actual log statements from the application you are configuring, .i.e. only hub logs, no proxy logs, no single-user server logs.

I think it’s generally preferable to use whatever service-runner (e.g. supervisor, systemd, docker) you use to launch jupyterhub to capture stdout/stderr and handle log rotation at that level. Then you get much more robust capture.

1 Like