Python_way, Day5

Source: Internet
Author: User

Time
Time.time ()
1465120729.18217 1987.1.1 0:0 minutes start in seconds

Time.ctime ()
Mon June 6 22:56:53 2016 current system time

Time.ctime (Time.time ()-86400)
Sun June 5 22:58:27 | Mon June 6 22:58:27 CTime module supports incoming parameters, passing in one minus day of the number of seconds to get the date and time of yesterday
Time.gmtime (Time.time () -86640)    Gmtime got UTC time
Time.struct_time (tm_year=2016, tm_mon=6, tm_mday=5, tm_hour=14, tm_min=56, tm_sec=53, tm_wday=6, tm_yday=157, TM_ISDST =0)
#将时间戳转换为struct mode, also supports value transfer

#作用是可以拿到里面每一个值
Time_obj = Time.gmtime ()
Print (Time.obj.tm_mon,tome.obj.tm_yday)
6 157

Time.localtime (Time.time () -86400) #这个得到的是本地时间, you can also add methods
#得到的也是一个 struct Object
Time.struct_time (tm_year=2016, tm_mon=6, tm_mday=6, tm_hour=23, tm_min=11, tm_sec=41, tm_wday=0, tm_yday=158, TM_ISDST =0)


Time.mktime (Time.localtime ()) #将struct_time modules into time objects


Time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ()/time.gmtime)   #将struct converted to a custom format


Time.strptime ("2016-01-28", "%y-%m-%d")     #将2016-01-28 converted to struct mode
Time.struct_time (tm_year=2016, Tm_mon=1, tm_mday=28, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=28, Tm_isdst=-1 )
Time.strptime ("2016-01-28 15:06", "%y-%m-%d%h:%m")

Time.struct_time (tm_year=2016, Tm_mon=1, tm_mday=28, tm_hour=23, tm_min=36, tm_sec=0, tm_wday=3, tm_yday=28, tm_isdst= -1)

Requirement: Converts the time of the string format to a timestamp:
2015-06-06 05:23:40 converted to 1348129312.12 such timestamps
A = Time.strptime ("2016-06-06 23:47:60", "%y-%m-%d%h:%m:%s")
b = Time.mktime (a)

· 1465228080.0

Time.sleep (4) #使程序睡眠4秒钟, can use program blocking


Datetime:

Datetime.date.today () #显示当前的日期
2016-06-07

Datetime.date.fromtimestamp (Time.time () -864400) #将时间戳转换成日期格式
2016-05-28

  

Log processing:
Import Logging     logging.warning ("user[xxx]attempted wrong password more than 3 time")     logging.critical ("Server IS-down ")
So that the information in warning and critical is printed on the screen.

warning:root:user[xxx]attempted wrong password more than 3 time
CRITICAL:root:server is down

But info debug can't output
The default is the log output with administrator privileges
Log level:
DEBUG
INFO
WARNING
ERROR
CRITICAL

If you want to export the log to a file
Import logging
Logging.basicconfig (filename= ' Test.log ',level=logging.info)
Registration log format, rank, journal name (you can also set the time, and then again)

Logging.debug ("Debuy")
Logging.info ("info")
Logging.warning ("wraning")
So the output of the log level will be above the info, so the debug at this time will not output
The log input has no date at this time.
How to add the date??
Logging.basicconfig (filename= ' Test.log ', level=logging.info,format= '% (asctime) s% (name) s% (message) s ', datefmt= '% M-%d-%y%i:%m:%s%p ') #这样就把更多的信息注册到了basicconfig中了  

Level = Set the rank of the log output
Format = Set the contents of the log output asctime placeholder for the time of the name of the user name placeholder for message information
DATEFMT = asctime time input in format%h:24- hour%i:8- hour system

Logging.debug ("==debuy==") logging.info ("==info==") logging.warning ("==wraning==")

06-11-2016 10:39:31 AM Root ==info==
06-11-2016 10:39:31 AM Root ==wraning==

Parameters in format:

    % (name) s name of the logger (logging channel)% (Levelno) s Numeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL)% (levelname) s Text logging level for the message (  "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL")% (pathname) s full pathname of the source File where the logging call is issued (if available)% (filename) s filename portion of P Athname% (module) s module (name portion of filename)% (Lineno) d Source line number where the Loggi NG call is issued (if available)% (funcName) s Function name% (created) F time When the LogRecord is created (Time.time () return value)% (asctime) s textual time when The LogRecord is created% (msecs) d millisecond portion of the creation time% (relativecreated) d time in M Illiseconds when the LogRecord was created, relative to the time of the logging module was loaded (Typi Cally at application startup time)% (thread) d thread ID (if available)% (threadname) s thread name (if Available)% (process) d process ID (if available)% (message) s The result of Record.getmessage (), Comp Uted just as the record is emitted

Mixed mode, both output to the screen and placed in a file

The log module contains: Loggers, handlers,filters,formatters

Loggers, which is called directly by the application

Handlers: Send logs to different places

Filters: Filtering provides some log filtering capabilities (what special characters are included in the log to output the log)

Formatters: Formatted output


Import logging



#create Logger

Logger = Logging.getlogger (' Test-log ')
#指定谁发的日志, the default is the location of root
#先获取到logger对象

Logger.setlevel (logging. DEBUG)
#设置全局的日志级别




# Create console handler and set level to debug
#把设置好的等级和位置就可以注册给后面的 handler.
ch = logging. Streamhandler ()
#StreamHandler把日志打印到屏幕, if you want to go to the screen output,
Ch.setlevel (logging. DEBUG)
#设置往屏幕输入时候的级别


# Create file handler and set level to warning
#往文件中输出
FH = logging. Filehandler ("Access.log", encoding= "Utf-8")
Fh.setlevel (logging. WARNING)
#设置往文件中输出的等级
# Create Formatter

Formatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s ')
#设置输出的格式


# Add formatter to CH and FH
#定义往屏幕输出的格式
Ch.setformatter (Formatter)
#定义往问价你输出的格式
Fh.setformatter (Formatter)



# Add CH and FH to Logger

Logger.addhandler (CH)
#将往屏幕输出的格式赋值给logger

Logger.addhandler (FH)
#将往文件输出的格式赋值给logger


# ' Application ' code

Logger.debug (' Debug message ')

Logger.info (' info message ')

Logger.warn (' Warn message ')

Logger.error (' Error message ')

Logger.critical (' critical message ')

  

Python_way, Day5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.