Using the Logger

Goal

Set up the logger to record and display messages about program execution.

Overview

Logging is the process of recording messages about what your program is doing — whether it’s running normally, encountering a problem, or handling an error. A good logging setup helps with debugging, diagnostics, and tracking execution.

The qrunch package provides a simple helper function (setup_logger) that takes care of:

  • Displaying messages in the console

  • Optionally saving messages to a file

  • Reducing clutter from other libraries

Basic Usage

To set up logging, add the following at the top of your script:

import qrunch as qc
qc.setup_logger()

This will:

  • Show important messages (INFO level and above) in your terminal;

  • Suppress most logs from other libraries.

Only the Essentials

Add the following at the top of your script to only get the high-level, essential messages:

import qrunch as qc
qc.setup_logger(qc.LOGGER_ESSENTIAL)

This will:

  • Show important messages (ESSENTIAL level and above) in your terminal.

  • Suppress most non-essential logs from other libraries.

Customizing the Logger

You can control:

  • Log level: e.g., DEBUG for detailed output, WARNING for fewer messages

  • Log file: store logs to a file for later review

Example:

from pathlib import Path
import qrunch as qc

log_file = Path("my_log_file.log")

# Enable DEBUG-level logging and save to file
qc.setup_logger(level=qc.LOGGER_DEBUG, log_file=log_file)

This will:

  • Print detailed debug messages to the console;

  • Save all log messages to my_log_file.log.

The available log levels are:

  • qc.LOGGER_CRITICAL: serious errors, system failures;

  • qc.LOGGER_ERROR: errors;

  • qc.LOGGER_WARNING: warnings and errors;

  • qc.LOGGER_ESSENTIAL: warnings, errors, and essential messages (default);

  • qc.LOGGER_INFO: warnings, errors, and informational messages;

  • qc.LOGGER_DEBUG: detailed debug messages.

Tip: for production runs, keep the log level at INFO, ESSENTIAL, or WARNING to avoid excessive output.

Verify

Run a quick test:

import logging
import qrunch as qc

qc.setup_logger(level=qc.LOGGER_DEBUG)
logging.info("Hello from qrunch!")
logging.debug("Debug message for troubleshooting.")

You should see both messages in the console (with DEBUG enabled).

See Also