Verbosity System and Logging in SageMath - Utilities

Skip to content
Back to top
View this page
Edit this page

# Verbosity System and Logging in SageMath

## Howto: Logging

### Using Python’s Logging Module

Import it:

Sage

    sage: import logging
    sage: logging.basicConfig()  # only needed once

Python

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

Setting the level:

Sage

    sage: logging.getLogger().setLevel(logging.INFO)

[Python]

    >>> from sage.all import *
    >>> logging.getLogger().setLevel(logging.INFO)

Log something:

Sage

    sage: logger = logging.getLogger(__name__)
    sage: logger.info('Hello. I am talking to you.')
    INFO:__main__:Hello. I am talking to you.

Python

    >>> 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

    sage: logger.debug('Hello. I am really talking a lot.')

[Python]

    >>> 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

    sage: logging.getLogger().setLevel(logging.WARNING)

Python

    >>> from sage.all import *
    >>> logging.getLogger().setLevel(logging.WARNING)

Warnings are still shown at this default level (`logging.WARNING`):

Sage

    sage: logger.warning('Hello. I am warning you.')
    WARNING:__main__:Hello. I am warning you.

[Python]

    >>> 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

    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)

Python

    >>> 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()

  Return the global Sage verbosity level.

  EXAMPLES:

  Sage

      sage: get_verbose()
      0
      sage: set_verbose(2)
      sage: get_verbose()
      2
      sage: set_verbose(0)

  Python

      >>> from sage.all import *
      >>> get_verbose()
      0
      >>> set_verbose(Integer(2))
      >>> get_verbose()
      2
      >>> set_verbose(Integer(0))

sage.misc.verbose.get_verbose_files()

sage.misc.verbose.set_verbose(level, files='all')

  Set the global Sage verbosity level.

  INPUT:

  - `level` – 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

      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)

  Python

      >>> 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)

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` – string; a message to print

  - `t` – integer (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` – integer (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

      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)

  Python

      >>> 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))