Python---进阶---logging---装饰器打印日志2
Posted niaocaizhou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python---进阶---logging---装饰器打印日志2相关的知识,希望对你有一定的参考价值。
### logging
- logging.debug
- logging.info
- logging.warning
- logging.error
- logging.critical
--------------------------------
import logging
logging.basicConfig(level=logging.DEBUG)
logging.debug("this is a debug")
logging.info("this is info")
logging.warning("this is warning")
logging.error("this is error")
logging.critical("this is critical")
logging.info("this is info")
logging.warning("this is warning")
logging.error("this is error")
logging.critical("this is critical")
--------------------------------
### 装饰器
- 使用装饰器,打印函数执行的时间
- 使用装饰器,打印函数执行的时间
----------------------------------
import logging
LOG_FORMART = "%(asctime)s - %(levelname)s - %(message)s"
logging.basicConfig(format = LOG_FORMART)
logging.basicConfig(format = LOG_FORMART)
def log(func):
def wrapper(*arg, **kw):
logging.error("this is info message")
return func(*arg, **kw)
return wrapper
def wrapper(*arg, **kw):
logging.error("this is info message")
return func(*arg, **kw)
return wrapper
@log
def test():
print("test done")
test()
------------------------------------------
##### 使用装饰器,根据不同的函数,传入的日志不相同
LOG_FORMART = "%(asctime)s - %(levelname)s - %(message)s"
logging.basicConfig(format = LOG_FORMART, filename = "my.log")
logging.basicConfig(format = LOG_FORMART, filename = "my.log")
def log(text):
def decorator(func):
def wrapper(*arg, **kw):
logging.error(text)
return func(*arg, **kw)
return wrapper
return decorator
@log("test done")
def test():
print("test done")
@log("main log")
def main():
print("main done")
def test():
print("test done")
@log("main log")
def main():
print("main done")
test()
main()
main()
-----------------------------------------
以上是关于Python---进阶---logging---装饰器打印日志2的主要内容,如果未能解决你的问题,请参考以下文章