英文:
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 = "%(asctime)s :: lvl: %(levelname)s :: line: %(lineno)d :: funcName: %(funcName)s :: message: %(message)s"
logging.basicConfig(
format=FORMAT,
level=logging.DEBUG,
filemode='w',
filename="LOG/"+name+".log"
)
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("Main")
logging.info('Test Main')
save_File_1()
save_File_2()
In File_1.py:
from log_config import write_log, logging
def save_File_1():
write_log("File 1")
logging.info("test File_1")
In File_2.py:
from log_config import write_log, logging
def save_File_2():
write_log("File 2")
logging.info("test File_2")
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: <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
If someone can give me a light for this resolution, I would be enormously grateful
答案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 = ("%(asctime)s :: %(name)s :: %(levelname)s :: %(funcName)s:"
"%(lineno)d :: %(message)s")
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("LOG/"+logger_name+".log", mode='a')
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("Main")
logger.debug('Test Main')
save_File_1()
save_File_2()
In File_1.py:
from log import write_log
import logging
def save_File_1():
logger = write_log("File 1")
logger.debug('Test File 1')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论