Python模块04/包/logging日志

内容大纲

1.包

2.logging日志

1.包

什么是包?
文件夹下具有__init__.py文件就是一个包
# import bake
# bake.api.policy.func() # import bake.api.policy
# bake.api.policy.func() # import bake.api.policy as p
# import bake.db.models as s
# p.func()
# s.foo() # from bake.api.policy import func
# func() # from bake.api import *
# www.ww() # from bake.cmd import *
# manage.get() # from bake.db import models
# models.foo() # from ss.bake import *
# db.models.foo()
# api.policy.func()
# cmd.manage.get() # import ss
# ss.bake.api.policy.func() # import sys
# print(sys.path)
# from ss.bake import * # import www
# www.ww()
# api.www.ww()
# print(sys.path) # 两种:
# 绝对路径导入:
# from ss.bake.api.policy import func
# func()
# 相对路径导入:
# from .. api.www import ww
# ww() # 注意点:
# 1.使用相对路径必须在最外层包的同级进行导入
# 2.python2中import包,如果包没有__init__.py就报错,python3 import包,包里没有__init__.py不会报错 # from ss.bake.cmd.manage import get
# get() # from ss import *
# manage.get()
# policy.func() # from ss import *
# # print(locals())
# c = bake.db.models
# c.foo()

2.logging日志

# logging -- 日志

# import logging
# logging.debug('我是调试')
# logging.info('我是信息')
# logging.warning('我是警告')
# logging.error('我是错误')
# logging.critical('我是危险') # 默认是从warning开始记录 # import logging
# logging.basicConfig(level=logging.DEBUG,
# format='%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s',
# datefmt='%Y-%m-%d %H:%M:%S',
# filename='test.log',
# filemode='w') # dic = {"key":123}
# logging.debug(dic) # num = 100
# logging.info(f"用户当前余额:{num - 50}") # try:
# num = int(input("请输入数字:"))
# except Exception as e:
# logging.warning("int将字符串转换报错了")
# print("12334") # logging.error('我是错误')
# logging.critical('我是危险') import logging
logger = logging.getLogger()
# 创建一个logger
fh = logging.FileHandler('test.log',mode="a",encoding='utf-8') # 文件
ch = logging.StreamHandler() # 屏幕
formatter = logging.Formatter('%(asctime)s - %(name)s - %(filename)s - [line:%(lineno)d] - %(levelname)s - %(message)s')
# 将屏幕和文件都是用以上格式
logger.setLevel(logging.DEBUG)
# 设置记录级别
fh.setFormatter(formatter)
# 使用自定义的格式化内容
ch.setFormatter(formatter)
logger.addHandler(fh) #logger对象可以添加多个fh和ch对象
logger.addHandler(ch) logger.debug('logger debug message')
logger.info('logger info message')
logger.warning('logger warning message')
logger.error('logger error message')
logger.critical('logger critical message')

3.今日总结

# 1.包
# import 包.包.包
# from 包.包.包 import 模块
# 路径:
# 绝对:从最外层的包开始导入
# 相对:从当前(.)开始导入或者父级(..)导入
# 使用相对路径的时候必须在包的外层且同级 # from 包 import *
# 需要在__init__.py做操作 # python2: import 文件夹(没有__init__.py)报错
# python3: import 文件夹(没有__init__.py)不报错 # 包:自己一定要多练练 # 2.日志:
# 记住怎么使用就好了 # 自己定义日志开始
# import logging
# logger = logging.getLogger()
# # 创建一个logger
# fh = logging.FileHandler('test.log',mode="a",encoding='utf-8') # 文件
# ch = logging.StreamHandler() # 屏幕
# formatter = logging.Formatter('%(asctime)s - %(name)s - %(filename)s - [line:%(lineno)d] - %(levelname)s - %(message)s')
# # 将屏幕和文件都是用以上格式
# logger.setLevel(logging.DEBUG)
# # 设置记录级别
# fh.setFormatter(formatter)
# # 使用自定义的格式化内容
# ch.setFormatter(formatter)
# logger.addHandler(fh) #logger对象可以添加多个fh和ch对象
# logger.addHandler(ch)
# 自己定义日志结束
#
# logger.debug('logger debug message')
# logger.info('logger info message')
# logger.warning('logger warning message')
# logger.error('logger error message')
# logger.critical('logger critical message')

最新文章

  1. xampp与Hbuilder、phpstorm配置
  2. 【杂记】JavaScript篇
  3. Effective Objective-C 2.0 — 第三条:多用字面量语法,少用与之等价的方法
  4. 在CentOS上安装SQLServer
  5. [一]初识Json
  6. React Native:使用 JavaScript 构建原生应用 详细剖析
  7. SQL Server 2008无日志文件附加数据库
  8. Android - 支持不同的设备 - 支持不同的平台版本
  9. location将地址栏参数拆分成键值对的对象
  10. 解决ubuntu更新中断后报错问题
  11. centos 7 部署 open-falcon 0.2.0
  12. 十倍效能提升——Web 基础研发体系的建立
  13. Android 几种网络请求的区别与联系
  14. Java代码的编译与反编译那些事儿
  15. 为什么目前无法再docker for windows中调用GPU
  16. 服务管理之NFS
  17. ORM(一)
  18. python框架之Django(16)-接入Redis
  19. MySQL5.7+版本一些问题
  20. 一种新型聚类算法(Clustering by fast search and find of density peaksd)

热门文章

  1. mail邮件操作
  2. 容器编排工具之Docker-compose
  3. input属性设置type="number"之后, 仍可输入e;input限制只输入数字
  4. 深入理解JVM(③)低延迟的Shenandoah收集器
  5. PyCharm远程连接服务器简明教程
  6. 打个总结:Web性能优化
  7. 使用git畅游代码的海洋
  8. jQuery实现全选、反选、删除
  9. 03 . 二进制部署kubernetes1.18.4
  10. JDK8--07:并行流与串行流