Python Logger API reference
Overview of using Neptune with Python's Logger.
You can useNeptuneHandlerto track logs from the PythonLogger.
NeptuneHandler
A handler that sends the records created by the logger to Neptune.
Logs will be stored as aStringSeriesfield.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| run | Run, optional | None | An existing run reference, as returned byneptune.init_run(). |
| level | int, optional | logging.NOTSET | Log levelof the logs to be captured by the handler. Defaults tologging.NOTSET, which captures all logs that match the logger level. |
| path | str, optional | None | Path to theStringSeriesfield used for captured logs. If None, tracked metadata will be stored under themonitoringnamespace. |
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()
As a best practice, you should save your Neptune API token and project name as environment variables:
export NEPTUNE_API_TOKEN="h0dHBzOi8aHR0cHM6Lkc78ghs74kl0jv...Yh3Kb8"
export NEPTUNE_PROJECT="ml-team/classification"
Alternatively, you can pass the information when using a function that takesapi_tokenandprojectas arguments:
run = neptune.init_run(
api_token="h0dHBzOi8aHR0cHM6Lkc78ghs74kl0jv...Yh3Kb8", # (1)!
project="ml-team/classification", # (2)!
)
- In the bottom-left corner, expand the user menu and select Get my API token .
- You can copy the path from the project details ( → Details & privacy ).
If you haven't registered, you can log anonymously to a public project:
api_token=neptune.ANONYMOUS_API_TOKEN
project="common/quickstarts"
Make sure not to publish sensitive data through your code!
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")
See also
Loggerreference in thePython documentation
This page is originally sourced from the legacy docs.