psychopy.logging - control what gets logged

Provides functions for logging error and other messages to one or more files and/or the console, using python’s own logging module. Some warning messages and error messages are generated by PsychoPy itself. The user can generate more using the functions in this module.

There are various levels for logged messages with the following order of importance: ERROR, WARNING, DATA, EXP, INFO and DEBUG.

When setting the level for a particular log target (e.g. LogFile) the user can set the minimum level that is required for messages to enter the log. For example, setting a level of INFO will result in INFO, EXP, DATA, WARNING and ERROR messages to be recorded but not DEBUG messages.

By default, PsychoPy will record messages of WARNING level and above to the console. The user can silence that by setting it to receive only CRITICAL messages, (which PsychoPy doesn’t use) using the commands:

from psychopy import logging
logging.console.setLevel(logging.CRITICAL)
class psychopy.logging.LogFile(f=None, level=30, filemode='a', logger=None, encoding='utf8')[source]

A text stream to receive inputs from the logging system

Create a log file as a target for logged entries of a given level

Parameters:
  • f:

    this could be a string to a path, that will be created if it doesn’t exist. Alternatively this could be a file object, sys.stdout or any object that supports .write() and .flush() methods

  • level:

    The minimum level of importance that a message must have to be logged by this target.

  • filemode: ‘a’, ‘w’

    Append or overwrite existing log file

setLevel(level)[source]

Set a new minimal level for the log file/stream

write(txt)[source]

Write directly to the log file (without using logging functions). Useful to send messages that only this file receives

class psychopy.logging._Logger(format='{t:.4f} \t{levelname} \t{message}')[source]

Maintains a set of log targets (text streams such as files of stdout)

self.targets is a list of dicts {‘stream’:stream, ‘level’:level}

The string-formatted elements {xxxx} can be used, where each xxxx is an attribute of the LogEntry. e.g. t, t_ms, level, levelname, message

addTarget(target)[source]

Add a target, typically a LogFile to the logger

flush()[source]

Process all current messages to each target

log(message, level, t=None, obj=None)[source]

Add the message to the log stack at the appropriate level

If no relevant targets (files or console) exist then the message is simply discarded.

removeTarget(target)[source]

Remove a target, typically a LogFile from the logger

psychopy.logging.addLevel(level, levelName)[source]

Associate ‘levelName’ with ‘level’.

This is used when converting levels to text during message formatting.

psychopy.logging.critical(message)[source]

Send the message to any receiver of logging info (e.g. a LogFile) of level log.CRITICAL or higher

psychopy.logging.data(msg, t=None, obj=None)[source]

Log a message about data collection (e.g. a key press)

usage::

log.data(message)

Sends the message to any receiver of logging info (e.g. a LogFile) of level log.DATA or higher

psychopy.logging.debug(msg, t=None, obj=None)[source]

Log a debugging message (not likely to be wanted once experiment is finalised)

usage::

log.debug(message)

Sends the message to any receiver of logging info (e.g. a LogFile) of level log.DEBUG or higher

psychopy.logging.error(message)[source]

Send the message to any receiver of logging info (e.g. a LogFile) of level log.ERROR or higher

psychopy.logging.exp(msg, t=None, obj=None)[source]

Log a message about the experiment (e.g. a new trial, or end of a stimulus)

usage::

log.exp(message)

Sends the message to any receiver of logging info (e.g. a LogFile) of level log.EXP or higher

psychopy.logging.fatal(msg, t=None, obj=None)

log.critical(message) Send the message to any receiver of logging info (e.g. a LogFile) of level log.CRITICAL or higher

psychopy.logging.flush(logger=<psychopy.logging._Logger object>)[source]

Send current messages in the log to all targets

psychopy.logging.getLevel(level)[source]

Return the textual representation of logging level ‘level’.

If the level is one of the predefined levels (CRITICAL, ERROR, WARNING, INFO, DEBUG) then you get the corresponding string. If you have associated levels with names using addLevelName then the name you have associated with ‘level’ is returned.

If a numeric value corresponding to one of the defined levels is passed in, the corresponding string representation is returned.

Otherwise, the string “Level %s” % level is returned.

psychopy.logging.info(msg, t=None, obj=None)[source]

Log some information - maybe useful, maybe not

usage::

log.info(message)

Sends the message to any receiver of logging info (e.g. a LogFile) of level log.INFO or higher

psychopy.logging.log(msg, level, t=None, obj=None)[source]

Log a message

usage::

log(msg, level, t=t, obj=obj)

Log the msg, at a given level on the root logger

psychopy.logging.setDefaultClock(clock)[source]

Set the default clock to be used to reference all logging times. Must be a psychopy.core.Clock object. Beware that if you reset the clock during the experiment then the resets will be reflected here. That might be useful if you want your logs to be reset on each trial, but probably not.

psychopy.logging.warn(msg, t=None, obj=None)

log.warning(message)

Sends the message to any receiver of logging info (e.g. a LogFile) of level log.WARNING or higher

psychopy.logging.warning(message)[source]

Sends the message to any receiver of logging info (e.g. a LogFile) of level log.WARNING or higher

flush()

psychopy.logging.flush(logger=<psychopy.logging._Logger object>)[source]

Send current messages in the log to all targets

setDefaultClock()

psychopy.logging.setDefaultClock(clock)[source]

Set the default clock to be used to reference all logging times. Must be a psychopy.core.Clock object. Beware that if you reset the clock during the experiment then the resets will be reflected here. That might be useful if you want your logs to be reset on each trial, but probably not.


Back to top