Python輸出日誌
Python provides a very powerful logging library in its standard library. A lot of programmers use print statements for debugging (myself included), but you can also use logging to do this. It’s actually cleaner to use logging as you won’t have to go through all your code to remove the print statements. In this tutorial we’ll cover the following topics:
• Creating a simple logger
• How to log from multiple modules
• Log formatting
• Log configuration
By the end of this tutorial, you should be able to confidently create your own logs for your applications. Let’s get started!
Creating a Simple Logger
Creating a log with the logging module is easy and straight-forward. It’s easiest to just look at a piece of code and then explain it, so here’s some code for you to read:
# add filemode="w" to overwrite
logging.basicConfig(filename="sample.log", level=logging.INFO)
logging.debug("This is a debug message")
logging.info("Informational message")
logging.error("An error has happened!")
• Creating a simple logger
• How to log from multiple modules
• Log formatting
• Log configuration
By the end of this tutorial, you should be able to confidently create your own logs for your applications. Let’s get started!
Creating a Simple Logger
Creating a log with the logging module is easy and straight-forward. It’s easiest to just look at a piece of code and then explain it, so here’s some code for you to read:
CODE:
import logging# add filemode="w" to overwrite
logging.basicConfig(filename="sample.log", level=logging.INFO)
logging.debug("This is a debug message")
logging.info("Informational message")
logging.error("An error has happened!")
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-739941/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- python如何輸出日誌?Python
- python日誌重複輸出Python
- reportbuilder 日誌輸出UI
- python怎麼將列印輸出日誌檔案Python
- Java日誌輸出問題Java
- awk多行日誌排序輸出排序
- Java 如何正確地輸出日誌Java
- C#按照日期輸出程式日誌C#
- 指令碼日誌的標準輸出指令碼
- 操作日誌記錄(包括輸出至自定義日誌檔案)
- 失敗登入行為輸出至alert日誌
- 【RMAN】rman的日誌輸出功能演示
- python 使用 loguru 輸出異常日誌同時列印變數值Python變數
- log4j日誌輸出級別(轉)
- logrus hook輸出日誌到本地磁碟Hook
- log4j2分層輸出日誌
- Robotframework-ride 日誌輸出中文亂碼FrameworkIDE
- (定時)任務輸出重定向到日誌
- 另一種列印輸出日誌資訊的方式
- spark指令碼日誌輸出級別設定Spark指令碼
- Hibernate 使用log4j輸出日誌
- 使用Log4j2輸出日誌演示
- logback下日誌輸出前處理操作——以日誌脫敏為例
- Web實時日誌輸出檢視管理系統Web
- linux自動清理Docker標準輸出日誌LinuxDocker
- Spring AOP實現統一日誌輸出Spring
- 實時檢視模擬器的日誌輸出
- MySQL 使用tee記錄語句和輸出日誌MySql
- Laravel 中輸出 SQL 語句的到 log 日誌LaravelSQL
- win8 學習筆記二 輸出日誌筆記
- log4j不輸出日誌錯誤分析
- Linux nohup 啟動服務且不輸出日誌Linux
- DataGuard日誌傳輸模式模式
- 如何利用NLog輸出結構化日誌,並在Kibana優雅分析日誌?
- 啟動tomcat時,日誌裡大量輸出建立資料來源dataSource的日誌Tomcat
- 如何使用 loguru 接管程式的所有日誌輸出?
- nohup不輸出nohup.out日誌資訊,已解決。
- 配置Tomcat的訪問日誌格式化輸出Tomcat