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

Latest commit

 

History

History
53 lines (36 loc) · 1.65 KB

logging.rst

File metadata and controls

53 lines (36 loc) · 1.65 KB
.. currentmodule:: pystan

Logging

PyStan uses logging-module from the Python Standard library to output messages for the user. By default, messages are sent to sys.stdout. For more information and usage see logging-module and logging-cookbook

Advanced users

To add other logger handlers or to redirect all messages from PyStan, the user needs to setup output handlers manually. If the setup is done before importing PyStan, PyStan won't add automatically logging.StreamHandler to logging handlers. Otherwise, PyStan adds logging.StreamHandler and other handlers coexist with the default handler.

Adding FileHandler

To redirect all messages only to a file.

import logging
logger = logging.getLogger("pystan")

# add root logger (logger Level always Warning)
# not needed if PyStan already imported
logger.addHandler(logging.NullHandler())

logger_path = "pystan.log"
fh = logging.FileHandler(logger_path, encoding="utf-8")
fh.setLevel(logging.INFO)
# optional step
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)

import pystan

To use both (default, file) logging options import pystan before the setup. In this case PyStan adds root handler, which means that user can skip the root handler step.

import pystan

import logging
logger = logging.getLogger("pystan")
...