langflow icon indicating copy to clipboard operation
langflow copied to clipboard

Adding to the "Logs" tab of a custom component.

Open SnakeO opened this issue 1 year ago • 1 comments

Feature Request

Hello, when writing a custom component (or modifying an existing one for that matter) -- I can't figure out how to add to the "Logs" tab of the output port. What library should I be using?

Motivation

Need to log some debug data.

Your Contribution

No response

SnakeO avatar Dec 10 '24 09:12 SnakeO

You can write into the output logs tab by using self.log(message, name).

from langflow.custom import Component
from langflow.io import MessageTextInput, Output
from langflow.schema import Data


class CustomComponent(Component):
    display_name = "Custom Component"
    description = "Use as a template to create your own component."
    documentation: str = "http://docs.langflow.org/components/custom"
    icon = "code"
    name = "CustomComponent"

    inputs = [
        MessageTextInput(
            name="input_value",
            display_name="Input Value",
            info="This is a custom component Input",
            value="Hello, World!",
            tool_mode=True,
        ),
    ]

    outputs = [
        Output(display_name="Output", name="output", method="build_output"),
    ]

    def build_output(self) -> Data:
        data = Data(value=self.input_value)
        self.status = data
        self.log("Building output", "my-logging-name")
        return data 

Additionally, you can write persistent logs with the loguru logger:

from langflow.custom import Component
from langflow.io import MessageTextInput, Output
from langflow.schema import Data
from loguru import logger

class CustomComponent(Component):
    display_name = "Custom Component"
    description = "Use as a template to create your own component."
    documentation: str = "http://docs.langflow.org/components/custom"
    icon = "code"
    name = "CustomComponent"

    inputs = [
        MessageTextInput(
            name="input_value",
            display_name="Input Value",
            info="This is a custom component Input",
            value="Hello, World!",
            tool_mode=True,
        ),
    ]

    outputs = [
        Output(display_name="Output", name="output", method="build_output"),
    ]

    def build_output(self) -> Data:
        data = Data(value=self.input_value)
        self.status = data
        configure(log_level="info", log_file="/app/langflow/logs/langflow.log")
        logger.info("Writing persistent log")
        return data

If you have the variables LANGFLOW_LOG_FILE and LANGFLOW_LOG_LEVEL in your environment, calling configure() without arguments would be sufficient

Dexter192 avatar Dec 10 '24 12:12 Dexter192

Hi, @SnakeO. I'm Dosu, and I'm helping the langflow team manage their backlog. I'm marking this issue as stale.

Issue Summary:

  • You sought guidance on adding log entries to the "Logs" tab of an output port for a custom component.
  • Dexter192 provided a detailed example using self.log(message, name) and recommended the loguru logger for persistent logging.
  • The solution included configuration options with LANGFLOW_LOG_FILE and LANGFLOW_LOG_LEVEL environment variables.
  • The response was well-received by other users, indicating the issue was resolved.

Next Steps:

  • Please confirm if this issue is still relevant to the latest version of the langflow repository. If so, feel free to comment to keep the discussion open.
  • If there are no further updates, this issue will be automatically closed in 7 days.

Thank you for your understanding and contribution!

dosubot[bot] avatar Mar 11 '25 16:03 dosubot[bot]