当前位置:网站首页>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 ”)
边栏推荐
- Generate token
- 一种跳板机的实现思路
- 一种跳板机的实现思路
- Training and recognition of handwritten digits through the lenet-5 network built by pytorch
- [unity] built in rendering pipeline to URP
- BLE蓝牙模块NRF518/NRF281/NRF528/NRF284芯片方案对比
- MarkDown——基本使用语法
- Cisco * VRF(虚拟路由转发表)
- Ble Bluetooth module nrf518/nrf281/nrf528/nrf284 chip scheme comparison
- 读取pdf文字和excel写入操作
猜你喜欢

Interface automation framework scaffold - use reflection mechanism to realize the unified initiator of the interface
![[Unity]EBUSY: resource busy or locked](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[Unity]EBUSY: resource busy or locked

How to use K-line diagram for technical analysis

Set up your own website (11)

Understand 12 convolution methods (including 1x1 convolution, transpose convolution and deep separable convolution)

Fastposter v2.8.4 release e-commerce poster generator

港伦敦金行情走势图所隐藏的信息

Katalon框架测试web(二十)自定义关键字以及上传弹窗操作

idea连接sql sever失败

fastposter v2.8.4 发布 电商海报生成器
随机推荐
使用 ABAP 操作 Excel 的几种方法
知道 Redis RDB 这些细节,可以少踩很多坑
Solve the problem that the value of the action attribute of the form is null when transferring parameters
如何利用k线图做技术分析
ECS MySQL query is slow
Sqlcmd database connection error
Chapter 3 stack and queue
Google开源依赖注入框架-Guice指南
datetime与logging模块
The R language uses the avplots function in the car package to create added variable plots. In image interaction, manually identify (add) strong influence points that have a great impact on each predi
Katalon框架测试一个web页面操作实例代码
Hystrix deployment
MarkDown——基本使用语法
Training and recognition of handwritten digits through the lenet-5 network built by pytorch
[leetcode daily question] [December 19, 2021] 997 Find the town judge
Bytecode proof in appliedzkp zkevm (9)
Unity AssetBundle asset packaging and asset loading
BLE蓝牙模块NRF518/NRF281/NRF528/NRF284芯片方案对比
metersphere使用js刷新当前页面
广州海关支持保障食品、农产品和中药材等民生物资稳定供港