Verbosity System and Logging in SageMath#

Howto: Logging#

Using Python’s Logging Module#

Import it:

sage: import logging
sage: logging.basicConfig()  # only needed once
>>> from sage.all import *
>>> import logging
>>> logging.basicConfig()  # only needed once

Setting the level:

sage: logging.getLogger().setLevel(logging.INFO)
>>> from sage.all import *
>>> 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.
>>> from sage.all import *
>>> logger = logging.getLogger(__name__)
>>> 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.')
>>> from sage.all import *
>>> 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)
>>> from sage.all import *
>>> 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.
>>> from sage.all import *
>>> 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)
>>> from sage.all import *
>>> from sage.misc.verbose import verbose, set_verbose, get_verbose
>>> set_verbose(Integer(1))
>>> t = verbose("This is SageMath.", level=Integer(0))
verbose 0 (<module>) This is SageMath.
>>> t = verbose("This is SageMath.", level=Integer(1))
verbose 1 (<module>) This is SageMath.
>>> t = verbose("This is SageMath.", level=Integer(2))

Logging Levels of SageMath and Python#

SageMath

Python

\(-2\)

logging.CRITICAL

\(-1\)

logging.ERROR

\(0\)

logging.WARNING

\(1\)

logging.INFO

\(2\)

logging.DEBUG

Various#

AUTHORS:

  • Daniel Krenn (2016)

Functions#

sage.misc.verbose.get_verbose()[source]#

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)
>>> from sage.all import *
>>> get_verbose()
0
>>> set_verbose(Integer(2))
>>> get_verbose()
2
>>> set_verbose(Integer(0))
sage.misc.verbose.get_verbose_files()[source]#
sage.misc.verbose.set_verbose(level, files='all')[source]#

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)
>>> from sage.all import *
>>> set_verbose(Integer(2))
>>> verbose("This is Sage.", level=Integer(1))  # not tested
VERBOSE1 (?): This is Sage.
>>> verbose("This is Sage.", level=Integer(2))  # not tested
VERBOSE2 (?): This is Sage.
>>> verbose("This is Sage.", level=Integer(3))  # not tested
[no output]
>>> set_verbose(Integer(0))
sage.misc.verbose.set_verbose_files(file_name)[source]#
sage.misc.verbose.unset_verbose_files(file_name)[source]#
sage.misc.verbose.verbose(mesg='', t=0, level=1, caller_name=None)[source]#

Print a message if the current verbosity is at least level.

INPUT:

  • mesg – str, a message to print

  • t – 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 printing

  • caller_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)
>>> from sage.all import *
>>> set_verbose(Integer(1))
>>> t = cputime()
>>> t = verbose("This is Sage.", t, level=Integer(1), caller_name="william")       # not tested
VERBOSE1 (william): This is Sage. (time = 0.0)
>>> set_verbose(Integer(0))