Skip to content

Programming: Logging

Joshua Newton edited this page Nov 10, 2020 · 4 revisions

Code in command-line scripts (e.g. sct_propseg)

Should use sct.printv, which is a modified version of Python's print to deal with color. Example:

import sct_utils as sct

sct.printv('Running segmentation, type='normal')
sct.printv('Finished! To view results, type: XYZ, type='info')

Code in library's APIs (e.g. spinalcordtoolbox/resampling)

Here we should not use printing, but logging instead. A logger is defined at the beginning of each library's file and later called in the code. Example:

# Start of module
import logging
logger = logging.getLogger(__name__)

# Within code
logger.info("Running segmentation...")
logger.debug(f"Centerline fitting error is: {fitting_error}")

Verbosity and log levels

For each SCT script, the logging level is controlled using -v. However, as of this edit, verbosity for printv/logging is inconsistent between scripts. This is an active issue being discussed in https://github.com/neuropoly/spinalcordtoolbox/issues/2676.

Clone this wiki locally