#-----------------------------------------------------------------------------# Copyright (c) Anaconda, Inc., and Bokeh Contributors.# All rights reserved.## The full license is in the file LICENSE.txt, distributed with this software.#-----------------------------------------------------------------------------""" Configure the logging system for Bokeh.By default, logging is not configured, to allow users of Bokeh to have fullcontrol over logging policy. However, it is useful to be able to enablelogging arbitrarily during when developing Bokeh. This can be accomplishedby setting the environment variable ``BOKEH_PY_LOG_LEVEL``. Valid values are,in order of increasing severity:- ``debug``- ``info``- ``warn``- ``error``- ``fatal``- ``none``The default logging level is ``none``."""#-----------------------------------------------------------------------------# Boilerplate#-----------------------------------------------------------------------------from__future__importannotationsimportlogging# isort:skiplog=logging.getLogger(__name__)#-----------------------------------------------------------------------------# Imports#-----------------------------------------------------------------------------# Standard library importsimportsysfromtypingimportAny,cast# Bokeh importsfrom..settingsimportsettings#-----------------------------------------------------------------------------# Globals and constants#-----------------------------------------------------------------------------__all__=('basicConfig',)default_handler:logging.Handler|None=None#-----------------------------------------------------------------------------# General API#-----------------------------------------------------------------------------# TODO: needs typings for basicConfig()
[docs]defbasicConfig(**kwargs:Any)->None:""" A logging.basicConfig() wrapper that also undoes the default Bokeh-specific configuration. """ifdefault_handlerisnotNone:bokeh_logger.removeHandler(default_handler)bokeh_logger.propagate=Truelogging.basicConfig(**kwargs)
#-----------------------------------------------------------------------------# Dev API#-----------------------------------------------------------------------------#-----------------------------------------------------------------------------# Private API#-----------------------------------------------------------------------------#-----------------------------------------------------------------------------# Code#-----------------------------------------------------------------------------TRACE=9logging.addLevelName(TRACE,"TRACE")deftrace(self:logging.Logger,message:str,*args:Any,**kws:Any)->None:ifself.isEnabledFor(TRACE):self._log(TRACE,message,args,**kws)cast(Any,logging).Logger.trace=tracecast(Any,logging).TRACE=TRACElevel=settings.py_log_level()bokeh_logger=logging.getLogger('bokeh')root_logger=logging.getLogger()iflevelisnotNone:bokeh_logger.setLevel(level)ifnot(root_logger.handlersorbokeh_logger.handlers):# No handlers configured => at least add a printer to sys.stderr for# Bokeh warnings to be displayeddefault_handler=logging.StreamHandler(sys.stderr)default_handler.setFormatter(logging.Formatter(logging.BASIC_FORMAT))bokeh_logger.addHandler(default_handler)# Avoid printing out twice if the root logger is later configured# by user.bokeh_logger.propagate=False