Python---进阶---logging---装饰器打印日志2
2024-10-01 05:43:16
### 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()
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
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()
-----------------------------------------
最新文章
- SQLite
- 瞄准SMART目标
- C#中将DataTable转成List
- 打开mysql时,提示 1040,Too many connections
- Asp.net MVC中提交集合对象,实现Model绑定
- HDOJ 2181 哈密顿绕行世界问题
- android文字阴影效果设置
- 【HDOJ】3496 Watch The Movie
- maven错误解决:编码GBK的不可映射字符
- The Time in Words
- [转] Lisp语言:Do循环的使用
- python web with bottle and session (beaker)
- 第七章——DMVs和DMFs(4)——用DMV和DMF监控磁盘IO
- JSP内置对象--response对象 (addCookie(),setHeader(),sendRedirect())
- nginx slab内存管理
- Hybrid容器设计之第三方网站
- 【php增删改查实例】第二十六节 - 个人详情页制作
- linux中执行shell命令的几种常用方法
- 由通过seeion识别保存在cookie中的seeionID引发的CSRF问题
- Android开发 ---基本UI组件4:拖动事件、评分进度条、圆圈式进度条、进度条控制