Render Eliot logs as an ASCII tree
—
Essential functions for parsing Eliot message dictionaries into task objects and rendering them as ASCII trees. These functions form the foundation of eliot-tree's functionality and handle the core transformation from flat log messages to hierarchical visualizations.
Converts an iterable of Eliot message dictionaries into parsed task objects suitable for rendering.
def tasks_from_iterable(iterable):
"""
Parse an iterable of Eliot message dictionaries into tasks.
Parameters:
- iterable: Iterable[Dict] - Iterable of serialized Eliot message dictionaries
Returns:
Iterable - Iterable of parsed Eliot tasks, suitable for use with render_tasks
Raises:
EliotParseError - If parsing fails for any message dictionary
"""Usage Example:
from eliottree import tasks_from_iterable
# Eliot message dictionaries from log file or stream
messages = [
{"timestamp": 1425356936.278875, "action_status": "started",
"task_uuid": "f3a32bb3-ea6b-457c-aa99-08a3d0491ab4",
"action_type": "app:request", "task_level": [1]},
{"timestamp": 1425356937.517161, "action_status": "succeeded",
"task_uuid": "f3a32bb3-ea6b-457c-aa99-08a3d0491ab4",
"action_type": "app:request", "task_level": [1]}
]
# Parse into task objects
tasks = tasks_from_iterable(messages)Renders parsed Eliot tasks as formatted ASCII tree output with extensive customization options.
def render_tasks(write, tasks, field_limit=0, ignored_fields=None,
human_readable=False, colorize=None, write_err=None,
format_node=format_node, format_value=None,
utc_timestamps=True, colorize_tree=False, ascii=False,
theme=None):
"""
Render Eliot tasks as an ASCII tree.
Parameters:
- write: Callable[[text_type], None] - Callable used to write the output
- tasks: Iterable - Iterable of parsed Eliot tasks from tasks_from_iterable
- field_limit: int - Length at which to begin truncating, 0 means no truncation
- ignored_fields: Set[text_type] - Set of field names to ignore, defaults to Eliot metadata
- human_readable: bool - Render field values as human-readable
- colorize: bool - Colorize the output (deprecated, use theme parameter)
- write_err: Callable[[text_type], None] - Callable used to write errors
- format_node: Callable - Node formatting function (see format_node)
- format_value: Callable[[Any], text_type] - Callable to format a value
- utc_timestamps: bool - Format timestamps as UTC
- colorize_tree: bool - Colorize the tree structure itself
- ascii: bool - Render the tree as plain ASCII instead of Unicode
- theme: Theme - Theme object to use for rendering colors and styles
Returns:
None - Output is written via the write callback
"""Usage Examples:
import sys
from eliottree import tasks_from_iterable, render_tasks, get_theme
# Basic rendering
tasks = tasks_from_iterable(messages)
render_tasks(sys.stdout.write, tasks)
# With colorization and theme
theme = get_theme(dark_background=True)
render_tasks(sys.stdout.write, tasks, theme=theme, colorize_tree=True)
# With field limiting and human-readable formatting
render_tasks(
sys.stdout.write,
tasks,
field_limit=100, # Truncate long field values
human_readable=True, # Format timestamps and durations
utc_timestamps=True, # Use UTC timezone
ascii=True # Use ASCII characters only
)
# With custom ignored fields
ignored = {"task_uuid", "timestamp", "action_status"}
render_tasks(sys.stdout.write, tasks, ignored_fields=ignored)
# With error handling
def error_handler(error_text):
print(f"ERROR: {error_text}", file=sys.stderr)
render_tasks(
sys.stdout.write,
tasks,
write_err=error_handler,
theme=theme
)import sys
import json
from eliottree import tasks_from_iterable, render_tasks, get_theme
def process_eliot_log(log_file_path):
"""Complete pipeline for processing Eliot log files."""
# Read and parse JSON messages
messages = []
with open(log_file_path, 'r') as f:
for line in f:
messages.append(json.loads(line.strip()))
# Parse into tasks
tasks = tasks_from_iterable(messages)
# Setup theme and rendering
theme = get_theme(dark_background=True)
# Render with full options
render_tasks(
sys.stdout.write,
tasks,
theme=theme,
human_readable=True,
colorize_tree=True,
field_limit=200,
write_err=sys.stderr.write
)
# Usage
process_eliot_log('eliot.log')# From six library for Python 2/3 compatibility
text_type = str # In Python 3, or unicode in Python 2By default, render_tasks ignores standard Eliot metadata fields to focus on application-specific data:
action_status - Action completion status (started/succeeded/failed)action_type - Type identifier for the actiontask_level - Hierarchical level within the task treetask_uuid - Unique identifier for the taskmessage_type - Message type identifierThese can be overridden using the ignored_fields parameter to customize which fields appear in the output.
The rendered output follows a hierarchical tree structure:
task-uuid
└── action_type/level ⇒ status timestamp ⧖ duration
├── field1: value1
├── field2: value2
└── nested_action/level ⇒ status timestamp
└── result: valueInstall with Tessl CLI
npx tessl i tessl/pypi-eliot-tree