python学习-datetime和logging简单使用

 1 #!/usr/bin/env python3
 2 # -*- coding: utf-8 -*-
 3 
 4 ' datetime 和 logging 使用 '
 7 
 8 __author__ = 'qfr'
 9 
10 import sys
11 import logging  # 日志输出
12 import os       # 调用操作系统提供的接口函数
13 from datetime import datetime   # 处理日期和时间
14 
15 logging.basicConfig(
16     filename=os.path.join(os.getcwd(),'all.log'),
17     level=logging.DEBUG,
18     format='%(asctime)s [%(levelname)s] : %(message)s --%(filename)s',  # 定义输出log的格式
19     filemode='w',       # w: 重新写文件; a: 追加在文件后
20     datefmt='%Y-%m-%d %A %H:%M:%S',
21 )
22 
23 if __name__=='__main__':
24     args = sys.argv
25     startTime = datetime.now()
26     logging.info('PLL simulation start[%s] !!!', startTime.strftime("%Y-%m-%d %H:%M:%S"))
27     if len(args) == 1:
28         print('Hello, world!')
29     elif len(args) == 2:
30         print('Hello, %s!' % args[1])
31     else:
32         print('Too many arguments!')
33     endTime = datetime.now()
34     logging.info('PLL simulation end[%s] !!!', endTime.strftime("%Y-%m-%d %H:%M:%S"))
35     logging.info('run time: %f sec', endTime.timestamp() - startTime.timestamp())

 

posted @ 2020-05-25 18:50  博客园—哆啦A梦  阅读(374)  评论(0)    收藏  举报