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
¶
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
¶
format
class-attribute
instance-attribute
¶
log_dir
class-attribute
instance-attribute
¶
log_file
class-attribute
instance-attribute
¶
use_ascii
class-attribute
instance-attribute
¶
use_ascii: bool = Field(
default=False,
description="Use ASCII-only characters for console output",
)
configure_logging
¶
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.
LogLevel
¶
Available log levels.
AsciiGlyphs
¶
Bases: Glyphs
ASCII-only glyphs for compatibility.
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
|
BoxStyle
¶
Predefined box styles for consistent output.
UnicodeGlyphs
¶
Bases: Glyphs
Unicode glyphs for rich terminal output.
RompyLogger
¶
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
¶
status_box
¶
bullet_list
¶
box
¶
Create a box with optional title (using default formatter).
bullet_list
¶
Create a bulleted list (using default formatter).
status_box
¶
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.
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 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
¶
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 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
¶
format_table_row
¶
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
¶
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
|