<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    統(tǒng)計

    留言簿(1)

    DB

    Others

    QA

    Tech Website

    閱讀排行榜

    評論排行榜

    【轉(zhuǎn)】Python的標準logging模塊

    Configuring Logging

    Programmers can configure logging either by creating loggers, handlers, 
    and formatters explicitly in a main module with the configuration methods listed above (using Python code), or by creating a logging config file. The following code is an example of configuring a very simple logger, a console handler, and a simple formatter in a Python module:

    程序員可以顯示地通過在主模塊里面用上面列出的配置方法創(chuàng)建loggers,handlers和formatters的方式,或者,創(chuàng)建一個logging的配置文件的方式來配置logging.以下是一個非常簡單的配置logger的例子,一個python模塊里面包含了一個命令行handler和一個簡單的formmater:

    import logging

     

    #create logger

    logger 
    = logging.getLogger("simple_example")

    logger.setLevel(logging.DEBUG)

    #create console handler and set level to debug

    ch 
    = logging.StreamHandler()

    ch.setLevel(logging.DEBUG)

    #create formatter

    formatter 
    = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - 

       
    %(message)s")

    #add formatter to ch

    ch.setFormatter(formatter)

    #add ch to logger

    logger.addHandler(ch)

     

    #"application" code

    logger.debug(
    "debug message")

    logger.info(
    "info message")

    logger.warn(
    "warn message")

    logger.error(
    "error message")

    logger.critical(
    "critical message")

    Running this module 
    from the command line produces the following output:

    在在字符界面下運行上面的命令產(chǎn)生以下輸出:

    jmjones@bean:
    ~/logging $ python simple_logging_module.py

    2005-03-19 15:10:26,618 - simple_example - DEBUG - debug message

    2005-03-19 15:10:26,620 - simple_example - INFO - info message

    2005-03-19 15:10:26,695 - simple_example - WARNING - warn message

    2005-03-19 15:10:26,697 - simple_example - ERROR - error message

    2005-03-19 15:10:26,773 - simple_example - CRITICAL - critical message

    The following Python module creates a logger, handler, 
    and formatter nearly identical to those in the example listed above, with the only difference being the names of the objects:

    跟例子差不多,以下的Python模塊創(chuàng)建了一個logger,一個handler和一個formatter,這里僅僅換了換名字:

    import logging

    import logging.config

     

    logging.config.fileConfig(
    "logging.conf")

     

    #create logger

    logger 
    = logging.getLogger("simpleExample")

     

    #"application" code

    logger.debug(
    "debug message")

    logger.info(
    "info message")

    logger.warn(
    "warn message")

    logger.error(
    "error message")

    logger.critical(
    "critical message")

    Here 
    is the logging.conf file:

    這里是logging.conf文件:

    [loggers]

    keys
    =root,simpleExample

     

    [handlers]

    keys
    =consoleHandler

     

    [formatters]

    keys
    =simpleFormatter

     

    [logger_root]

    level
    =DEBUG

    handlers
    =consoleHandler

     

    [logger_simpleExample]

    level
    =DEBUG

    handlers
    =consoleHandler

    qualname
    =simpleExample

    propagate
    =0

     

    [handler_consoleHandler]

    class=StreamHandler

    level
    =DEBUG

    formatter
    =simpleFormatter

    args
    =(sys.stdout,)

     

    [formatter_simpleFormatter]

    format
    =%(asctime)s - %(name)s - %(levelname)s - %(message)s

    datefmt
    =

    The output 
    is nearly identical to that of the non-config-file-based example:

    輸出跟沒有配置文件的例子完全一樣:

    jmjones@bean:
    ~/logging $ python simple_logging_config.py

    2005-03-19 15:38:55,977 - simpleExample - DEBUG - debug message

    2005-03-19 15:38:55,979 - simpleExample - INFO - info message

    2005-03-19 15:38:56,054 - simpleExample - WARNING - warn message

    2005-03-19 15:38:56,055 - simpleExample - ERROR - error message

    2005-03-19 15:38:56,130 - simpleExample - CRITICAL - critical message

    The config file approach has a few advantages over the Python code approach. The first 
    is the separation of configuration and code. The second is the ability of noncoders to easily modify the logging properties. The third is the really cool listen() method, which causes the application to listen on a socket for new configurations--and will update configurations at runtime without forcing you to restart the application!

    用配置文件的方法比直接在Python代碼里面寫有幾個好處.第一個好處是配置和代碼的分離.第二個好處是即使看不懂程序也能方便的更改logging的屬性.第三個好處是”最酷的listen()方法”,用這個方法可以讓你的應(yīng)用程序在一個socket上監(jiān)聽新的配置信息 
    -- 可以直接在運行時改變配置而用不著重啟你的應(yīng)用~!

    Here 
    is a slight modification of the previous config file-based script:

    這里是一個簡單的基于上面配置文件的腳本:

    #!/usr/bin/env python

     

    import logging

    import logging.config

    import time

    import os

     

    #specify logging config file

    logging.config.fileConfig(
    "logging.conf")

     

    #create and start listener on port 9999

    = logging.config.listen(9999)

    t.start()

     

    #create logger

    logger 
    = logging.getLogger("simpleExample")

     

    #watch for existence of file named "f"

    #loop through the code while this file exists

    while os.path.isfile('f'):

        logger.debug(
    "debug message")

        logger.info(
    "info message")

        logger.warn(
    "warn message")

        logger.error(
    "error message")

        logger.critical(
    "critical message")

        time.sleep(
    5)

     

    #cleanup

    logging.config.stopListening()

    t.join()

    That was simple enough. Unfortunately, figuring out what format the config file needs to be took some investigation. The only useful information that I found 
    in the Python Standard Library Reference documentation was in the logging configuration section under the listen() method:

    夠簡單了吧!不幸地是,理解這個配置文件需要進行些研究.我僅僅在Python的標準庫文檔logging配置一節(jié)里面的listen()方法找到一點有用的信息:

    Logging configurations will be sent as a file suitable 
    for processing by fileConfig().

    Logging配置會作為一個能被fileConfig()方法處理的文件發(fā)送.

    Pushing a filename did 
    not work. Pushing the contents of a config file did not work. I had to dig into the source a little. The docstring for the logging socket server's handle() method is:

    用文件名不行,用配置文件的內(nèi)容也不行.我不得不深入一下源碼logging socket服務(wù)器的handler()方法的docstring是這樣寫的:

    Handle a request.

     

    Each request 
    is expected to be a 4-byte length,

    followed by the config file. Uses fileConfig() to do the

    grunt work. 

    處理一個請求.

    每個請求都應(yīng)該是4
    -byte長,后面跟一個配置文件.用fileConfig()方法完成剩下的工作.

    This struck me as a bit odd. Does that mean you can send lengths only 
    for config files of up to 9,999 bytes? Converting the length of the config file to a string did not work either. I looked farther down in the source code of the handle() method. Aaahh. It does a struct.unpack(), so the socket expects the first 4 bytes to be packed binary data. I tried it this way and it worked. The following snippet of code sends the contents of the file named on the command line to localhost:9999:

    這讓我感到有些奇怪.難道說只能發(fā)送長度大于9,999bytes的配置文件嗎?而且把一個配置文件的內(nèi)容轉(zhuǎn)化成一個字符串也是不起作用.我又看了一下handler()方法的源碼.哈.它其實做了struct.unpack()!,所以socket才要求前面4個bytes打包二進制的數(shù)據(jù).我用這種方式重新試了一下,可以了.下面的代碼片斷把指定的文件內(nèi)容通過字符界面發(fā)送到了localhost:9999端口:

    #!/usr/bin/env python

     

    import socket

    import sys

    import struct

     

    HOST 
    = 'localhost'

    PORT 
    = 9999

    = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    print "connecting"

    s.connect((HOST, PORT))

    print "connected"

    data_to_send 
    = open(sys.argv[1], "r").read()

    print "sending length"

    s.send(struct.pack(
    ">L", len(data_to_send)))

    print "sending data"

    s.send(data_to_send)

    print "closing"

    s.close()


    文章轉(zhuǎn)自:http://crazier9527.iteye.com/blog/290026
    參考:
    http://crazier9527.iteye.com/blog/290024
    http://crazier9527.iteye.com/blog/290018
    http://crazier9527.iteye.com/blog/290027

    posted on 2011-08-12 14:49 XXXXXX 閱讀(1213) 評論(0)  編輯  收藏 所屬分類: Python


    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 日本一区二区在线免费观看| 亚洲综合成人婷婷五月网址| 亚洲AV无码久久| 亚洲午夜免费视频| 亚洲三级中文字幕| 美女隐私免费视频看| 三级网站免费观看| 国产精品成人观看视频免费| 国产免费啪嗒啪嗒视频看看| 亚洲最大av无码网址| 久久99亚洲网美利坚合众国| 亚洲色成人WWW永久在线观看| 亚洲AV网一区二区三区| a毛片免费全部在线播放**| 亚洲免费福利在线视频| 亚洲av手机在线观看| 久久亚洲精品中文字幕无码| 中文字幕亚洲精品无码| 九九久久精品国产免费看小说| 99久久99久久免费精品小说| 日本人护士免费xxxx视频| 亚洲中文字幕日产乱码高清app| 亚洲精品影院久久久久久| 免费大片黄在线观看| 精品福利一区二区三区免费视频 | 日韩高清免费观看| 亚洲色精品88色婷婷七月丁香| 亚洲成AV人综合在线观看 | 亚洲乱码无限2021芒果| 色吊丝性永久免费看码| 亚洲免费在线视频观看| 国产美女无遮挡免费视频| 亚洲av综合色区| 精品国产日韩亚洲一区91| 97精品免费视频| 亚洲精品国自产拍在线观看| 亚洲第一页在线视频| sss日本免费完整版在线观看| 国产片AV片永久免费观看| 夜夜春亚洲嫩草影院| 亚洲精华国产精华精华液网站|