python for循环读取复合json数据_python day five

#简单写法:

import logging

logging.warning("user [alex] attempted wrong password more than 3 times")

logging.critical("server is down")

'''

#输出

WARNING:root:user [alex] attempted wrong password more than 3 times

CRITICAL:root:server is down

'''

#日志级别

'''

Level    When it’s used

DEBUG    Detailed information, typically of interest only when diagnosing problems.

INFO    Confirmation that things are working as expected.

WARNING An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.

ERROR    Due to a more serious problem, the software has not been able to perform some function.

CRITICAL A serious error, indicating that the program itself may be unable to continue running.

'''

#把日志写到文件里:

import logging

logging.basicConfig(filename='example.log',level=logging.INFO)

logging.debug('This message should go to the log file')

logging.info('So should this')

logging.warning('And this, too')

其中下面这句中的level=loggin.INFO意思是,把日志纪录级别设置为INFO,只有比日志是INFO或比INFO级别更高的日志才会被纪录到文件里,在这个例子, 第一条debug日志是不会被纪录的,如果希望纪录debug的日志,那把日志级别改成DEBUG就行了。

#日志加上时间:

import logging

logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')

logging.warning('is when this event was logged.')

'''

#输出

12/12/2010 11:46:36 AM is when this event was logged.

'''

#同时把log打印在屏幕和文件日志里,需要了解一点复杂的知识

The logging library takes a modular approach and offers several categories of components: loggers, handlers, filters, and formatters.

Loggers expose the interface that application code directly uses.

Handlers send the log records (created by loggers) to the appropriate destination.

Filters provide a finer grained facility for determining which log records to output.

Formatters specify the layout of log records in the final output.

例如:

import logging

#create logger

logger = logging.getLogger('TEST-LOG')

logger.setLevel(logging.DEBUG)

# create console handler and set level to debug

ch = logging.StreamHandler()

ch.setLevel(logging.DEBUG)

# create file handler and set level to warning

fh = logging.FileHandler("access.log")

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.addHandler(fh)

# 'application' code

logger.debug('debug message')

logger.info('info message')

logger.warn('warn message')

logger.error('error message')

logger.critical('critical message')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值