多くのプログラムではログを記録する必要があり、ログに含まれる情報には通常のプログラム アクセス ログが含まれるほか、エラー、警告、その他の情報出力も含まれる場合があります。Python のログ モジュールは、さまざまな形式で保存できる標準ログ インターフェイスを提供します。記録されたログは、デバッグ、情報、警告、エラー、重大の 5 つのレベルに分類できます。
モジュールの最初の紹介:
#logging初识 import logging logging.warning("user [James] attempted wrong password more than 3 times") logging.critical("server is down") # WARNING:root:user [James] attempted wrong password more than 3 times # CRITICAL:root:server is down
は、最も簡単な方法は、括弧内にある内容が出力された情報であり、ログを記録する場合は、ログレベルの 5 つのレベルを見てみましょう。
#日志打印到文件中 import logging logging.basicConfig(filename="example.log",level=logging.INFO, format="%(asctime)s %(message)s", datefmt="%m/%d/%Y %H:%M:%S [%A]") # H 24小时格式 I 12小时格式 A 周几完整 a 周几简写 p AM/PM logging.debug("This message should go to the log file") logging.info("So should this") logging.warning("And this ,too")
logging .basicConfig は入力ファイルのパス、入力ログ情報レベル、入力形式を定義し、コードの実行後に形式をカスタマイズできます。以下の情報:
10/31/2016 17:16:17 [Monday] So should this 10/31/2016 17:16:17 [Monday] And this ,tooこのうち、次の文の level=loggin は、ログレベルを INFO に設定することを意味します。つまり、INFO 以上のレベルのログのみがファイルに記録されます。たとえば、最初のログは記録されません。はい、デバッグ ログを記録したい場合は、ログ レベルを DEBUG に変更するだけです。 ログを画面上とファイル ログに同時に印刷したい場合は、 ロギング ライブラリはモジュラー アプローチを採用し、ロガー、ハンドラー、フィルター、フォーマッタなどのコンポーネントのいくつかのカテゴリを提供します。ロガーは、アプリケーション コードが直接使用するインターフェイスを公開します。ハンドラー(ロガーによって作成された) ログ レコードを適切な宛先に送信します。 フィルターは、出力するログ レコードを決定するためのより詳細な機能を提供します。 フォーマッターは、最終出力でのログ レコードのレイアウトを指定します。
#!/usr/bin/env python # -*- coding:utf-8 -*- #-Author-Lian import logging #创建logger logger = logging.getLogger("test_log") #创建logger对象 括号内容随便写 logger.setLevel(logging.INFO) #全局日志级别 ch = logging.StreamHandler() #日志打印到屏幕上 ch.setLevel(logging.DEBUG) #指定ch日志打印级别 fh = logging.FileHandler("access.log") #日志存进文件 fh.setLevel(logging.WARNING) #指定fh日志输入级别 formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") #定义日志格式,可写多个 #添加日志格式到ch,fh ch.setFormatter(formatter) fh.setFormatter(formatter) #添加ch,fh到logger中 logger.addHandler(ch) logger.addHandler(fh) logger.debug('debug message') logger.info('info message') logger.warn('warn message') logger.error('error message') logger.critical('critical message')グローバル ログレベルはプログラム全体の最終行です。印刷する場合、ローカル ログ レベルはこれより高くすることはできません。レベルを低くすることはできません 画面印刷情報
2016-10-31 17:23:42,988 - test_log - INFO - info message 2016-10-31 17:23:42,988 - test_log - WARNING - warn message 2016-10-31 17:23:42,988 - test_log - ERROR - error message 2016-10-31 17:23:42,988 - test_log - CRITICAL - critical messageaccess.log:
2016-10-31 17:02:06,223 - test_log - WARNING - warn message 2016-10-31 17:02:06,224 - test_log - ERROR - error message 2016-10-31 17:02:06,224 - test_log - CRITICAL - critical messageすべてのログ形式:
いくつかの重要な形式: %(lineno)d 出力印刷ログ コード行、%(process) d は印刷ログのプロセス ID を出力、%(thread)d は印刷ログのスレッド ID を出力します

Pythonlistscanstoreanydatatype,arraymodulearraysstoreonetype,andNumPyarraysarefornumericalcomputations.1)Listsareversatilebutlessmemory-efficient.2)Arraymodulearraysarememory-efficientforhomogeneousdata.3)NumPyarraysareoptimizedforperformanceinscient

heouttemptemptostoreavure ofthewrongdatatypeinapythonarray、yure counteractypeerror.thisduetothearraymodule'sstricttypeeencultionyを使用します

PythonListSarePartOfThestAndardarenot.liestareBuilting-in、versatile、forStoringCollectionsのpythonlistarepart。

theScriptisrunningwithwrongthonversionduetorectRectDefaultEntertersettings.tofixthis:1)CheckthedededefaultHaulthonsionsingpython - versionorpython3-- version.2)usevirtualenvironmentsbycreatingonewiththon3.9-mvenvmyenv、andverixe

PythonArraysSupportVariousoperations:1)SlicingExtractsSubsets、2)Appending/ExtendingAdddesements、3)inSertingSelementSatspecificpositions、4)remvingingDeletesements、5)sorting/verversingsorder、and6)listenionsionsionsionsionscreatenewlistsebasedexistin

numpyarraysAressertialentionsionceivationsefirication-efficientnumericalcomputations andDatamanipulation.theyarecrucialindatascience、mashineelearning、物理学、エンジニアリング、および促進可能性への適用性、scaledatiencyを効率的に、forexample、infinancialanalyyy

UseanArray.ArrayOverAlistinPythonは、Performance-criticalCode.1)homogeneousdata:araysavememorywithpedelements.2)Performance-criticalcode:Araysofterbetterbetterfornumerumerumericaleperations.3)interf

いいえ、notallistoperationSaresuptedbyarrays、andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorintorintorinsertizizing、whosimpactsporformance.2)リスト


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

Dreamweaver Mac版
ビジュアル Web 開発ツール

WebStorm Mac版
便利なJavaScript開発ツール

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

mPDF
mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。

ホットトピック









