Python Flask记录到多个文件 [英] Python Flask logging to multiple files

查看:120
本文介绍了Python Flask记录到多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个flask应用程序,它从2个不同的文件调用2个函数.我已将日志记录设置为2个不同的文件.但是,日志记录似乎总是会附加到一个文件中(无论哪个端点先被命中)

I have a flask application, which calls 2 functions from 2 different files. I've setup logging to 2 different files. However, the logging always seems to append to one file (whatever end point is hit first)

这是文件的结构-

  • app.py
from file1 import fun1
from file2 import fun2
 
@app.route("/end_point1")
def data_1:
    return fun1()
     
@app.route("/end_point2")
def data_2:
    return fun2()

  • file1.py
  • import logging
    
    def fun1:
        logging.basicConfig(filename = "file1.log")
        logging.info("Logging function 1 details to to file1")
        return foo
    

    • file2.py
    • def fun2:
          logging.basicConfig(filename = "file2.log")
          logging.info("Logging function 2 details to to file2")
          return bar
      

      当我运行单个 python 文件时,这个记录很好(单独) - file1.py/file2.py但是,当我运行API时,日志仅追加到一个文件中.

      This logs fine (separately) when I run the individual python files - file1.py / file2.py But the logs append to just one file when I run the API.

      我在记录日志时出了什么问题?我该如何解决?

      What am I doing wrong with the logging? How can I fix it?

      推荐答案

      将此添加到logger_setup.py

      Add this to logger_setup.py

      import logging
      from pathlib import Path
      formatter = logging.Formatter('%(asctime)s - %(levelname)s - [%(filename)s:%(lineno)d] - %(message)s')
      
      
      def setup_logger( name, log_file, level=logging.DEBUG): 
      
          my_file = Path(log_file)
          # print("check the if condition for the file")
          # print(my_file.is_file())
      
          if my_file.is_file():
              #print(logging.getLogger(name).hasHandlers())
              # if logging.getLogger(name).hasHandlers():
              if len(logging.getLogger(name).handlers)>0:
                  return logging.getLogger(name)
              else:
                  handler = logging.FileHandler(log_file, mode='a')        
                  handler.setFormatter(formatter)
                  logger = logging.getLogger(name)
                  logger.setLevel(level)
                  logger.addHandler(handler)
                  logger.propagate = False
                  return logger
          else:
              handler = logging.FileHandler(log_file, mode='a')        
              handler.setFormatter(formatter)
      
              logger = logging.getLogger(name)
              logger.setLevel(level)
              logger.addHandler(handler)
              logger.propagate = False
              return logger
      

      在文件1和2中,您可以使用类似的内容

      Inside the file1 and 2 you can use something like this

      import logging
      import logger_setup
      
      def fun1():
          log_location = 'logs'
          logger = logger_setup.setup_logger(__name__,log_location+__name__+'.log')
          logger.info("Logging function 1 details to to file1")
          return "1"
      

      这篇关于Python Flask记录到多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆