Skip to content

Python Logger#

You can use NeptuneHandler to track logs from the Python Logger.

Related

Logger reference in the Python documentation


NeptuneHandler#

A handler that sends the records created by the logger to Neptune.

Logs will be stored as a StringSeries field.

Parameters

Name Type Default       Description
run Run, optional None An existing run reference, as returned by neptune.init_run().
level int, optional logging.NOTSET Log level of the logs to be captured by the handler. Defaults to logging.NOTSET, which captures all logs that match the logger level.
path str, optional None Path to the StringSeries field used for captured logs. If None, tracked metadata will be stored under the monitoring namespace.

Examples

Create a logger:

import logging

logger = logging.getLogger("root_experiment")
logger.setLevel(logging.DEBUG)

Create a Neptune run:

import neptune

run = neptune.init_run()
If Neptune can't find your project name or API token

As a best practice, you should save your Neptune API token and project name as environment variables:

export NEPTUNE_API_TOKEN="h0dHBzOi8aHR0cHM6Lkc78ghs74kl0jvYh3Kb8"
export NEPTUNE_PROJECT="ml-team/classification"

You can, however, also pass them as arguments when initializing Neptune:

run = neptune.init_run(
    api_token="h0dHBzOi8aHR0cHM6Lkc78ghs74kl0jvYh3Kb8",  # your token here
    project="ml-team/classification",  # your full project name here
)
  • API token: In the bottom-left corner, expand the user menu and select Get my API token.
  • Project name: in the top-right menu: Edit project details.

If you haven't registered, you can also log anonymously to a public project (make sure not to publish sensitive data through your code!):

run = neptune.init_run(
    api_token=neptune.ANONYMOUS_API_TOKEN,
    project="common/quickstarts",
)

Create a NeptuneHandler object to capture the logs:

from neptune.integrations.python_logger import NeptuneHandler

npt_handler = NeptuneHandler(run=run)
logger.addHandler(npt_handler)

# Log something to the logger
logger.debug("Starting data preparation")
logger.debug("Data preparation done")