Skip to content

[LOGS] Add new handlers for pylog #5

@ShravanTata

Description

@ShravanTata

The feature, motivation and pitch

Currently, pylog supports logging to the terminal and the file system. While working on the farms-app project, I encountered a use case where it would be beneficial to have an additional handler type that streams log messages to a list or similar structure.

Use case:

During GUI development (e.g., using ImGui), it’s useful to collect log messages dynamically and display them in a dedicated window or panel. Having a built-in GUIHandler or similar would allow:
• Streaming log messages to a Python list
• Easy integration with GUI elements or other reactive applications
• Real-time display of logs in the frontend

Proposed solution:

Add a new handler class (e.g., StringHandler, GUIHandler) to pylog that appends formatted log messages to a user-provided list-like container.

class StringLogHandler(logging.Handler):
    """Handler that captures log messages into a string buffer"""
    def __init__(self):
        super().__init__()
        self.buffer = io.StringIO()
        self.setFormatter(LogFormatter(color=False))

    def emit(self, record):
        msg = self.format(record)
        self.buffer.write(msg + "\n")  # each log on a new line

    def get_value(self):
        return self.buffer.getvalue()

    def clear(self):
        self.buffer = io.StringIO()


class GuiLogHandler(logging.Handler):
    """Stores log messages with level-based color"""

    def __init__(self):
        super().__init__()
        self.logs = []  # list of (color, formatted_message)
        self.setFormatter(LogFormatter(color=False))

    def emit(self, record):
        msg = self.format(record)
        color = LogFormatter.COLOR.get(record.levelno, (1.0, 1.0, 1.0, 1.0))  # Default white
        self.logs.append((color, msg))

    def get_logs(self):
        return self.logs

    def clear(self):
        self.logs.clear()

Optional features:
• Optional formatter injection
• Clear/reset interface

This would greatly improve pylog’s utility in GUI-based applications and interactive environments.

Alternatives

No response

FARMS packages

farms_core

Additional context

No response

Metadata

Metadata

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions