码迷,mamicode.com
首页 > 编程语言 > 详细

python_way ,day5

时间:2016-06-17 21:03:15      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:

time:
time.time()
1465120729.18217 1987.1.1 0:0分 开始以秒计时

time.ctime()
Mon Jun  6 22:56:53 2016  当前系统时间

time.ctime(time.time()-86400)
Sun Jun  5 22:58:27 2016 | Mon Jun  6 22:58:27 2016 ctime模块支持传入参数,传入一个减去一天的秒数,获得的就是昨天的日期时间
time.gmtime(time.time()-86640)    gmtime 得到的是utc时间
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 模式 ,也支持传值

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

time.localtime(time.time()-86400)  #这个得到的是本地时间,也可以加方法
#得到的也是一个 struct 对象
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 模块转变成时间对象


time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()/time.gmtime)   #将struct 转换成自定义格式


time.strptime("2016-01-28","%Y-%m-%d")     #将2016-01-28 转换成 struct模式
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)

需求:将字符串格式的时间转换成时间戳:
2015-06-06 05:23:40 转为1348129312.12 这种时间戳
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秒钟,可使用程序的阻塞


datetime:

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

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

  

 

日志处理:

import logging
     logging.warning("user[xxx]attempted wrong password more than 3 time")
     logging.critical("server is down")
这样就把 warning 和 critical中的信息输出到了屏幕上

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

 但是info debug都不能输出
 默认是用管理员权限输出的日志
日志级别:
DEBUG
INFO
WARNING
ERROR
CRITICAL

如果想把日志输出到文件中
import logging
logging.basicConfig(filename=‘test.log‘,level=logging.INFO)
注册日志的格式,等级,日志名称(也可以设置时间,一会再说)

logging.debug("debuy")
logging.info("info")
logging.warning("wraning")
这样输出的日志的等级就要在info以上,所以此时的debug不会输出
此时日志输入没有日期。
怎么加日期哪??
logging.basicConfig(filename=‘test.log‘,level=logging.INFO,format=‘%(asctime)s %(name)s %(message)s‘,datefmt=‘%m-%d-%Y %I:%M:%S %p‘)
#这样就把更多的信息注册到了basicconfig中了  
#filename = 定义日志输出到日志中,如果没有这个就会把日志输出到屏幕上。
level = 设置日志输出的等级
format = 设置日志输出内容的格式 asctime 时间的占位符 name用户名的占位符 message 信息的占位符
datefmt = format中asctime时间输入的格式 %H:24小时制 %I:8小时制

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==

  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 was issued (if available)
    %(filename)s        Filename portion of pathname
    %(module)s          Module (name portion of filename)
    %(lineno)d          Source line number where the logging call was issued
                        (if available)
    %(funcName)s        Function name
    %(created)f         Time when the LogRecord was created (time.time()
                        return value)
    %(asctime)s         Textual time when the LogRecord was created
    %(msecs)d           Millisecond portion of the creation time
    %(relativeCreated)d Time in milliseconds when the LogRecord was created,
                        relative to the time the logging module was loaded
                        (typically 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(), computed just as
                        the record is emitted

混合模式,既输出到屏幕上,又放到文件中

log模块中包含了:loggers , handlers,filters,formatters

loggers,是应用程序直接调用的

handlers:把日志发送到不同的地方

filters:过滤  提供了一些日志过滤的功能(日志中包括了什么特殊字符,就把日志输出出来)

formatters: 格式化输出

 

 

import logging



#create logger

logger = logging.getLogger(‘TEST-LOG‘)
#指定谁发的日志,默认是root的位置
#先获取到logger对象

logger.setLevel(logging.DEBUG)
#设置全局的日志级别




# create console handler and set level to debug
#把设置好的等级和位置就可以注册给后面的 handler 了
ch = logging.StreamHandler()
#StreamHandler把日志打印到屏幕,如果想往屏幕输出,
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

标签:

原文地址:http://www.cnblogs.com/python-way/p/5565638.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!