8. Log messages

We provide CPL log messages in two different ways: via Python logging and as a list of messages in the cpl.Result object.

For convienience, simple terminal messages and predefined log file output in a style similar to the original CPL messages.

8.1. Python style logging

The preferred and most flexible way to do logging is the use of the logging module of Python. A basic setup (similar to the style used in esorex) is:

import logging

log = logging.getLogger()
log.setLevel(logging.INFO)
ch = logging.FileHandler('cpl_recipe.log')
ch.setLevel(logging.INFO)
fr = logging.Formatter('%(created)s [%(levelname)s] %(name)s: %(message)s',
                       '%H:%M:%S')
ch.setFormatter(fr)
log.addHandler(ch)

The default basic log name for CPL log messages in the recipes is cpl.recipename. The log name can be changed with the logname parameter of cpl.Recipe.__call__() to follow own naming rules, or to separate the output of recipes that are executed in parallel:

res = [ muse_focus(f, logname = 'cpl.muse_focus%02i' % (i+1), threading = True)
        for i, f in enumerate(inputfiles) ]

To the basic log name the function name is appended to allow selective logging of a certain function. The following sample line:

logging.getLogger('cpl.muse_sky.muse_sky_create_skymask').setLevel(logging.DEBUG)

will log the debug messages from muse_sky_create_skymask() additionally to the other messages.

Note

Since the log messages are cached in CPL, they may occur with some delay in the python log module. Also, log messages from different recipes running in parallel may be mixed in their chronological order. The resolution of the log time stamp is one second. The fields logging.LogRecord.args, logging.LogRecord.exc_info and logging.LogRecord.lineno are not set. Also, due to limitations in the CPL logging module, level filtering is done only after the creation of the log entries. This may cause performance problems if extensive debug logging is done and filtered out by logging.Logger.setLevel(). In this case the cpl.Recipe.__call__() parameter loglevel may be used.

See also

cpl.esorex.msg and cpl.esorex.log

EsoRex like convienience logging.

8.2. Log message lists

The cpl.Result object as well as a cpl.CplError have an attribute cpl.Result.log resp. cpl.CplError.log that contains the list of all log messages.

class cpl.logger.LogList

List of log messages.

Accessing this list directly will return the logging.LogRecord instances.

Example:

res = muse_bias(bias_frames)
for logrecord in res.log:
    print '%s: %s' % (entry.funcname, entry.msg)

To get them formatted as string, use the error, warning, info or debug attributes:

res = muse_bias(bias_frames)
for line in res.log.info:
    print line
error

Error messages as list of str

warning

Warnings and error messages as list of str

info

Info, warning and error messages as list of str

debug

Debug, info, warning, and error messages as list of str