Python – 为每个存档记录日志

huangapple go评论50阅读模式
英文:

Python - logging for each archive

问题

我需要为每个.py文件创建一个.log文件。

我需要的目录结构示例:

  • /FILES/File_1.py
  • /FILES/File_2.py
  • /LOGS/File_1.log
  • /LOGS/File_2.log
  • /LOGS/Main.log
  • /log_config.py
  • /main.py

在log_config.py中:

import logging

def write_log(name):
     FORMAT = "%(asctime)s :: lvl: %(levelname)s :: line: %(lineno)d :: funcName: %(funcName)s :: message: %(message)s"
     logging.basicConfig(
     format=FORMAT,
     level=logging.DEBUG,
     filemode='w',
     filename="LOGS/"+name+".log"
)

在main.py中:

from log_config import write_log, logging
from FILES.File_1 import save_File_1
from FILES.File_2 import save_File_2

write_log("Main")
logging.info('Test Main')

save_File_1()
save_File_2()

在File_1.py中:

from log_config import write_log, logging

def save_File_1():
     write_log("File 1")
     logging.info("test File_1")

在File_2.py中:

from log_config import write_log, logging

def save_File_2():
     write_log("File 2")
     logging.info("test File_2")

结果:
只创建了一个名为"Main.log"的.log文件,并记录了以下内容:

2023-04-19 11:09:33,544 :: lvl: INFO :: line: 6 :: funcName: <module> :: message: Test Main
2023-04-19 11:09:33,544 :: lvl: INFO :: line: 6 :: funcName: save_File_1 :: message: test File_1
2023-04-19 11:09:33,544 :: lvl: INFO :: line: 6 :: funcName: save_File_2 :: message: test File_2

如果需要进一步帮助,请告诉我。

英文:

I need create a file .log to every py archive.

Example of tree I need:

  • /FILES/File_1.py
  • /FILES/File_2.py
  • /LOGS/File_1.log
  • /LOGS/File_2.log
  • /LOGS/Main.log
  • /log_config.py
  • /main.py

There may be an additional file with the 'logging' configuration, as in the main it will be the call for each of the scripts in the 'FILES' folder.

Now in the application, what I tried:

In log_config.py:

import logging

def write_log(name):

     FORMAT = &quot;%(asctime)s :: lvl: %(levelname)s :: line: %(lineno)d :: funcName: %(funcName)s :: message: %(message)s&quot;
     logging.basicConfig(
     format=FORMAT,
     level=logging.DEBUG,
     filemode=&#39;w&#39;,
     filename=&quot;LOG/&quot;+name+&quot;.log&quot;
)

In main.py:

from log_config import write_log, logging
from FILES.File_1 import save_File_1
from FILES.File_2 import save_File_2

write_log(&quot;Main&quot;)
logging.info(&#39;Test Main&#39;)

save_File_1()
save_File_2()

In File_1.py:

from log_config import write_log, logging

def save_File_1():
     write_log(&quot;File 1&quot;)

     logging.info(&quot;test File_1&quot;)

In File_2.py:

from log_config import write_log, logging

def save_File_2():
     write_log(&quot;File 2&quot;)

     logging.info(&quot;test File_2&quot;)

Result:
Only 1 .log file is created, named "Main.log", and has recorded in it:

2023-04-19 11:09:33,544 :: lvl: INFO :: line: 6 :: funcName: &lt;module&gt; :: message: Test Main
2023-04-19 11:09:33,544 :: lvl: INFO :: line: 6 :: funcName: save_File_1 :: message: test File_1
2023-04-19 11:09:33,544 :: lvl: INFO :: line: 6 :: funcName: save_File_2 :: message: test File_2

If someone can give me a light for this resolution, I would be enormously grateful Python – 为每个存档记录日志

答案1

得分: 0

在 log_config.py 中:

import logging
import sys

def write_log(logger_name, level=logging.DEBUG):
    logger = logging.getLogger(logger_name)
    logger.setLevel(level)
    format_string = "%(asctime)s :: %(name)s :: %(levelname)s :: %(funcName)s:" \
                    "%(lineno)d :: %(message)s"
    log_format = logging.Formatter(format_string)

    # 创建并添加控制台处理程序
    console_handler = logging.StreamHandler(sys.stdout)
    console_handler.setFormatter(log_format)
    logger.addHandler(console_handler)
    # 创建并添加文件处理程序
    file_handler = logging.FileHandler("LOG/"+logger_name+".log", mode='a')
    file_handler.setFormatter(log_format)
    logger.addHandler(file_handler)
    return logger

在 main.py 中:

from log import write_log
from FILES.File_1 import save_File_1
from FILES.File_2 import save_File_2

logger = write_log("Main")
logger.debug('Test Main')

save_File_1()
save_File_2()

在 File_1.py 中:

from log import write_log
import logging

def save_File_1():
    logger = write_log("File 1")
    logger.debug('Test File 1')
英文:

After some research, I found the solution.
I found the answer here: https://stackoverflow.com/questions/54591352/python-logging-new-log-file-each-loop-iteration

In log_config.py:

import logging
import sys

def write_log(logger_name, level=logging.DEBUG):
     logger = logging.getLogger(logger_name)
     logger.setLevel(level)
     format_string = (&quot;%(asctime)s :: %(name)s :: %(levelname)s :: %(funcName)s:&quot;
                 &quot;%(lineno)d :: %(message)s&quot;)
     log_format = logging.Formatter(format_string)

     # Creating and adding the console handler
     console_handler = logging.StreamHandler(sys.stdout)
     console_handler.setFormatter(log_format)
     logger.addHandler(console_handler)
     # Creating and adding the file handler
     file_handler = logging.FileHandler(&quot;LOG/&quot;+logger_name+&quot;.log&quot;, mode=&#39;a&#39;)
     file_handler.setFormatter(log_format)
     logger.addHandler(file_handler)
     return logger

In main.py:

from log import write_log
from FILES.File_1 import save_File_1
from FILES.File_2 import save_File_2

logger = write_log(&quot;Main&quot;)
logger.debug(&#39;Test Main&#39;)

save_File_1()
save_File_2()

In File_1.py:

from log import write_log
import logging

def save_File_1():
    logger = write_log(&quot;File 1&quot;)
    logger.debug(&#39;Test File 1&#39;)

huangapple
  • 本文由 发表于 2023年4月19日 22:18:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定