python logging usage

來源:互聯網
上載者:User

標籤:

  python中,logging模組主要是處理日誌的。    所謂日誌,可理解為在軟體運行過程中,所記錄的的一些運行情況資訊    軟體開發人員可以根據自己的需求添加日誌,日誌可以協助軟體開發人員    瞭解軟體的運行資訊,對軟體的維護尤為重要。    記錄層級:       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 happended,or indicative of some problem in the near future.The software is still working as expected      ERROR                         Due to a more serious problem,the software has not been able to perform some funciton      CRITICAL                      A serious error, indication that the program itself may be unable to continue running.    The default level is WARNING.    Here is an Example:        import logging    logging.info(‘this is an info log!‘)    logging.warning(‘this is a warn log!‘)    you can see the result:    WARNING:root:this is a warn log!    如果你想看到層級比較低的一些日誌,你可以這樣做:    Here is an Example:    import logging    logging.basicConfig(filename = ‘c:\\test\\hongten.log‘, level = logging.DEBUG)    logging.debug(‘this is a debug log!‘)    logging.info(‘this is an info log!‘)    logging.warning(‘this is a warn log!‘)    you can see the result:    DEBUG:root:this is a debug log!    INFO:root:this is an info log!    WARNING:root:this is a warn log!    如果你想格式化輸出日誌,你可以這樣做:        Here is an Example:    import logging    logging.basicConfig(format = ‘%(levelname)s:%(message)s‘, level = logging.DEBUG)    logging.debug(‘this is a debug log!‘)    logging.info(‘this is an info log!‘)    logging.warning(‘this is a warn log!‘)    you can see the result:    DEBUG:this is a debug log!    INFO:this is an info log!    WARNING:this is a warn log!    下面是LogRecord attributes,在格式化輸出日誌的時候需要用到:    Attribute name                     Format                                   Description     args                              You shouldn’t need to format              The tuple of arguments merged into msg to produce message.                                      this yourself.           asctime                           %(asctime)s                               時間格式    created                           %(created)s                               建立時間    filename                          %(filename)s                              檔案名稱    levelname                         %(levelname)s                             記錄層級    levelno                           %(levelno)s                               日誌id號    lineno                            %(lineno)s                                行號    module                            %(module)s                                模組名稱    mescs                             %(mescs)s                                 Millisecond portion of the time when the LogRecord was created.    message                           %(message)s                               日誌資訊    name                              %(name)s                                  日誌名稱    pathname                          %(pathname)s                              檔案絕對路徑    process                           %(process)s                               進程id    processName                       %(processName)s                           進程名稱    relativeCreated                   %(relativeCreated)s                       Time in milliseconds when the LogRecord was created,                                                                                relative to the time the logging module was loaded.    thread                            %(thread)s                                線程id    threadName                        %(threadName)s                            線程名稱

python logging usage

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.