Logging

Most C-PAC logging is handled by the built-in Python logging library. When logging, take care to choose an appropriate getLogger function or method. While CPAC.utils.monitoring.custom_logging.getLogger and nipype.utils.logger.Logging.getLogger each fall back on logging.getLogger,

each have their own intended use cases.

Loggers are singletons that are never freed during a script execution, and so creating lots of loggers will use up memory which can’t then be freed.

Logging Cookbook: Creating a lot of loggers

CPAC.utils.monitoring.custom_logging.getLogger will look for a CPAC.utils.monitoring.custom_logging.MockLogger before falling back on logging.getLogger.

A CPAC.utils.monitoring.custom_logging.MockLogger can be used in place of a logging.Logger and deleted from memory when no longer needed. For an example, see how missing_log is created and deleted in CPAC.pipeline.check_outputs.check_outputs.

nipype.utils.logger.Logging.getLogger will only consider loggers that are part of the class instance calling the method.

CPAC.utils.monitoring.custom_logging.getLogger(name)[source]

Function to get a mock logger if one exists, falling back on real loggers.

Parameters:

name (str) –

Returns:

logger

Return type:

CPAC.utils.monitoring.custom_logging.MockLogger or logging.Logger

class CPAC.utils.monitoring.custom_logging.MockLogger(name, filename, level, log_dir)[source]

Mock logging.Logger to provide the same API without keeping the logger in memory.

__init__(name, filename, level, log_dir)[source]
critical(*items, exc_info=False)

Log a message if logging level >= critical. See Logging Levels for a list of levels.

debug(*items, exc_info=False)

Log a message if logging level >= debug. See Logging Levels for a list of levels.

delete()[source]

Delete the mock logger from memory.

error(*items, exc_info=False)

Log a message if logging level >= error. See Logging Levels for a list of levels.

exception(msg, *args, exc_info=True, **kwargs)[source]

Log a message with severity ‘ERROR’ on the root logger, with exception information. If the logger has no handlers, basicConfig() is called to add a console handler with a pre-defined format.

info(*items, exc_info=False)

Log a message if logging level >= info. See Logging Levels for a list of levels.

warning(*items, exc_info=False)

Log a message if logging level >= warning. See Logging Levels for a list of levels.

Logging.getLogger(name)[source]
Logging.__init__(config)[source]
logging.getLogger(name=None)[source]

Return a logger with the specified name, creating it if necessary.

If no name is specified, return the root logger.

Logging subprocess.check_outputs

CPAC.utils.monitoring.custom_logging.log_subprocess(cmd, *args, raise_error=True, **kwargs)[source]

Pass STDERR and STDOUT from subprocess to interface’s logger. This function is nearly a drop-in replacement for subprocess.check_output.

Caveat: if you’re assigning to a variable (like

>>> output = subprocess.check_output(cmd)  

), the new function also returns the command’s exit code, so you can just assign that to a throwaway variable if you don’t want it

>>> output, _ = log_subprocess(cmd)  

or subscript the command like

>>> output = log_subprocess(cmd)[0]  

. If you’re not assigning to a variable, it doesn’t matter and just

>>> log_subprocess(cmd)  

should work just like

>>> subprocess.check_output(cmd)  
Parameters:
  • cmd (str) – command to run with subprocess.check_output

  • raise_error (boolean) – raise any exception after logging

  • args (any) – pass-through arguments for subprocess.check_output

  • kwargs (any) – pass-through arguments for subprocess.check_output

Returns:

  • output (str)

  • exit_code (int)