python 日志 装饰器_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")

--------------------------------

### 装饰器

- 使用装饰器,打印函数执行的时间

----------------------------------

import logging

LOG_FORMART = "%(asctime)s - %(levelname)s - %(message)s"

logging.basicConfig(format = LOG_FORMART)

def log(func):

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")

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")

test()

main()

-----------------------------------------

你可能感兴趣的:(python,日志,装饰器)