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

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

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

    統計

    留言簿(1)

    DB

    Others

    QA

    Tech Website

    閱讀排行榜

    評論排行榜

    【轉】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:

    程序員可以顯示地通過在主模塊里面用上面列出的配置方法創建loggers,handlers和formatters的方式,或者,創建一個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:

    在在字符界面下運行上面的命令產生以下輸出:

    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模塊創建了一個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()方法”,用這個方法可以讓你的應用程序在一個socket上監聽新的配置信息 
    -- 可以直接在運行時改變配置而用不著重啟你的應用~!

    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配置一節里面的listen()方法找到一點有用的信息:

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

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

    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:

    用文件名不行,用配置文件的內容也不行.我不得不深入一下源碼logging socket服務器的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. 

    處理一個請求.

    每個請求都應該是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:

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


    文章轉自: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 閱讀(1214) 評論(0)  編輯  收藏 所屬分類: Python

    主站蜘蛛池模板: jizz免费在线影视观看网站| 亚洲精品欧美综合四区| aaa毛片免费观看| 亚洲人成无码网站久久99热国产| 亚洲精华国产精华精华液好用 | 丁香花免费完整高清观看| 久久亚洲私人国产精品| 蜜桃AV无码免费看永久| 亚洲成a人片毛片在线| 亚欧在线精品免费观看一区| 亚洲人成激情在线播放| 国产一精品一AV一免费孕妇| 亚洲日本一线产区和二线产区对比| 手机在线毛片免费播放| 女bbbbxxxx另类亚洲| 精品国产日韩亚洲一区| 久久精品国产大片免费观看| 亚洲欧洲日产专区| 在线免费观看色片| 成人久久久观看免费毛片| 亚洲VA中文字幕无码毛片| **俄罗斯毛片免费| 亚洲av最新在线观看网址| 亚洲精品久久久www| 日韩免费高清大片在线| 国产成人精品日本亚洲18图| 国产成人精品高清免费| 中文字幕手机在线免费看电影| 亚洲精品高清视频| 色播在线永久免费视频| 91福利免费网站在线观看| 久久久久亚洲AV无码专区首JN| 思思99re66在线精品免费观看| 爱情岛论坛免费视频| 色婷婷六月亚洲婷婷丁香| 男女交性永久免费视频播放| 国产黄色片免费看| 亚洲一区二区三区免费视频| 五月婷婷亚洲综合| 免费观看激色视频网站bd| 亚洲国产成人AV网站|