datetime与logging
一、datetime模块
1、概念
2、基本操作
import datetime
t = datetime.time(14,30,30,30) # 时 分 秒 毫秒
print(t) # 14:30:30.000030
date = datetime.date(2019,1,13) # 年 月 日
print(date) # 2019-01-13
dateandtime = datetime.datetime(2019,1,13,14,30,30,30) # 年 月 日 时 分 秒 毫秒
print(dateandtime) # 2019-01-13 14:30:30.000030
1)datetime.now()
获取当前日期时间
t = datetime.datetime.now()
print(t) # 2019-01-13 14:36:23.117000
print(t.time()) # 14:37:09.436000
print(t.date()) # 2019-01-13
2)时间戳
日期时间转化为时间戳:时间日期对象.timestamp()
时间戳转化为日期时间:datetime.fromtimestamp(时间戳)
t = datetime.datetime.now()
res = t.timestamp()
print(t) # 2019-01-13 14:40:14.880000
print(res) # 1547361614.88
t1 = datetime.datetime.fromtimestamp(res)
print(t1) # 2019-01-13 14:40:14.880000
3)日期时间对象转字符串
时间日期对象.strftime(format)
t = datetime.datetime.now()
s = t.strftime("%Y-%m-%d %H:%M:%S")
print(t) # 2019-01-13 14:44:48.428000
print(s) # 2019-01-13 14:44:48
4)字符串转日期时间对象
datetime.strptime(data_str, format)
# s = "2019/1/13 下午2:47:13"
s = "2019-1-13 14:47:13"
t = datetime.datetime.strptime(s,"%Y-%m-%d %H:%M:%S")
print(t)
5)timedelte:时间运算
# datetime.timedelta( days=0,seconds=0, microseconds=0 milliseconds=0,minutes=0, hours=0, weeks=0 )
t1 = datetime.datetime(2019,1,13,14,30,30,30)
t2 = datetime.datetime.now()
t3 = t2 + datetime.timedelta(days=5)
t4 = t1 - datetime.timedelta(hours=5)
print(t2.date()) # 2019-01-13
print(t3.date()) # 2019-01-18
print(t1.time()) # 14:30:30.000030
print(t4.time()) # 09:30:30.000030
二、logging模块
1、概念
2、级别
DEBUG < INFO < WARNING < ERROR < CRITICAL
3、基本操作
import logging
from datetime import datetime
now = datetime.now()
logging.basicConfig(level = logging.DEBUG)
print(now) # 2019-01-13 20:46:49.448000
logging.debug(now)
logging.info(now)
logging.warning(now)
logging.error(now)
logging.critical(now)
'''
DEBUG:root:2019-01-13 20:46:49.448000
INFO:root:2019-01-13 20:46:49.448000
WARNING:root:2019-01-13 20:46:49.448000
ERROR:root:2019-01-13 20:46:49.448000
CRITICAL:root:2019-01-13 20:46:49.448000
'''
# logging.basicConfig(level=logging.DEBUG) # 调试模式,显示调试信息
# logging.basicConfig(level=logging.INFO) # 普通信息,确认程序按照预期运行
# 配置是一次性的,只执行第一次的logging.basicConfig函数
logging.basicConfig(
level=logging.DEBUG ,# 配置日志等级
format= "时间:%(asctime)s 文件名:%(filename)s 行号:%(lineno)d",
filename= "my.log" ,# 配置写入文件名
filemode="a" # 配置写入方式
)
a = 6 + 1
print(a)
logging.debug(a)
b = a - 2
print(b)
logging.debug(b)
c = b * 3
print(c)
logging.debug(c)
# exit()后面的程序不执行,可用于调试
# exit()
d = c / 4
print(d)
logging.debug(d)
e = d ** 2
print(e)
logging.debug(e)
# 创建logger(日志处理器)对象
my_logger = logging.Logger("bai")
# 定义handler(日志处理器),决定把日志发到哪里
mh = logging.FileHandler("test.log")
# 设置日志级别
mh.setLevel(logging.INFO)
# 设置日志格式Formatter(日志格式器)
fmt = logging.Formatter("时间:%(asctime)s 消息:%(message)s 行号:%(lineno)d")
# 把设置日志格式Formatter添加到对应的handler中去,把handler添加到对应的logger中去
mh.setFormatter(fmt)
my_logger.addHandler(mh)
# 定义handler(日志处理器),决定把日志发到哪里
mh1 = logging.StreamHandler()
# 设置日志级别
mh1.setLevel(logging.DEBUG)
# 设置日志格式Formatter(日志格式器)
fmt1 = logging.Formatter("时间:%(asctime)s 消息:%(message)s 行号:%(lineno)d StreamHandler")
# 把设置日志格式Formatter添加到对应的handler中去,把handler添加到对应的logger中去
mh1.setFormatter(fmt1)
my_logger.addHandler(mh1)
# 设置完成后,使用
my_logger.info("我是日志组件")
'''
时间:2019-01-13 20:49:47,097 消息:我是日志组件 行号:82 StreamHandler
'''