我正在使用TaskWeaver,我想在其中开发一个插件。我希望能够添加调试语句。但是,由于插件代码是由代码解释器运行并且现在直接通过我的终端运行,因此仅添加打印语句是行不通的。我也尝试过使用日志记录,但它不起作用。日志只是不显示在任何地方。既不在文件中也不在控制台中。知道我能做什么吗?
这是我的示例插件的代码:
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!"))
test_plugin.yaml 文件是这样的:
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
该怎么办?
在_call方法内的插件代码中,我们可以添加日志语句,如下所示。它将打印在taskweaver.log文件中。
self.log("info","printing dummy message")