Verbosity System and Logging in SageMath#
Howto: Logging#
Using Python’s Logging Module#
Import it:
sage: import logging
sage: logging.basicConfig() # only needed once
Setting the level:
sage: logging.getLogger().setLevel(logging.INFO)
Log something:
sage: logger = logging.getLogger(__name__)
sage: logger.info('Hello. I am talking to you.')
INFO:__main__:Hello. I am talking to you.
If we haven’t set the logging level to logging.INFO
, then the previous
wouldn’t have been shown.
sage: logger.debug('Hello. I am really talking a lot.')
The latter is not shown as the current logging level is only
logging.INFO
and not logging.DEBUG
.
Reset the level:
sage: logging.getLogger().setLevel(logging.WARNING)
Warnings are still shown at this default level (logging.WARNING
):
sage: logger.warning('Hello. I am warning you.')
WARNING:__main__:Hello. I am warning you.
And that’s all.
There are a lot more features, see Logging facility for Python.
Using SageMath’s Verbosity System#
Alternatively, this module provides
verbose()
, set_verbose()
, get_verbose()
which can
be used as follows:
sage: from sage.misc.verbose import verbose, set_verbose, get_verbose
sage: set_verbose(1)
sage: t = verbose("This is SageMath.", level=0)
verbose 0 (<module>) This is SageMath.
sage: t = verbose("This is SageMath.", level=1)
verbose 1 (<module>) This is SageMath.
sage: t = verbose("This is SageMath.", level=2)
Logging Levels of SageMath and Python#
SageMath 
Python 
\(2\) 

\(1\) 

\(0\) 

\(1\) 

\(2\) 

Various#
AUTHORS:
Daniel Krenn (2016)
Functions#
 sage.misc.verbose.get_verbose()#
Return the global Sage verbosity level.
INPUT: int level: an integer between 0 and 2, inclusive.
OUTPUT: changes the state of the verbosity flag.
EXAMPLES:
sage: get_verbose() 0 sage: set_verbose(2) sage: get_verbose() 2 sage: set_verbose(0)
 sage.misc.verbose.get_verbose_files()#
 sage.misc.verbose.set_verbose(level, files='all')#
Set the global Sage verbosity level.
INPUT:
level
– an integer between 0 and 2, inclusive.files
(default: ‘all’): list of files to make verbose, or‘all’ to make ALL files verbose (the default).
OUTPUT: changes the state of the verbosity flag and possibly appends to the list of files that are verbose.
EXAMPLES:
sage: set_verbose(2) sage: verbose("This is Sage.", level=1) # not tested VERBOSE1 (?): This is Sage. sage: verbose("This is Sage.", level=2) # not tested VERBOSE2 (?): This is Sage. sage: verbose("This is Sage.", level=3) # not tested [no output] sage: set_verbose(0)
 sage.misc.verbose.set_verbose_files(file_name)#
 sage.misc.verbose.unset_verbose_files(file_name)#
 sage.misc.verbose.verbose(mesg='', t=0, level=1, caller_name=None)#
Print a message if the current verbosity is at least level.
INPUT:
mesg
 str, a message to printt
 int, optional, if included, will also print cputime(t),  which is the time since time t. Thus t should have been obtained with t=cputime()level
 int, (default: 1) the verbosity level of what we are printingcaller_name
 string (default: None), the name of the calling function; in most cases Python can deduce this, so it need not be provided.
OUTPUT: possibly prints a message to stdout; also returns cputime()
EXAMPLES:
sage: set_verbose(1) sage: t = cputime() sage: t = verbose("This is Sage.", t, level=1, caller_name="william") # not tested VERBOSE1 (william): This is Sage. (time = 0.0) sage: set_verbose(0)