Skip to content

Logging and formatting

Logging configuration and the helpers rompy uses to format its console output.

rompy.logging

Centralized logging and formatting utilities for ROMPY.

This module provides a unified approach to logging and output formatting across the ROMPY codebase. It supports both console and file logging with configurable formatting and verbosity levels.

Basic usage:

from rompy.logging import get_logger, config, BoxStyle

# Configure logging
config.update(level="DEBUG", log_dir="./logs")

# Get a logger
logger = get_logger(__name__)
# Log messages
logger.info("This is an info message")
logger.success("Operation completed successfully")
logger.error("Something went wrong")

# Create a box
logger.box("This is some content inside a box", title="My Box")

# Create a status box
logger.status_box("Processing complete!", BoxStyle.SUCCESS)

# Create a bulleted list
logger.bullet_list(["Item 1", "Item 2", "Item 3"])

LogFormat

Bases: str, Enum

Available log formats.

SIMPLE class-attribute instance-attribute

SIMPLE = 'simple'

STANDARD class-attribute instance-attribute

STANDARD = 'standard'

VERBOSE class-attribute instance-attribute

VERBOSE = 'verbose'

LoggingConfig

Bases: BaseSettings

Centralized logging configuration for ROMPY.

This class provides a singleton instance that can be configured once and accessed throughout the application. It supports both programmatic configuration and environment variables.

Environment variables: ROMPY_LOG_LEVEL: Minimum log level (DEBUG, INFO, WARNING, ERROR, CRITICAL) ROMPY_LOG_FORMAT: Log format (simple, standard, verbose) ROMPY_LOG_DIR: Directory to save log files ROMPY_USE_ASCII: Use ASCII-only output (true/false) ROMPY_LOG_FILE: Name of the log file (default: 'rompy.log')

level class-attribute instance-attribute

level: LogLevel = Field(
    default=LogLevel.INFO,
    description="Default logging level",
)

format class-attribute instance-attribute

format: LogFormat = Field(
    default=LogFormat.VERBOSE,
    description="Log message format",
)

log_dir class-attribute instance-attribute

log_dir: Optional[Path] = Field(
    default=None, description="Directory to save log files"
)

log_file class-attribute instance-attribute

log_file: str = Field(
    default="rompy.log", description="Name of the log file"
)

use_ascii class-attribute instance-attribute

use_ascii: bool = Field(
    default=False,
    description="Use ASCII-only characters for console output",
)

log_file_path property

log_file_path: Optional[Path]

Get the full path to the log file.

configure_logging

configure_logging() -> None

Configure Python logging based on current settings.

This method sets up the logging configuration with our custom RompyLogger and ensures all loggers use the appropriate formatters and handlers.

update

update(**kwargs) -> None

Update configuration and reconfigure logging if needed.

reset classmethod

reset() -> None

Reset the singleton instance (mainly for testing).

LogLevel

Bases: str, Enum

Available log levels.

DEBUG class-attribute instance-attribute

DEBUG = 'DEBUG'

INFO class-attribute instance-attribute

INFO = 'INFO'

WARNING class-attribute instance-attribute

WARNING = 'WARNING'

ERROR class-attribute instance-attribute

ERROR = 'ERROR'

CRITICAL class-attribute instance-attribute

CRITICAL = 'CRITICAL'

AsciiGlyphs

AsciiGlyphs()

Bases: Glyphs

ASCII-only glyphs for compatibility.

TOP_LEFT instance-attribute

TOP_LEFT: str

TOP_RIGHT instance-attribute

TOP_RIGHT: str

BOTTOM_LEFT instance-attribute

BOTTOM_LEFT: str

BOTTOM_RIGHT instance-attribute

BOTTOM_RIGHT: str

HORIZONTAL instance-attribute

HORIZONTAL: str

VERTICAL instance-attribute

VERTICAL: str

LEFT_T instance-attribute

LEFT_T: str

RIGHT_T instance-attribute

RIGHT_T: str

TOP_T instance-attribute

TOP_T: str

BOTTOM_T instance-attribute

BOTTOM_T: str

CROSS instance-attribute

CROSS: str

ARROW instance-attribute

ARROW: str

BULLET instance-attribute

BULLET: str

CHECK instance-attribute

CHECK: str

CROSS_MARK instance-attribute

CROSS_MARK: str

ELLIPSIS instance-attribute

ELLIPSIS: str

BoxFormatter

BoxFormatter(config: Optional[LoggingConfig] = None)

Formatter for creating boxes and other visual elements.

Initialize with optional logging config.

Parameters:

Name Type Description Default
config Optional[LoggingConfig]

Logging configuration. If None, uses the global config.

None

config property writable

config: LoggingConfig

Get the current config.

glyphs property

glyphs: Glyphs

Get the appropriate glyphs based on current config.

box

box(
    content: str,
    title: Optional[str] = None,
    style: BoxStyle = SIMPLE,
) -> str

Create a box with optional title.

Parameters:

Name Type Description Default
content str

The content to put in the box

required
title Optional[str]

Optional title for the box

None
style BoxStyle

Box style to use

SIMPLE

Returns:

Type Description
str

Formatted box as a string

status_box

status_box(message: str, status: BoxStyle) -> str

Create a status box with appropriate styling.

Parameters:

Name Type Description Default
message str

The status message

required
status BoxStyle

Status type (error, warning, success, etc.)

required

Returns:

Type Description
str

Formatted status box

bullet_list

bullet_list(items: List[str], indent: int = 2) -> str

Create a bulleted list.

Parameters:

Name Type Description Default
items List[str]

List items

required
indent int

Number of spaces to indent

2

Returns:

Type Description
str

Formatted bullet list

arrow

arrow(text: str) -> str

Format text with an arrow.

success

success(text: str) -> str

Format success message.

error

error(text: str) -> str

Format error message.

warning

warning(text: str) -> str

Format warning message.

info

info(text: str) -> str

Format info message.

BoxStyle

Bases: str, Enum

Predefined box styles for consistent output.

SIMPLE class-attribute instance-attribute

SIMPLE = 'simple'

ROUNDED class-attribute instance-attribute

ROUNDED = 'rounded'

DOUBLE class-attribute instance-attribute

DOUBLE = 'double'

SUCCESS class-attribute instance-attribute

SUCCESS = 'success'

ERROR class-attribute instance-attribute

ERROR = 'error'

WARNING class-attribute instance-attribute

WARNING = 'warning'

INFO class-attribute instance-attribute

INFO = 'info'

PROCESSING class-attribute instance-attribute

PROCESSING = 'processing'

UnicodeGlyphs

UnicodeGlyphs()

Bases: Glyphs

Unicode glyphs for rich terminal output.

TOP_LEFT instance-attribute

TOP_LEFT: str

TOP_RIGHT instance-attribute

TOP_RIGHT: str

BOTTOM_LEFT instance-attribute

BOTTOM_LEFT: str

BOTTOM_RIGHT instance-attribute

BOTTOM_RIGHT: str

HORIZONTAL instance-attribute

HORIZONTAL: str

VERTICAL instance-attribute

VERTICAL: str

LEFT_T instance-attribute

LEFT_T: str

RIGHT_T instance-attribute

RIGHT_T: str

TOP_T instance-attribute

TOP_T: str

BOTTOM_T instance-attribute

BOTTOM_T: str

CROSS instance-attribute

CROSS: str

ARROW instance-attribute

ARROW: str

BULLET instance-attribute

BULLET: str

CHECK instance-attribute

CHECK: str

CROSS_MARK instance-attribute

CROSS_MARK: str

ELLIPSIS instance-attribute

ELLIPSIS: str

RompyLogger

RompyLogger(name: str, level: int = NOTSET)

Bases: Logger

Enhanced logger with ROMPY-specific functionality.

Initialize the logger.

Parameters:

Name Type Description Default
name str

The name of the logger

required
level int

The log level (defaults to logging.NOTSET)

NOTSET

box_formatter property

box_formatter: BoxFormatter

Get the box formatter for this logger.

Returns:

Name Type Description
BoxFormatter BoxFormatter

The box formatter instance

box

box(
    content: str,
    title: Optional[str] = None,
    style: BoxStyle = SIMPLE,
) -> None

Log a box with the given content and title.

Parameters:

Name Type Description Default
content str

The content to put in the box

required
title Optional[str]

Optional title for the box

None
style BoxStyle

Box style to use

SIMPLE

status_box

status_box(message: str, status: BoxStyle) -> None

Log a status box with the given message and status.

Parameters:

Name Type Description Default
message str

The status message

required
status BoxStyle

Status type (error, warning, success, etc.)

required

bullet_list

bullet_list(items: list[str], indent: int = 2) -> None

Log a bulleted list.

Parameters:

Name Type Description Default
items list[str]

List items to log

required
indent int

Number of spaces to indent

2

success

success(message: str, *args: Any, **kwargs: Any) -> None

Log a success message.

error

error(message: str, *args: Any, **kwargs: Any) -> None

Log an error message.

warning

warning(message: str, *args: Any, **kwargs: Any) -> None

Log a warning message.

info

info(message: str, *args: Any, **kwargs: Any) -> None

Log an info message.

box

box(
    content: str,
    title: Optional[str] = None,
    style: BoxStyle = SIMPLE,
) -> str

Create a box with optional title (using default formatter).

bullet_list

bullet_list(items: List[str], indent: int = 2) -> str

Create a bulleted list (using default formatter).

status_box

status_box(message: str, status: BoxStyle) -> str

Create a status box (using default formatter).

get_logger

get_logger(name: Optional[str] = None) -> RompyLogger

Get a ROMPY logger instance.

Parameters:

Name Type Description Default
name Optional[str]

Logger name. If None, returns the root logger.

None

Returns:

Type Description
RompyLogger

Configured RompyLogger instance

rompy.formatting

Formatting utilities for ROMPY.

This module provides various formatting utilities for creating consistent and visually appealing output in the ROMPY codebase.

logger module-attribute

logger = get_logger(__name__)

logging_config module-attribute

logging_config = LoggingConfig()

DEFAULT_WIDTH module-attribute

DEFAULT_WIDTH = 72 if logging_config.use_ascii else 70

ARROW module-attribute

ARROW = '>' if logging_config.use_ascii else '→'

BULLET module-attribute

BULLET = '*' if logging_config.use_ascii else '•'

TABLE_FORMATS module-attribute

TABLE_FORMATS = {
    "ascii": {
        "top_line": "+{}-+{}-+",
        "header_line": "| {} | {} |",
        "separator": "+{}-+{}-+",
        "data_line": "| {} | {} |",
        "bottom_line": "+{}-+{}-+",
        "h_line": "-" * DEFAULT_WIDTH,
    },
    "unicode": {
        "top_line": "┏{}━┳{}━┓",
        "header_line": "┃ {} ┃ {} ┃",
        "separator": "┠{}━╋{}━┨",
        "data_line": "┃ {} ┃ {} ┃",
        "bottom_line": "┗{}━┻{}━┛",
        "h_line": "━" * DEFAULT_WIDTH,
    },
}

STATUS_BOX_TEMPLATES module-attribute

STATUS_BOX_TEMPLATES = {
    "processing": {
        "title": "PROCESSING",
        "width": DEFAULT_WIDTH,
    },
    "completed": {
        "title": "COMPLETED",
        "width": DEFAULT_WIDTH,
    },
    "error": {"title": "ERROR", "width": DEFAULT_WIDTH},
    "warning": {"title": "WARNING", "width": DEFAULT_WIDTH},
    "info": {
        "title": "INFORMATION",
        "width": DEFAULT_WIDTH,
    },
}

get_ascii_mode

get_ascii_mode() -> bool

Return the current ASCII mode setting.

Returns:

Name Type Description
bool bool

True if ASCII-only mode is enabled, False otherwise

get_simple_logs

get_simple_logs() -> bool

Return the current simple logs setting.

Returns:

Name Type Description
bool bool

True if simple logs mode is enabled, False otherwise

configure_logging

configure_logging(
    verbosity: int = 0, log_dir: Optional[str] = None
) -> None

Configure logging for ROMPY based on verbosity level and environment settings.

This function is maintained for backward compatibility. The new logging system is now configured through the LoggingConfig class in rompy.logging.

Parameters:

Name Type Description Default
verbosity int

Level of verbosity (0=INFO, 1=VERBOSE, 2=DEBUG)

0
log_dir Optional[str]

Optional directory to save log files

None
get_formatted_header_footer(
    title: str = None,
    use_ascii: Optional[bool] = None,
    width: Optional[int] = None,
) -> Tuple[str, str, str]

Create formatted header and footer for output blocks.

Parameters:

Name Type Description Default
title str

The title text to display in the header (optional)

None
use_ascii Optional[bool]

Whether to use ASCII-only characters (defaults to global setting)

None
width Optional[int]

The width of the header/footer in characters (defaults to ASCII-appropriate width)

None

Returns:

Type Description
Tuple[str, str, str]

A tuple containing (header, footer, bullet_char)

get_formatted_box

get_formatted_box(
    title: str = None,
    content: List[str] = None,
    use_ascii: Optional[bool] = None,
    width: Optional[int] = None,
) -> str

Create a formatted box with a title and optional content.

Parameters:

Name Type Description Default
title str

The title text to display in the box

None
content List[str]

Optional list of content lines to display in the box

None
use_ascii Optional[bool]

Whether to use ASCII-only characters (defaults to global setting)

None
width Optional[int]

The width of the box in characters (defaults to ASCII-appropriate width)

None

Returns:

Type Description
str

A string containing the formatted box

log_box

log_box(
    title: str,
    logger: Logger | None = None,
    use_ascii: Optional[bool] = None,
    width: Optional[int] = None,
    add_empty_line: bool = True,
) -> None

Create a formatted box and log each line.

This utility function creates a formatted box and logs each line to the specified logger, handling the common pattern of creating a box and then splitting it for logging.

Parameters:

Name Type Description Default
title str

The title text to display in the box

required
logger Logger | None

The logger to use (if None, imports and uses the root logger)

None
use_ascii Optional[bool]

Whether to use ASCII-only characters (defaults to global setting)

None
width Optional[int]

The width of the box in characters (defaults to ASCII-appropriate width)

None
add_empty_line bool

Whether to add an empty line after the box

True

format_value

format_value(obj: Any) -> Optional[str]

Format specific types of values for display.

This utility function provides special formatting for specific types used throughout ROMPY, such as paths, timestamps, and configuration objects.

Parameters:

Name Type Description Default
obj Any

The object to format

required

Returns:

Type Description
Optional[str]

A formatted string or None to use default formatting

get_table_format

get_table_format(use_ascii: Optional[bool] = None) -> Dict

Get the appropriate table formatting elements based on ASCII mode.

Parameters:

Name Type Description Default
use_ascii Optional[bool]

Whether to use ASCII-only characters (defaults to global setting)

None

Returns:

Type Description
Dict

A dictionary of table formatting elements

format_table_row

format_table_row(
    key: str, value: str, use_ascii: Optional[bool] = None
) -> str

Format a key-value pair as a table row.

Parameters:

Name Type Description Default
key str

The key/label for the row

required
value str

The value to display

required
use_ascii Optional[bool]

Whether to use ASCII-only characters (defaults to global setting)

None

Returns:

Type Description
str

A formatted table row string

log_horizontal_line

log_horizontal_line(
    logger: Logger | None = None,
    use_ascii: Optional[bool] = None,
) -> None

Log a horizontal line for visual separation.

Parameters:

Name Type Description Default
logger Logger | None

The logger to use (if None, imports and uses the root logger)

None
use_ascii Optional[bool]

Whether to use ASCII-only characters (defaults to global setting)

None

get_status_box

get_status_box(
    status_type: str,
    custom_title: Optional[str] = None,
    use_ascii: Optional[bool] = None,
) -> str

Get a pre-configured status box.

Parameters:

Name Type Description Default
status_type str

Type of status box ('processing', 'completed', 'error', 'warning', 'info')

required
custom_title Optional[str]

Optional custom title to override the default

None
use_ascii Optional[bool]

Whether to use ASCII-only characters (defaults to global setting)

None

Returns:

Type Description
str

A formatted box string

log_status

log_status(
    status_type: str,
    custom_title: Optional[str] = None,
    logger: Logger | None = None,
    add_empty_line: bool = True,
) -> None

Log a pre-configured status box.

Parameters:

Name Type Description Default
status_type str

Type of status box ('processing', 'completed', 'error', 'warning', 'info')

required
custom_title Optional[str]

Optional custom title to override the default

None
logger Logger | None

The logger to use (if None, imports and uses the root logger)

None
add_empty_line bool

Whether to add an empty line after the box

True

str_helper

str_helper(
    lines: list,
    name: str,
    obj: Any,
    level: int,
    format_value_func: Callable[[Any], str | None]
    | None = None,
) -> None

Helper method to build a hierarchical string representation.

Parameters:

Name Type Description Default
lines list

List to append formatted string lines

required
name str

Name of the current object/field

required
obj Any

The object to format

required
level int

Current indentation level

required
format_value_func Callable[[Any], str | None] | None

Optional custom formatter function

None