如何在python中为两个脚本实现单个记录器,一个定义工作流,另一个包含所有函数



我有两个python脚本一个作为workflow.py,另一个是task.py。workflow.py定义了task.py的工作流,因此它只有main在其中,它实例化了task.py的构造函数。worflow.py的结构如下:

workflow.py
from optparse import OptionParser
from optparse import OptionGroup
from task import *
def main():
        dir = os.path.dirname(os.path.abspath(sys.argv[0]))
    try:
        parser = parse_options(parser, dir)
        (options, args) = parser.parse_args()
        print ("everything working fine in workflow")
    except SystemExit:
        return 0
    task_object = task_C(options.source_dir, options.target, options.build_dir)
    print ("workflow is reached")
    task_object.another_funct()
    ##following 3 lines is when set_logger is defined
    log = task_object .set_logger()
    log.info(""workflow is reached"")
    log.info("more info if required")

def parser(parser, dir):
        group = OptionGroup(parser, "Prepare")
        group.add_option("-t", "--target",action="store", dest="target",
                      help="Specifies the target.")
        group.add_option("-s", "--Source",action="store", dest="source_dir",
                      help="Specifies the source dir.")
        group.add_option("-b", "--build",action="store", dest="build_dir",
                      help="Specifies the build dir.")
        parser.add_option_group(group)

task.py
class task_C():
   def __init__(self, source_dir, target, build_dir):
     self.target = self.settargetplatform(target)
     self.source_dir = self.setsourcedir(source_dir)
     self.build_dir = self.setbuilddir(build_dir)
   def settargetplatform( target):
    ...sets target dir
   def setsourcedir(source_dir ):
    ...sets source_dir
   def setbuilddir(build_dir):
    ..sets build_dir
   def another_funct( ):
    print ("inside the another funct")
    print ("some usefull info")
    print ("...")
   ##following part after adding set_logger then using logger
    log = self.set_logger()
    log.info( "inside the another funct")
    log.info( " some usefull info")
    log.info ("...")
    .
  def set_logger(self):
        logging.basicConfig()
        l = logging.getLogger('root')
        l.setLevel(logging.DEBUG)
        formatter = logging.Formatter(' %(levelname)s : %(message)s')
        fileHandler = logging.FileHandler(self.build_dir+"/root.log", mode='w')
        fileHandler.setFormatter(formatter)    
        l.addHandler(fileHandler)

现在,正如上面的脚本所示,任务。py构造函数在工作流中调用,两个脚本中都有各种打印语句,我想有一个日志记录器而不是打印语句,为此目的,我想将日志放在位置"build_dir"中,但该位置设置在任务。py中,我不想在workflow.py中添加另一个函数,它检索回"build_dir"。我在task.py中添加了set_logger()函数,正如您在task.py中看到的那样,它可以满足我的目的,但我得到的日志包含所有NULL NULL NULL…等等。所以,建议我怎么能有一个日志包含所有的打印语句在这两个脚本和我需要做什么改进?

实际上可以这样做,但关键是在那个case日志位置必须在工作流。py中定义,我不想定义位置因为它已经在task.py中定义了。在工作流中,我不想这样做为已经在task.py

中设置的记录器定义相同的位置

根据你上面的评论-然后,您可以调用worker.py中的set_logger()并将其传递给task.py,即在worker.py中具有以下行:

task_object = task_C(options.source_dir, options.target, options.build_dir)
log = task_object .set_logger()

对于任何对任务方法的调用,传递记录器(方法必须接受它作为参数)-例如:

task_object.another_funct(log=log)

对于日志不能正常工作-在task.py

set_logger()末尾添加return l

我想我会在workflow.py中定义logger,并将其作为参数传递给task_C的构造函数。这似乎是最简单的解决方案。

最新更新