I am playing around with TaskWeaver and I want to develop a plugin in it. I want to be able to add debug statements. However, since the plugin code is run by the code interpreter and now directly through my terminal, simply adding print statements is not working. I also tried using logging and it doesn’t work. The logs just don’t show up anywhere. Neither in file nor in console. Any idea what I can do?
Here’s the code for my sample plugin:
from typing import Any, Dict
from taskweaver.plugin import Plugin, register_plugin
import logging
from logging.handlers import RotatingFileHandler
# Set up logging to file
logging.basicConfig(
filename='sample.log',
filemode='a',
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
datefmt='%H:%M:%S',
level=logging.DEBUG
)
# Set up logging to console
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
# Example usage
logging.info("This should log to both console and file.")
@register_plugin
class TestPlugin(Plugin):
def __call__(self, text: str):
log_file = open("log.txt", "w")
log_file.write("The plugin is called with text: " + text)
logging.info("The plugin is called with text: " + text)
return text + " from test plugin after setting up logs"
if __name__ == "__main__":
from taskweaver.plugin.context import temp_context
with temp_context() as temp_ctx:
render = TestPlugin(name="test_plugin", ctx=temp_ctx, config={})
print(render(text="hello world!"))
And the test_plugin.yaml file is this:
name: test_plugin
enabled: true
required: true
description: >-
This plugin is a test plugin for the OpenTelemetry Collector
parameters:
- name: text
type: string
required: true
description: >-
This is a text parameter
returns:
- name: text
type: string
description: >-
This is a text return
What to do?