同一文件中的临时记录格式
我希望我在main.log
中保存在main.log
中,我在loggging.basicconfig()
中提到的所有模块登录。格式
是代码:
文件main.py
import module1
import logging
logging.basicConfig(
filename="main.log",
format="%(asctime)s , <%(name)s> , %(levelname)s : %(message)s",
datefmt="%Y-%m-%d %I:%M:%S",
level=logging.DEBUG
)
def main(ip):
lg = logging.getLogger("main")
newformat="%(asctime)s , <%(name)s> , [" + ip + "] , %(levelname)s : %(message)s",
# do another things
lg.debug("somthing done...")
# call func1
module1.func1()
lg.debug("service done")
def main2(ip):
lg = logging.getLogger("main")
newformat="%(asctime)s , <%(name)s> , [" + ip + "] , %(levelname)s : %(message)s",
# do another things
lg.debug("somthing done...")
# call func1
module1.func1()
lg.debug("service done")
main("192.168.1.100")
main2("192.168.1.101")
file module1.py
import logging
def func1():
lg = logging.getLogger("module1.func1")
lg.setLevel(logging.DEBUG)
# do somthing ...
lg.debug("done")
输出是: 文件main.log
,
2022-07-02 05:39:51 , <main1> , DEBUG : somthing done...
2022-07-02 05:39:51 , <module1.func1> , DEBUG : done
2022-07-02 05:39:51 , <main1> , DEBUG : service done
2022-07-02 05:39:51 , <main2> , DEBUG : somthing done...
2022-07-02 05:39:51 , <module1.func1> , DEBUG : done
2022-07-02 05:39:51 , <main2> , DEBUG : service done
但我希望使用新格式:
2022-07-02 05:39:51 , <main1> , [192.168.1.100] , DEBUG : somthing done...
2022-07-02 05:39:51 , <module1.func1> , DEBUG : done
2022-07-02 05:39:51 , <main1> , [192.168.1.100] , DEBUG : service done
2022-07-02 05:39:51 , <main2> , [192.168.1.101] , DEBUG : somthing done...
2022-07-02 05:39:51 , <module1.func1> , DEBUG : done
2022-07-02 05:39:51 , <main2> , [192.168.1.101] , DEBUG : service done
i want all modules logs that import to the main.py
save in main.log
that i mention in logging.basicConfig()
but with their formats
here is the code:
file main.py
import module1
import logging
logging.basicConfig(
filename="main.log",
format="%(asctime)s , <%(name)s> , %(levelname)s : %(message)s",
datefmt="%Y-%m-%d %I:%M:%S",
level=logging.DEBUG
)
def main(ip):
lg = logging.getLogger("main")
newformat="%(asctime)s , <%(name)s> , [" + ip + "] , %(levelname)s : %(message)s",
# do another things
lg.debug("somthing done...")
# call func1
module1.func1()
lg.debug("service done")
def main2(ip):
lg = logging.getLogger("main")
newformat="%(asctime)s , <%(name)s> , [" + ip + "] , %(levelname)s : %(message)s",
# do another things
lg.debug("somthing done...")
# call func1
module1.func1()
lg.debug("service done")
main("192.168.1.100")
main2("192.168.1.101")
file module1.py
import logging
def func1():
lg = logging.getLogger("module1.func1")
lg.setLevel(logging.DEBUG)
# do somthing ...
lg.debug("done")
the output is:
file main.log
2022-07-02 05:39:51 , <main1> , DEBUG : somthing done...
2022-07-02 05:39:51 , <module1.func1> , DEBUG : done
2022-07-02 05:39:51 , <main1> , DEBUG : service done
2022-07-02 05:39:51 , <main2> , DEBUG : somthing done...
2022-07-02 05:39:51 , <module1.func1> , DEBUG : done
2022-07-02 05:39:51 , <main2> , DEBUG : service done
but i want this with new format:
2022-07-02 05:39:51 , <main1> , [192.168.1.100] , DEBUG : somthing done...
2022-07-02 05:39:51 , <module1.func1> , DEBUG : done
2022-07-02 05:39:51 , <main1> , [192.168.1.100] , DEBUG : service done
2022-07-02 05:39:51 , <main2> , [192.168.1.101] , DEBUG : somthing done...
2022-07-02 05:39:51 , <module1.func1> , DEBUG : done
2022-07-02 05:39:51 , <main2> , [192.168.1.101] , DEBUG : service done
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了一种使用
logging.loggeradapter(Logger,{parameters})
您可以将vars传递给日志格式的
方法,因此我编写了一个函数来返回logger和format and format and andlers and Handlers。
并在每个模块主体中调用此功能,并使用
logging.logger.adapter()
module1.py
或您可以将其用于函数和伐木链中
module2.py
I found a way with
logging.LoggerAdapter(logger, {parameters})
you can pass vars to log format
so I wrote a function to return logger and format and handlers want.
and call this function in each module body and pass IP with
logging.Logger.Adapter()
module1.py
or you can use it in functions and logging chain
module2.py
不是最好的解决方案,但这应该完成工作
Not the best solution but this should get the job done