模块 -logging

一:在控制台显示:默认

import logging

logging.debug("debug")

logging.info("debug")

logging.warning("warning")

logging.error("error")

logging.critical("critical")

二.输入到文件,并且设置等级:

import logging

logging.basicConfig(filename="hello.log",level=logging.DEBUG,

format='%(asctime)s %(name)s %(filename)s %(thread)d [%(levelno)s] : %(message)s',)

logging.debug("debug")

logging.info("debug")

logging.warning("warning")

logging.error("error")

logging.critical("critical")

使用到logging.basicConfig()

filename Specifies that a FileHandler be created, using the specified

filename, rather than a StreamHandler.

filemode Specifies the mode to open the file, if filename is specified

(if filemode is unspecified, it defaults to 'a').

format Use the specified format string for the handler.

datefmt Use the specified date/time format.

style If a format string is specified, use this to specify the

type of format string (possible values '%', '{', '$', for

%-formatting, :meth:`str.format` and :class:`string.Template`

- defaults to '%').

level Set the root logger level to the specified level.

stream Use the specified stream to initialize the StreamHandler. Note

that this argument is incompatible with 'filename' - if both

are present, 'stream' is ignored.

handlers If specified, this should be an iterable of already created

handlers, which will be added to the root handler. Any handler

in the list which does not have a formatter assigned will be

assigned the formatter created in this function.

Format的使用:

使用help( logging.Formatter)查看使用方法

| %(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

三.既想在控制台输出,也想输入文件,甚至使用udp发出.

写法一:

import logging

logger = logging.getLogger()

fh = logging.FileHandler("test1",encoding="utf-8")

sh = logging .StreamHandler()

fm = logging.Formatter(

'%(asctime)s %(name)s %(filename)s %(thread)d [%(levelno)s] : %(message)s',

"%Y-%m-%d"

)

sh.setFormatter(fm)

fh.setFormatter(fm)

logger.setLevel(logging.DEBUG)

logger.debug("debug")

logger.info("debug")

logger.warning("warning")

logger.error("error")

logger.critical("critical")

方法二:

import logging

logger = logging.Logger("test", level=logging.DEBUG)

sh = logging.StreamHandler()

fh = logging.FileHandler("test1", encoding="utf-8")

fm = logging.Formatter(

'%(asctime)s %(name)s %(filename)s %(thread)d [%(levelno)s] : %(message)s',

)

sh.setFormatter(fm)

logger.addHandler(sh)

logger.addHandler(fh)

logger.debug("debug")

logger.info("debug")

logger.warning("warning")

logger.error("error")

logger.critical("critical")

明天补充udp

-----------------------------------2017年10月27日11:30:09-----------------------更新---------------------------------------------

udp,,,,2333333333

方法三:

import  logging.handlers

# 创建handlers

rfh = logging.handlers.RotatingFileHandler("ttt", mode='a', maxBytes=1024*10*10,

backupCount=3, encoding="utf-8", delay=False)

slh=logging.handlers.SysLogHandler(address=('192.168.1.5', 1234))

sh=logging.StreamHandler()

# 设置basicConfig

logging.basicConfig(

level=logging.DEBUG,

format='%(asctime)s  %(name)s  %(filename)s %(thread)d [%(lineno)d] :   %(message)s',

handlers=[rfh,slh,sh]

)

logging.debug("debug")

logging.info("debug")

logging.warning("warning")

logging.error("error")

logging.critical("critical")

handlers

 

具体:

http://www.cnblogs.com/dkblog/archive/2011/08/26/2155018.html

logging.StreamHandler: 日志输出到流,可以是sys.stderr、sys.stdout或者文件

logging.FileHandler: 日志输出到文件

日志回滚方式,实际使用时用RotatingFileHandler和TimedRotatingFileHandler

logging.handlers.BaseRotatingHandler

logging.handlers.RotatingFileHandler

logging.handlers.TimedRotatingFileHandler

logging.handlers.SocketHandler: 远程输出日志到TCP/IP sockets

logging.handlers.DatagramHandler:  远程输出日志到UDP sockets

logging.handlers.SMTPHandler:  远程输出日志到邮件地址

logging.handlers.SysLogHandler: 日志输出到syslog

logging.handlers.NTEventLogHandler: 远程输出日志到Windows NT/2000/XP的事件日志

logging.handlers.MemoryHandler: 日志输出到内存中的制定buffer

logging.handlers.HTTPHandler: 通过"GET"或"POST"远程输出到HTTP服务器

模块 -logging的更多相关文章

  1. Python标准模块--logging

    1 logging模块简介 logging模块是Python内置的标准模块,主要用于输出运行日志,可以设置输出日志的等级.日志保存路径.日志文件回滚等:相比print,具备如下优点: 可以通过设置不同 ...

  2. 日志模块logging使用心得

    在应用程序使用中,日志输出对应用维护人员.开发人员判断程序的问题起重要作用. 那么在python中如何定义程序的日志输出? 推荐使用日志模块logging 需求:实现日志内容输出在文件中和控制器中 i ...

  3. python日志模块logging

    python日志模块logging   1. 基础用法 python提供了一个标准的日志接口,就是logging模块.日志级别有DEBUG.INFO.WARNING.ERROR.CRITICAL五种( ...

  4. Python 日志模块logging

    logging模块: logging是一个日志记录模块,可以记录我们日常的操作. logging日志文件写入默认是gbk编码格式的,所以在查看时需要使用gbk的解码方式打开. logging日志等级: ...

  5. shelve模块,sys模块,logging模块

    1.shelve模块 用于序列化的模块,shelve模块比pickle模块简单,只有open函数,返回类似字典的对象,可读可写;key必须为字符串,而值可以是python所支持的数据类型. impor ...

  6. python最重要的模块logging

    logging模块 这个模块是目前最重要的模块!!!我一定给讲透彻一点 很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python中的loggi ...

  7. Python标准模块--logging(转载)

    转载地址:http://www.cnblogs.com/zhbzz2007/p/5943685.html#undefined Python标准模块--logging 1 logging模块简介 log ...

  8. os模块/sys模块/json/pickle模块/logging模块(day16整理)

    目录 今日内容 os模块 对文件操作 对文件夹此操作 辅助性的 了解 sys模块 json和pickle模块 json模块 pickle模块 logging模块 日志级别 添加设置 自定义配置 今日内 ...

  9. Python日志模块logging简介

    日志处理是项目的必备功能,配置合理的日志,可以帮助我们了解系统的运行状况.定位位置,辅助数据分析技术,还可以挖掘出一些额外的系统信息. 本文介绍Python内置的日志处理模块logging的常见用法. ...

随机推荐

  1. PHP关于注册注意的问题

    1.注意转义字符的问题 get_magic_quotes_gpc()开启时,所有的 ' (单引号), " (双引号), \(反斜线) and 空字符(null)会自动转为含有反斜线的溢出字符 ...

  2. PHP的反射API

    PHP5的类和对象并没有告诉我们类内的所有一切,而只是报告了他们的公共成员.要充分了解一个类,需要知道其私有 成员和保护成员,还要知道其方法所期望的参数,对此我们要使用API 1.获得反射API的转储 ...

  3. 21_HTML&CSS

    今日内容: 1. HTML标签:表单标签​2. CSS: HTML标签:表单标签 * 表单: * 概念:用于采集用户输入的数据的.用于和服务器进行交互. * form:用于定义表单的.可以定义一个范围 ...

  4. 路飞学城Python-Day23

    1.计算机基础 Python可以实现各种应用软件,类比word.QQ.爱奇艺等,但是应用这些软件需要计算机硬件, 计算机发展的过程就是人类不断的希望机器去取代人力,解放更多的人力,最终极的理想就是完全 ...

  5. ArchLinux出现ACPI ERROR的解决方法

    ArchLinux关机.重启时出现ACPI错误: ACPI Error:Method parse/execution failed \_SB.PCI0.PGON,AE_AML_LOOP_TIMEOUT ...

  6. python--(常用模块-3-正则表达式)

    python--(常用模块-3-正则表达式) 正则表达式是对字符串操作的⼀种逻辑公式. 我们⼀般使⽤正则表达式对字符串进⾏匹 配和过滤. 使⽤正则的优缺点: 优点: 灵活, 功能性强, 逻辑性强. 缺 ...

  7. webpack实战---安装操作

    什么是webpack? 他有什么优点?     首先对于很多刚接触webpack人来说,肯定会问webpack是什么?它有什么优点?我们为什么要使用它?     Webpack是前端一个工具,可以让各 ...

  8. 【 【henuacm2016级暑期训练】动态规划专题 K】 Really Big Numbers

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 会发现如果x是reallynumber那么x+1也会是reallynumber.... (个位数+1,各位数的和+1了但是整个数也+ ...

  9. 2015 Multi-University Training Contest 5 hdu 5352 MZL's City

    MZL's City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  10. POJ 1671

    其实求的是BELL数,即前N个第二类斯特林数的和. 一首诗有n行,每一行有一种韵律,问这首诗总共可能有多少种韵律排列.如4行,则所有的15种情况为:aaaa, aaab, aaba, aabb, aa ...