当前位置:网站首页>Datetime and logging module
Datetime and logging module
2022-06-28 10:36:00 【0&1 * 1】
One 、Datetime modular
1) brief introduction
1.0datetime yes python Standard library for processing time and date
3. The main use of : datetime.datetime( ) 、 datetime.timedelta( )
3) Class
1. Get the current date time :datetime.now();
res = datetime.now()
print(res) #2019-08-02 11:26:11.663369
2. Convert date time to timestamp : Time Date object .timestamp(); for example :datetime.now().timestamp()
re = datetime.now().timestamp()
print(re) #1564716560.600063
3. Time stamp is converted to date time :datetime.fromtimestamp( Time stamp ) for example : datetime.fromtimestamp(1534231316.796308)
cu = datetime.fromtimestamp(1564716560.600063)
print(cu) #2019-08-02 11:29:20.600063
4. Date time object to string : Time Date object .strftime(format) for example : datetime.now().strftime("%Y-%m-%d")
sh = datetime.now().strftime('%Y year -%m month -%d Japan ')
print(sh) #2019 year -08 month -02 Japan
5. String to date time object :datetime.strptime(data_str, format) for example : datetime.strptime(‘2018-3-22 15:28:18’, ‘%Y-%m-%d %H:%M:%S’)
s = datetime.strptime('2019-08-02 11:29:20','%Y-%m-%d %H:%M:%S')
print(s) #2019-08-02 11:29:20
5)Timedelte: Time operations
1. for example :
datetime.timedelta( days=0,
seconds=0, microseconds=0 milliseconds=0,
minutes=0, hours=0, weeks=0 )
``
#### Two 、Logging modular
##### 1) brief introduction
.logging.level(message) Create a level Level of logging
.logging.basicConfig() Yes logger To configure
from datetime import datetime
import logging
now = datetime.now()
print(now)
logging.info(now)
logging.warning(now) #WARNING:root:2019-08-02 12:25:51.620682
logging.basicConfig(level=logging.DEBUG) # The output information can be divided into several levels
logging.basicConfig(level=logging.INFO) # take logging.debug(a) Debugging ignored
# Application debugging
a = 2 + 3
logging.debug(a) #DEBUG:root:5
b = a - 2
logging.debug(b) #DEBUG:root:3
c = b / 2
logging.debug
d = c * 8
logging.debug(d)
e = d ** 2
print(e) #144.0
##### 2) The log level
1.Logging Several levels in :
DEBUG < INFO < WARNING < ERROR < CRITICAL
]
##### 3)Formatter Format
import logging
logging.basicConfig(filename=‘my.py’, # Save to virtual machine file (my.py)
filemode=‘a’, #a To add w To cover
level=logging.DEBUG, # Restriction level
format=’%(asctime)s %(filename)s %(lineno)s %(message)s’)
a = 8 +5
logging.debug(a) # 2019-08-02 12:52:04,876 date_t.py 53 13
# Be careful : Two configurations Only for the first time , The second time is invalid
4) Modular components
1( Create a logger( Log processor ) object
2( Definition handler( Log processor ), Decide where to send the log
What is commonly used is
StreamHandler Output to console
FileHandler output to a file
3( Set the log level (level) And output format Formatters( Log formatter )
4( hold handler Add to the corresponding logger In the middle
import logging
my_logger = logging.Logger(‘ The first log processor ’) # Log processor object
# Create a file to save
fh = logging.FileHandler(“you.log”)
# Set level and format
fh.setLevel(logging.INFO)
fmt = logging.Formatter(‘time: %(asctime)s - content: %(message)s’)
fh.setFormatter(fmt)
# That's it , hold handler Add to logger Go inside
my_logger.addHandler(fh)
# Set the second handler
sh = logging.StreamHandler()
sh.setLevel(logging.DEBUG)
fmt1 = logging.Formatter(‘time: %(asctime)s - content: %(message)s-streamHandler’)
my_logger.addHandler(sh)
# Setup complete , Start using
my_logger.info(“ I am a component ”)
边栏推荐
- etf持仓如何影响现货金价?
- 【NLP】今年高考英语AI得分134,复旦武大校友这项研究有点意思
- Resolution: overview of decentralized hosting solution
- datetime与logging模块
- Read PDF Text and write excel operation
- 【功能建议】多个工作空间启动时选择某个空间
- Why does istio use spirit for identity authentication?
- Discard Tkinter! Simple configuration to quickly generate cool GUI!
- Katalon当中的output使用方法
- Information hidden in the trend chart of Hong Kong London gold market
猜你喜欢

一种跳板机的实现思路

无线模块透明传输技术的物联网应用案例

Wireless communication module fixed-point transmission - point to multipoint specific transmission application

【力扣——动态规划】整理题目1:基础题目:509、70、746、62、63、343、96(附链接、题目描述、解题方法及代码)
![[unity][ecs] learning notes (I)](/img/eb/1f0ad817bbc441fd8c14d046b82dd0.png)
[unity][ecs] learning notes (I)

Katalon全局变量在TestObject引用

Katalon当中的debug调试

如何利用k线图做技术分析

Fastposter v2.8.4 release e-commerce poster generator

Starting from full power to accelerate brand renewal, Chang'an electric and electrification products sound the "assembly number"
随机推荐
学习机器学习的最佳路径是什么
Why does istio use spirit for identity authentication?
MySQL(二)
appliedzkp zkevm(9)中的Bytecode Proof
Katalon框架测试web(二十)自定义关键字以及上传弹窗操作
Pop up and push in sequence of stack < difficulty coefficient >
请教下, 我在本地idea运行flinkcdc的mysql到mysql全量同步,这个是在我本地ide
2022吴恩达机器学习specialization Week 2 practice lab: Linear regression
Katalon当中的output使用方法
[Li Kou - dynamic planning] sort out topic 1: basic topics: 509, 70, 746, 62, 63, 343, 96 (with links, topic descriptions, problem solving methods and codes)
AQS understanding
June training (day 28) - Dynamic Planning
etf持仓如何影响现货金价?
I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!
【LeetCode每日一题】【2021/12/19】997. 找到小镇的法官
Summary of characteristics of five wireless transmission protocols of Internet of things
Ble Bluetooth module nrf518/nrf281/nrf528/nrf284 chip scheme comparison
Katalon全局变量在TestObject引用
[leetcode daily question] [December 19, 2021] 997 Find the town judge
DlhSoft Kanban Library for WPF