-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathlogging.html
More file actions
1783 lines (1716 loc) · 138 KB
/
Copy pathlogging.html
File metadata and controls
1783 lines (1716 loc) · 138 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="fa" data-content_root="../">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<meta property="og:title" content="Logging HOWTO" />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://docs.python.org/3/howto/logging.html" />
<meta property="og:site_name" content="Python documentation" />
<meta property="og:description" content="Author, Vinay Sajip <vinay_sajip at red-dove dot com>,. This page contains tutorial information. For links to reference information and a logging cookbook, please see Other resources. Basic L..." />
<meta property="og:image" content="_static/og-image.png" />
<meta property="og:image:alt" content="Python documentation" />
<meta name="description" content="Author, Vinay Sajip <vinay_sajip at red-dove dot com>,. This page contains tutorial information. For links to reference information and a logging cookbook, please see Other resources. Basic L..." />
<meta name="theme-color" content="#3776ab">
<meta property="og:image:width" content="200">
<meta property="og:image:height" content="200">
<title>Logging HOWTO — مستندات Python3.14.6</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />
<link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=4365c8fe" />
<link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=0fc419ee" />
<script src="../_static/documentation_options.js?v=e254dbbb"></script>
<script src="../_static/doctools.js?v=9bcbadda"></script>
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="../_static/translations.js?v=5df48d09"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="جستجو در مستندات Python3.14.6"
href="../_static/opensearch.xml"/>
<link rel="author" title="درباره این مستندات" href="../about.html" />
<link rel="index" title="فهرست" href="../genindex.html" />
<link rel="search" title="جستجو" href="../search.html" />
<link rel="copyright" title="حق چاپ" href="../copyright.html" />
<link rel="next" title="Logging Cookbook" href="logging-cookbook.html" />
<link rel="prev" title="Functional Programming HOWTO" href="functional.html" />
<link rel="canonical" href="https://docs.python.org/3/howto/logging.html">
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
<link rel="shortcut icon" type="image/png" href="../_static/py.svg">
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/menu.js"></script>
<script type="text/javascript" src="../_static/search-focus.js"></script>
<script type="text/javascript" src="../_static/themetoggle.js"></script>
<script type="text/javascript" src="../_static/rtd_switcher.js"></script>
<meta name="readthedocs-addons-api-version" content="1">
</head>
<body>
<div class="mobile-nav">
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">
<nav class="nav-content" role="navigation">
<label for="menuToggler" class="toggler__label">
<span></span>
</label>
<span class="nav-items-wrapper">
<a href="https://www.python.org/" class="nav-logo">
<img src="../_static/py.svg" alt="Python logo">
</a>
<span class="version_switcher_placeholder"></span>
<form role="search" class="search" action="../search.html" method="get">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
<path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
</svg>
<input placeholder="جستجو سریع" aria-label="جستجو سریع" type="search" name="q">
<input type="submit" value="برو">
</form>
</span>
</nav>
<div class="menu-wrapper">
<nav class="menu" role="navigation" aria-label="main navigation">
<div class="language_switcher_placeholder"></div>
<label class="theme-selector-label">
Theme
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label>
<div>
<h3><a href="../contents.html">فهرست عناوین</a></h3>
<ul>
<li><a class="reference internal" href="#">Logging HOWTO</a><ul>
<li><a class="reference internal" href="#basic-logging-tutorial">Basic Logging Tutorial</a><ul>
<li><a class="reference internal" href="#when-to-use-logging">When to use logging</a></li>
<li><a class="reference internal" href="#a-simple-example">A simple example</a></li>
<li><a class="reference internal" href="#logging-to-a-file">Logging to a file</a></li>
<li><a class="reference internal" href="#logging-variable-data">Logging variable data</a></li>
<li><a class="reference internal" href="#changing-the-format-of-displayed-messages">Changing the format of displayed messages</a></li>
<li><a class="reference internal" href="#displaying-the-date-time-in-messages">Displaying the date/time in messages</a></li>
<li><a class="reference internal" href="#next-steps">Next Steps</a></li>
</ul>
</li>
<li><a class="reference internal" href="#advanced-logging-tutorial">Advanced Logging Tutorial</a><ul>
<li><a class="reference internal" href="#logging-flow">Logging Flow</a></li>
<li><a class="reference internal" href="#loggers">Loggers</a></li>
<li><a class="reference internal" href="#handlers">Handlers</a></li>
<li><a class="reference internal" href="#formatters">Formatters</a></li>
<li><a class="reference internal" href="#configuring-logging">Configuring Logging</a></li>
<li><a class="reference internal" href="#what-happens-if-no-configuration-is-provided">What happens if no configuration is provided</a></li>
<li><a class="reference internal" href="#configuring-logging-for-a-library">Configuring Logging for a Library</a></li>
</ul>
</li>
<li><a class="reference internal" href="#logging-levels">Logging Levels</a><ul>
<li><a class="reference internal" href="#custom-levels">Custom Levels</a></li>
</ul>
</li>
<li><a class="reference internal" href="#useful-handlers">Useful Handlers</a></li>
<li><a class="reference internal" href="#exceptions-raised-during-logging">Exceptions raised during logging</a></li>
<li><a class="reference internal" href="#using-arbitrary-objects-as-messages">Using arbitrary objects as messages</a></li>
<li><a class="reference internal" href="#optimization">Optimization</a></li>
<li><a class="reference internal" href="#other-resources">Other resources</a></li>
</ul>
</li>
</ul>
</div>
<div>
<h4>موضوع قبلی</h4>
<p class="topless"><a href="functional.html"
title="فصل قبلی">Functional Programming HOWTO</a></p>
</div>
<div>
<h4>موضوع بعدی</h4>
<p class="topless"><a href="logging-cookbook.html"
title="فصل بعدی">Logging Cookbook</a></p>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const title = document.querySelector('meta[property="og:title"]').content;
const elements = document.querySelectorAll('.improvepage');
const pageurl = window.location.href.split('?')[0];
elements.forEach(element => {
const url = new URL(element.href.split('?')[0].replace("-nojs", ""));
url.searchParams.set('pagetitle', title);
url.searchParams.set('pageurl', pageurl);
url.searchParams.set('pagesource', "howto/logging.rst");
element.href = url.toString();
});
});
</script>
<div role="note" aria-label="source link">
<h3>این صفحه</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">گزارش یک اشکال</a></li>
<li><a class="improvepage" href="../improve-page-nojs.html">بهبود این صفحه</a></li>
<li>
<a href="https://github.com/python/cpython/blob/main/Doc/howto/logging.rst?plain=1"
rel="nofollow">نمایش منبع
</a>
</li>
<li>
<a href="https://github.com/python/python-docs-fa/blob/3.14/howto/logging.po?plain=1"
rel="nofollow">نمایش منبع ترجمه</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div class="related" role="navigation" aria-label="Related">
<h3>ناوبری</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="فهرست کلی"
accesskey="I">فهرست</a></li>
<li class="right" >
<a href="../py-modindex.html" title="نمایه ی ماژول های پایتون"
>ماژول ها</a> |</li>
<li class="right" >
<a href="logging-cookbook.html" title="Logging Cookbook"
accesskey="N">بعدی</a> |</li>
<li class="right" >
<a href="functional.html" title="Functional Programming HOWTO"
accesskey="P">قبلی</a> |</li>
<li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.14.6 Documentation</a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python HOWTOs</a> »</li>
<li class="nav-item nav-item-this"><a href="">Logging HOWTO</a></li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="جستجو سریع" aria-label="جستجو سریع" type="search" name="q" id="search-box">
<input type="submit" value="برو">
</form>
</div>
|
</li>
<li class="right">
<label class="theme-selector-label">
Theme
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label> |</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="logging-howto">
<span id="id1"></span><h1>Logging HOWTO<a class="headerlink" href="#logging-howto" title="Link to this heading">¶</a></h1>
<dl class="field-list simple">
<dt class="field-odd">Author<span class="colon">:</span></dt>
<dd class="field-odd"><p>Vinay Sajip <vinay_sajip at red-dove dot com></p>
</dd>
</dl>
<p id="logging-basic-tutorial">This page contains tutorial information. For links to reference information and a
logging cookbook, please see <a class="reference internal" href="#tutorial-ref-links"><span class="std std-ref">Other resources</span></a>.</p>
<section id="basic-logging-tutorial">
<h2>Basic Logging Tutorial<a class="headerlink" href="#basic-logging-tutorial" title="Link to this heading">¶</a></h2>
<p>Logging is a means of tracking events that happen when some software runs. The
software's developer adds logging calls to their code to indicate that certain
events have occurred. An event is described by a descriptive message which can
optionally contain variable data (i.e. data that is potentially different for
each occurrence of the event). Events also have an importance which the
developer ascribes to the event; the importance can also be called the <em>level</em>
or <em>severity</em>.</p>
<section id="when-to-use-logging">
<h3>When to use logging<a class="headerlink" href="#when-to-use-logging" title="Link to this heading">¶</a></h3>
<p>You can access logging functionality by creating a logger via <code class="docutils literal notranslate"><span class="pre">logger</span> <span class="pre">=</span>
<span class="pre">logging.getLogger(__name__)</span></code>, and then calling the logger's <a class="reference internal" href="../library/logging.html#logging.Logger.debug" title="logging.Logger.debug"><code class="xref py py-meth docutils literal notranslate"><span class="pre">debug()</span></code></a>,
<a class="reference internal" href="../library/logging.html#logging.Logger.info" title="logging.Logger.info"><code class="xref py py-meth docutils literal notranslate"><span class="pre">info()</span></code></a>, <a class="reference internal" href="../library/logging.html#logging.Logger.warning" title="logging.Logger.warning"><code class="xref py py-meth docutils literal notranslate"><span class="pre">warning()</span></code></a>, <a class="reference internal" href="../library/logging.html#logging.Logger.error" title="logging.Logger.error"><code class="xref py py-meth docutils literal notranslate"><span class="pre">error()</span></code></a> and
<a class="reference internal" href="../library/logging.html#logging.Logger.critical" title="logging.Logger.critical"><code class="xref py py-meth docutils literal notranslate"><span class="pre">critical()</span></code></a> methods. To determine when to use logging, and to see
which logger methods to use when, see the table below. It states, for each of a
set of common tasks, the best tool to use for that task.</p>
<table class="docutils align-default">
<thead>
<tr class="row-odd"><th class="head"><p>Task you want to perform</p></th>
<th class="head"><p>The best tool for the task</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>Display console output for ordinary
usage of a command line script or
program</p></td>
<td><p><a class="reference internal" href="../library/functions.html#print" title="print"><code class="xref py py-func docutils literal notranslate"><span class="pre">print()</span></code></a></p></td>
</tr>
<tr class="row-odd"><td><p>Report events that occur during
normal operation of a program (e.g.
for status monitoring or fault
investigation)</p></td>
<td><p>A logger's <a class="reference internal" href="../library/logging.html#logging.Logger.info" title="logging.Logger.info"><code class="xref py py-meth docutils literal notranslate"><span class="pre">info()</span></code></a> (or
<a class="reference internal" href="../library/logging.html#logging.Logger.debug" title="logging.Logger.debug"><code class="xref py py-meth docutils literal notranslate"><span class="pre">debug()</span></code></a> method for very
detailed output for diagnostic
purposes)</p></td>
</tr>
<tr class="row-even"><td><p>Issue a warning regarding a
particular runtime event</p></td>
<td><p><a class="reference internal" href="../library/warnings.html#warnings.warn" title="warnings.warn"><code class="xref py py-func docutils literal notranslate"><span class="pre">warnings.warn()</span></code></a> in library
code if the issue is avoidable and
the client application should be
modified to eliminate the warning</p>
<p>A logger's <a class="reference internal" href="../library/logging.html#logging.Logger.warning" title="logging.Logger.warning"><code class="xref py py-meth docutils literal notranslate"><span class="pre">warning()</span></code></a>
method if there is nothing the client
application can do about the
situation, but the event should still
be noted</p>
</td>
</tr>
<tr class="row-odd"><td><p>Report an error regarding a
particular runtime event</p></td>
<td><p>Raise an exception</p></td>
</tr>
<tr class="row-even"><td><p>Report suppression of an error
without raising an exception (e.g.
error handler in a long-running
server process)</p></td>
<td><p>A logger's <a class="reference internal" href="../library/logging.html#logging.Logger.error" title="logging.Logger.error"><code class="xref py py-meth docutils literal notranslate"><span class="pre">error()</span></code></a>,
<a class="reference internal" href="../library/logging.html#logging.Logger.exception" title="logging.Logger.exception"><code class="xref py py-meth docutils literal notranslate"><span class="pre">exception()</span></code></a> or
<a class="reference internal" href="../library/logging.html#logging.Logger.critical" title="logging.Logger.critical"><code class="xref py py-meth docutils literal notranslate"><span class="pre">critical()</span></code></a> method as
appropriate for the specific error
and application domain</p></td>
</tr>
</tbody>
</table>
<p>The logger methods are named after the level or severity of the events
they are used to track. The standard levels and their applicability are
described below (in increasing order of severity):</p>
<table class="docutils align-default">
<thead>
<tr class="row-odd"><th class="head"><p>Level</p></th>
<th class="head"><p>When it's used</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">DEBUG</span></code></p></td>
<td><p>Detailed information, typically of interest
only when diagnosing problems.</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">INFO</span></code></p></td>
<td><p>Confirmation that things are working as
expected.</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">WARNING</span></code></p></td>
<td><p>An indication that something unexpected
happened, or indicative of some problem in
the near future (e.g. 'disk space low').
The software is still working as expected.</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">ERROR</span></code></p></td>
<td><p>Due to a more serious problem, the software
has not been able to perform some function.</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">CRITICAL</span></code></p></td>
<td><p>A serious error, indicating that the program
itself may be unable to continue running.</p></td>
</tr>
</tbody>
</table>
<p>The default level is <code class="docutils literal notranslate"><span class="pre">WARNING</span></code>, which means that only events of this severity and higher
will be tracked, unless the logging package is configured to do otherwise.</p>
<p>Events that are tracked can be handled in different ways. The simplest way of
handling tracked events is to print them to the console. Another common way
is to write them to a disk file.</p>
</section>
<section id="a-simple-example">
<span id="howto-minimal-example"></span><h3>A simple example<a class="headerlink" href="#a-simple-example" title="Link to this heading">¶</a></h3>
<p>A very simple example is:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">logging</span>
<span class="n">logging</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s1">'Watch out!'</span><span class="p">)</span> <span class="c1"># will print a message to the console</span>
<span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">'I told you so'</span><span class="p">)</span> <span class="c1"># will not print anything</span>
</pre></div>
</div>
<p>If you type these lines into a script and run it, you'll see:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>WARNING:root:Watch out!
</pre></div>
</div>
<p>printed out on the console. The <code class="docutils literal notranslate"><span class="pre">INFO</span></code> message doesn't appear because the
default level is <code class="docutils literal notranslate"><span class="pre">WARNING</span></code>. The printed message includes the indication of the
level and the description of the event provided in the logging call, i.e.
'Watch out!'. The actual output can be formatted quite flexibly if you need
that; formatting options will also be explained later.</p>
<p>Notice that in this example, we use functions directly on the <code class="docutils literal notranslate"><span class="pre">logging</span></code>
module, like <code class="docutils literal notranslate"><span class="pre">logging.debug</span></code>, rather than creating a logger and calling
functions on it. These functions operate on the root logger, but can be useful
as they will call <a class="reference internal" href="../library/logging.html#logging.basicConfig" title="logging.basicConfig"><code class="xref py py-func docutils literal notranslate"><span class="pre">basicConfig()</span></code></a> for you if it has not been called yet, like in
this example. In larger programs you'll usually want to control the logging
configuration explicitly however - so for that reason as well as others, it's
better to create loggers and call their methods.</p>
</section>
<section id="logging-to-a-file">
<h3>Logging to a file<a class="headerlink" href="#logging-to-a-file" title="Link to this heading">¶</a></h3>
<p>A very common situation is that of recording logging events in a file, so let's
look at that next. Be sure to try the following in a newly started Python
interpreter, and don't just continue from the session described above:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">logging</span>
<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>
<span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="n">filename</span><span class="o">=</span><span class="s1">'example.log'</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="s1">'utf-8'</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">)</span>
<span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">'This message should go to the log file'</span><span class="p">)</span>
<span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">'So should this'</span><span class="p">)</span>
<span class="n">logger</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s1">'And this, too'</span><span class="p">)</span>
<span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s1">'And non-ASCII stuff, too, like Øresund and Malmö'</span><span class="p">)</span>
</pre></div>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">تغییر داده شده در نسخه 3.9: </span>The <em>encoding</em> argument was added. In earlier Python versions, or if not
specified, the encoding used is the default value used by <a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a>. While
not shown in the above example, an <em>errors</em> argument can also now be passed,
which determines how encoding errors are handled. For available values and
the default, see the documentation for <code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code>.</p>
</div>
<p>And now if we open the file and look at what we have, we should find the log
messages:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>DEBUG:__main__:This message should go to the log file
INFO:__main__:So should this
WARNING:__main__:And this, too
ERROR:__main__:And non-ASCII stuff, too, like Øresund and Malmö
</pre></div>
</div>
<p>This example also shows how you can set the logging level which acts as the
threshold for tracking. In this case, because we set the threshold to
<code class="docutils literal notranslate"><span class="pre">DEBUG</span></code>, all of the messages were printed.</p>
<p>If you want to set the logging level from a command-line option such as:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>--log=INFO
</pre></div>
</div>
<p>and you have the value of the parameter passed for <code class="docutils literal notranslate"><span class="pre">--log</span></code> in some variable
<em>loglevel</em>, you can use:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">getattr</span><span class="p">(</span><span class="n">logging</span><span class="p">,</span> <span class="n">loglevel</span><span class="o">.</span><span class="n">upper</span><span class="p">())</span>
</pre></div>
</div>
<p>to get the value which you'll pass to <a class="reference internal" href="../library/logging.html#logging.basicConfig" title="logging.basicConfig"><code class="xref py py-func docutils literal notranslate"><span class="pre">basicConfig()</span></code></a> via the <em>level</em>
argument. You may want to error check any user input value, perhaps as in the
following example:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># assuming loglevel is bound to the string value obtained from the</span>
<span class="c1"># command line argument. Convert to upper case to allow the user to</span>
<span class="c1"># specify --log=DEBUG or --log=debug</span>
<span class="n">numeric_level</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">logging</span><span class="p">,</span> <span class="n">loglevel</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="kc">None</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">numeric_level</span><span class="p">,</span> <span class="nb">int</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">'Invalid log level: </span><span class="si">%s</span><span class="s1">'</span> <span class="o">%</span> <span class="n">loglevel</span><span class="p">)</span>
<span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="n">level</span><span class="o">=</span><span class="n">numeric_level</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>
</pre></div>
</div>
<p>The call to <a class="reference internal" href="../library/logging.html#logging.basicConfig" title="logging.basicConfig"><code class="xref py py-func docutils literal notranslate"><span class="pre">basicConfig()</span></code></a> should come <em>before</em> any calls to a logger's
methods such as <a class="reference internal" href="../library/logging.html#logging.Logger.debug" title="logging.Logger.debug"><code class="xref py py-meth docutils literal notranslate"><span class="pre">debug()</span></code></a>, <a class="reference internal" href="../library/logging.html#logging.Logger.info" title="logging.Logger.info"><code class="xref py py-meth docutils literal notranslate"><span class="pre">info()</span></code></a>, etc. Otherwise,
that logging event may not be handled in the desired manner.</p>
<p>If you run the above script several times, the messages from successive runs
are appended to the file <em>example.log</em>. If you want each run to start afresh,
not remembering the messages from earlier runs, you can specify the <em>filemode</em>
argument, by changing the call in the above example to:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="n">filename</span><span class="o">=</span><span class="s1">'example.log'</span><span class="p">,</span> <span class="n">filemode</span><span class="o">=</span><span class="s1">'w'</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">)</span>
</pre></div>
</div>
<p>The output will be the same as before, but the log file is no longer appended
to, so the messages from earlier runs are lost.</p>
</section>
<section id="logging-variable-data">
<h3>Logging variable data<a class="headerlink" href="#logging-variable-data" title="Link to this heading">¶</a></h3>
<p>To log variable data, use a format string for the event description message and
append the variable data as arguments. For example:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">logging</span>
<span class="n">logging</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s1">'</span><span class="si">%s</span><span class="s1"> before you </span><span class="si">%s</span><span class="s1">'</span><span class="p">,</span> <span class="s1">'Look'</span><span class="p">,</span> <span class="s1">'leap!'</span><span class="p">)</span>
</pre></div>
</div>
<p>will display:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>WARNING:root:Look before you leap!
</pre></div>
</div>
<p>As you can see, merging of variable data into the event description message
uses the old, %-style of string formatting. This is for backwards
compatibility: the logging package pre-dates newer formatting options such as
<a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a> and <a class="reference internal" href="../library/string.html#string.Template" title="string.Template"><code class="xref py py-class docutils literal notranslate"><span class="pre">string.Template</span></code></a>. These newer formatting
options <em>are</em> supported, but exploring them is outside the scope of this
tutorial: see <a class="reference internal" href="logging-cookbook.html#formatting-styles"><span class="std std-ref">Using particular formatting styles throughout your application</span></a> for more information.</p>
</section>
<section id="changing-the-format-of-displayed-messages">
<h3>Changing the format of displayed messages<a class="headerlink" href="#changing-the-format-of-displayed-messages" title="Link to this heading">¶</a></h3>
<p>To change the format which is used to display messages, you need to
specify the format you want to use:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">logging</span>
<span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="nb">format</span><span class="o">=</span><span class="s1">'</span><span class="si">%(levelname)s</span><span class="s1">:</span><span class="si">%(message)s</span><span class="s1">'</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">)</span>
<span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">'This message should appear on the console'</span><span class="p">)</span>
<span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">'So should this'</span><span class="p">)</span>
<span class="n">logging</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s1">'And this, too'</span><span class="p">)</span>
</pre></div>
</div>
<p>which would print:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>DEBUG:This message should appear on the console
INFO:So should this
WARNING:And this, too
</pre></div>
</div>
<p>Notice that the 'root' which appeared in earlier examples has disappeared. For
a full set of things that can appear in format strings, you can refer to the
documentation for <a class="reference internal" href="../library/logging.html#logrecord-attributes"><span class="std std-ref">LogRecord attributes</span></a>, but for simple usage, you just
need the <em>levelname</em> (severity), <em>message</em> (event description, including
variable data) and perhaps to display when the event occurred. This is
described in the next section.</p>
</section>
<section id="displaying-the-date-time-in-messages">
<h3>Displaying the date/time in messages<a class="headerlink" href="#displaying-the-date-time-in-messages" title="Link to this heading">¶</a></h3>
<p>To display the date and time of an event, you would place '%(asctime)s' in
your format string:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">logging</span>
<span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="nb">format</span><span class="o">=</span><span class="s1">'</span><span class="si">%(asctime)s</span><span class="s1"> </span><span class="si">%(message)s</span><span class="s1">'</span><span class="p">)</span>
<span class="n">logging</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s1">'is when this event was logged.'</span><span class="p">)</span>
</pre></div>
</div>
<p>which should print something like this:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>2010-12-12 11:41:42,612 is when this event was logged.
</pre></div>
</div>
<p>The default format for date/time display (shown above) is like ISO8601 or
<span class="target" id="index-0"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc3339.html"><strong>RFC 3339</strong></a>. If you need more control over the formatting of the date/time, provide
a <em>datefmt</em> argument to <code class="docutils literal notranslate"><span class="pre">basicConfig</span></code>, as in this example:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">logging</span>
<span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="nb">format</span><span class="o">=</span><span class="s1">'</span><span class="si">%(asctime)s</span><span class="s1"> </span><span class="si">%(message)s</span><span class="s1">'</span><span class="p">,</span> <span class="n">datefmt</span><span class="o">=</span><span class="s1">'%m/</span><span class="si">%d</span><span class="s1">/%Y %I:%M:%S %p'</span><span class="p">)</span>
<span class="n">logging</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s1">'is when this event was logged.'</span><span class="p">)</span>
</pre></div>
</div>
<p>which would display something like this:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>12/12/2010 11:46:36 AM is when this event was logged.
</pre></div>
</div>
<p>The format of the <em>datefmt</em> argument is the same as supported by
<a class="reference internal" href="../library/time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a>.</p>
</section>
<section id="next-steps">
<h3>Next Steps<a class="headerlink" href="#next-steps" title="Link to this heading">¶</a></h3>
<p>That concludes the basic tutorial. It should be enough to get you up and
running with logging. There's a lot more that the logging package offers, but
to get the best out of it, you'll need to invest a little more of your time in
reading the following sections. If you're ready for that, grab some of your
favourite beverage and carry on.</p>
<p>If your logging needs are simple, then use the above examples to incorporate
logging into your own scripts, and if you run into problems or don't understand
something, please post a question in the Help category of the <a class="reference external" href="https://discuss.python.org/c/help/7">Python
discussion forum</a> and you should receive
help before too long.</p>
<p>Still here? You can carry on reading the next few sections, which provide a
slightly more advanced/in-depth tutorial than the basic one above. After that,
you can take a look at the <a class="reference internal" href="logging-cookbook.html#logging-cookbook"><span class="std std-ref">Logging Cookbook</span></a>.</p>
</section>
</section>
<section id="advanced-logging-tutorial">
<span id="logging-advanced-tutorial"></span><h2>Advanced Logging Tutorial<a class="headerlink" href="#advanced-logging-tutorial" title="Link to this heading">¶</a></h2>
<p>The logging library takes a modular approach and offers several categories
of components: loggers, handlers, filters, and formatters.</p>
<ul class="simple">
<li><p>Loggers expose the interface that application code directly uses.</p></li>
<li><p>Handlers send the log records (created by loggers) to the appropriate
destination.</p></li>
<li><p>Filters provide a finer grained facility for determining which log records
to output.</p></li>
<li><p>Formatters specify the layout of log records in the final output.</p></li>
</ul>
<p>Log event information is passed between loggers, handlers, filters and
formatters in a <a class="reference internal" href="../library/logging.html#logging.LogRecord" title="logging.LogRecord"><code class="xref py py-class docutils literal notranslate"><span class="pre">LogRecord</span></code></a> instance.</p>
<p>Logging is performed by calling methods on instances of the <a class="reference internal" href="../library/logging.html#logging.Logger" title="logging.Logger"><code class="xref py py-class docutils literal notranslate"><span class="pre">Logger</span></code></a>
class (hereafter called <em class="dfn">loggers</em>). Each instance has a name, and they are
conceptually arranged in a namespace hierarchy using dots (periods) as
separators. For example, a logger named 'scan' is the parent of loggers
'scan.text', 'scan.html' and 'scan.pdf'. Logger names can be anything you want,
and indicate the area of an application in which a logged message originates.</p>
<p>A good convention to use when naming loggers is to use a module-level logger,
in each module which uses logging, named as follows:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>
</pre></div>
</div>
<p>This means that logger names track the package/module hierarchy, and it's
intuitively obvious where events are logged just from the logger name.</p>
<p>The root of the hierarchy of loggers is called the root logger. That's the
logger used by the functions <a class="reference internal" href="../library/logging.html#logging.debug" title="logging.debug"><code class="xref py py-func docutils literal notranslate"><span class="pre">debug()</span></code></a>, <a class="reference internal" href="../library/logging.html#logging.info" title="logging.info"><code class="xref py py-func docutils literal notranslate"><span class="pre">info()</span></code></a>, <a class="reference internal" href="../library/logging.html#logging.warning" title="logging.warning"><code class="xref py py-func docutils literal notranslate"><span class="pre">warning()</span></code></a>,
<a class="reference internal" href="../library/logging.html#logging.error" title="logging.error"><code class="xref py py-func docutils literal notranslate"><span class="pre">error()</span></code></a> and <a class="reference internal" href="../library/logging.html#logging.critical" title="logging.critical"><code class="xref py py-func docutils literal notranslate"><span class="pre">critical()</span></code></a>, which just call the same-named method of
the root logger. The functions and the methods have the same signatures. The
root logger's name is printed as 'root' in the logged output.</p>
<p>It is, of course, possible to log messages to different destinations. Support
is included in the package for writing log messages to files, HTTP GET/POST
locations, email via SMTP, generic sockets, queues, or OS-specific logging
mechanisms such as syslog or the Windows NT event log. Destinations are served
by <em class="dfn">handler</em> classes. You can create your own log destination class if
you have special requirements not met by any of the built-in handler classes.</p>
<p>By default, no destination is set for any logging messages. You can specify
a destination (such as console or file) by using <a class="reference internal" href="../library/logging.html#logging.basicConfig" title="logging.basicConfig"><code class="xref py py-func docutils literal notranslate"><span class="pre">basicConfig()</span></code></a> as in the
tutorial examples. If you call the functions <a class="reference internal" href="../library/logging.html#logging.debug" title="logging.debug"><code class="xref py py-func docutils literal notranslate"><span class="pre">debug()</span></code></a>, <a class="reference internal" href="../library/logging.html#logging.info" title="logging.info"><code class="xref py py-func docutils literal notranslate"><span class="pre">info()</span></code></a>,
<a class="reference internal" href="../library/logging.html#logging.warning" title="logging.warning"><code class="xref py py-func docutils literal notranslate"><span class="pre">warning()</span></code></a>, <a class="reference internal" href="../library/logging.html#logging.error" title="logging.error"><code class="xref py py-func docutils literal notranslate"><span class="pre">error()</span></code></a> and <a class="reference internal" href="../library/logging.html#logging.critical" title="logging.critical"><code class="xref py py-func docutils literal notranslate"><span class="pre">critical()</span></code></a>, they will check to see
if no destination is set; and if one is not set, they will set a destination
of the console (<code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code>) and a default format for the displayed
message before delegating to the root logger to do the actual message output.</p>
<p>The default format set by <a class="reference internal" href="../library/logging.html#logging.basicConfig" title="logging.basicConfig"><code class="xref py py-func docutils literal notranslate"><span class="pre">basicConfig()</span></code></a> for messages is:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>severity:logger name:message
</pre></div>
</div>
<p>You can change this by passing a format string to <a class="reference internal" href="../library/logging.html#logging.basicConfig" title="logging.basicConfig"><code class="xref py py-func docutils literal notranslate"><span class="pre">basicConfig()</span></code></a> with the
<em>format</em> keyword argument. For all options regarding how a format string is
constructed, see <a class="reference internal" href="../library/logging.html#formatter-objects"><span class="std std-ref">Formatter Objects</span></a>.</p>
<section id="logging-flow">
<h3>Logging Flow<a class="headerlink" href="#logging-flow" title="Link to this heading">¶</a></h3>
<p>The flow of log event information in loggers and handlers is illustrated in the
following diagram.</p>
<svg width="22cm" height="23cm" viewBox="1 1 439 446" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- Invert color in dark mode -->
<style type="text/css">
svg {
background-color: transparent !important;
}
line {
stroke: #000000;
fill: none;
stroke-opacity: 1;
}
polygon, rect {
fill: none;
stroke: #000000;
fill-opacity: 1;
stroke-opacity: 1;
}
polygon.filled {
fill: #000000;
}
polyline {
fill: none;
stroke-opacity: 1;
stroke: #000000;
}
text {
fill: #000000;
fill-opacity: 1;
stroke: none;
font-family: sans-serif;
font-style: normal;
font-weight: normal;
text-anchor: start;
}
@media (prefers-color-scheme: dark) {
polygon, rect, polyline, line {
stroke: #ffffff;
}
polygon.filled {
fill: #ffffff;
}
text {
fill: #ffffff;
}
image {
filter: invert(100%) hue-rotate(180deg) saturate(1.25);
}
}
/* These rules are for when the theme selector is used, perhaps in contrast to the browser theme. */
body.dark-theme polygon, body.dark-theme rect, body.dark-theme polyline, body.dark-theme line {
stroke: #ffffff;
}
body.dark-theme polygon.filled {
fill: #ffffff;
}
body.dark-theme text {
fill: #ffffff;
}
body.light-theme polygon, body.light-theme rect, body.light-theme polyline, body.light-theme line {
stroke: #000000;
}
body.light-theme polygon.filled {
fill: #000000;
}
body.light-theme text {
fill: #000000;
}
</style>
<defs />
<g id="Background">
<rect style="stroke-width: 1.8; stroke-dasharray: 3;" x="227.504" y="1.91011" width="211.607" height="207.375" rx="0" ry="0" />
<rect style="stroke-width: 1.2;" x="230.966" y="5.62454" width="68.4216" height="21.9166" rx="0" ry="0" />
<rect style="stroke-width: 1.2;" x="5.27912" y="5.70399" width="68.4216" height="21.9166" rx="0" ry="0" />
<text font-size="6.77333" style="font-weight: 700;" x="17.0649" y="19.0071">
<tspan x="17.0649" y="19.0071">Logger flow</tspan>
</text>
<g>
<rect x="81.5533" y="106.469" width="44.45" height="25.9333" rx="0" ry="0" />
<text font-size="6.77333" style="text-anchor: middle;" x="103.778" y="117.256">
<tspan x="103.778" y="117.256">Create</tspan>
<tspan x="103.778" y="125.723" style="font-family: monospace">LogRecord</tspan>
</text>
</g>
<g>
<line x1="103.778" y1="82.8734" x2="103.778" y2="102.351" />
<polygon fill-rule="evenodd" points="103.778,105.351 101.778,101.351 103.778,102.351 105.778,101.351 " />
</g>
<g>
<line x1="103.774" y1="3.65583" x2="103.778" y2="30.3755" />
<polygon fill-rule="evenodd" points="103.778,33.3755 101.778,29.3758 103.778,30.3755 105.778,29.3752 " />
</g>
<text font-size="6.77333" style="text-anchor: middle;" x="144.365" y="10.4604">
<tspan x="143.798" y="10.4604">Logging call in user</tspan>
<tspan x="143.798" y="18.927">code, e.g.</tspan>
</text>
<text font-size="6.77333" style="font-family: monospace;" x="110.837" y="27.4696">
<tspan x="111.893" y="27.4696">logger.info(...)</tspan>
</text>
<g>
<line x1="155.62" y1="58.6834" x2="183.943" y2="58.6924" />
<polygon fill-rule="evenodd" points="186.943,58.6933 182.942,60.6921 183.943,58.6924 182.943,56.6921 " />
</g>
<g>
<rect x="188.061" y="49.9604" width="24.4" height="17.4667" rx="6" ry="6" />
<text font-size="6.77333" style="text-anchor: middle;" x="200.261" y="60.7475">
<tspan x="200.261" y="60.7475">Stop</tspan>
</text>
</g>
<g>
<polygon fill-rule="evenodd" points="103.778,162.482 169.307,193.042 103.778,223.603 38.2493,193.042 " />
<text font-size="6.77333" style="text-anchor: middle;" x="103.778" y="186.629">
<tspan x="103.778" y="188.741">Does a filter attached</tspan>
<tspan x="103.778" y="197.208">to logger reject the</tspan>
<tspan x="103.778" y="205.675">record?</tspan>
</text>
</g>
<g>
<line x1="103.778" y1="132.402" x2="103.778" y2="158.364" />
<polygon class="filled" fill-rule="evenodd" points="103.778,161.364 101.778,157.364 103.778,158.364 105.778,157.364 " />
</g>
<g>
<rect x="75.2033" y="249.478" width="57.15" height="34.4" rx="0" ry="0" />
<text font-size="6.77333" style="text-anchor: middle;" x="103.778" y="260.265">
<tspan x="103.778" y="260.265">Pass record to</tspan>
<tspan x="103.778" y="268.732">handlers of</tspan>
<tspan x="103.778" y="277.198">current logger</tspan>
</text>
</g>
<g>
<polygon fill-rule="evenodd" points="103.778,326.569 158.193,352.399 103.778,378.229 49.3637,352.399 " />
<text font-size="6.77333" style="text-anchor: middle;" x="103.778" y="350.22">
<tspan x="103.778" y="351.276">Is propagate true for</tspan>
<tspan x="103.778" y="359.742">current logger?</tspan>
</text>
</g>
<g>
<polygon fill-rule="evenodd" points="103.778,399.9 150.573,422.994 103.778,446.087 56.984,422.994 " />
<text font-size="6.77333" style="text-anchor: middle;" x="103.778" y="420.814">
<tspan x="103.778" y="422.926">Is there a parent</tspan>
<tspan x="103.778" y="431.393">logger?</tspan>
</text>
</g>
<g>
<rect x="2.43852" y="295.984" width="63.9" height="26.5909" rx="0" ry="0" />
<text font-size="6.77333" style="text-anchor: middle;" x="34.3885" y="307.1">
<tspan x="34.3885" y="307.1">Set current</tspan>
<tspan x="34.3885" y="315.566">logger to parent</tspan>
</text>
</g>
<g>
<polygon fill-rule="evenodd" points="278.422,240.202 330.96,266.686 278.422,293.169 225.885,266.686 " />
<text font-size="6.77333" style="text-anchor: middle;" x="278.422" y="264.506">
<tspan x="278.422" y="266.618">At least one handler</tspan>
<tspan x="278.422" y="275.085">in hierarchy?</tspan>
</text>
</g>
<g>
<rect x="298.963" y="312.257" width="57.75" height="25.9333" rx="0" ry="0" />
<text font-size="6.77333" x="327.838" y="323.044">
<tspan x="301" y="324.100">Use</tspan>
<tspan x="315" y="324.100" style="font-family: monospace">lastResort</tspan>
<tspan x="316" y="332.567">handler</tspan>
</text>
</g>
<g>
<polygon fill-rule="evenodd" points="320.041,35.7307 373.377,60.8536 320.041,85.9765 266.704,60.8536 " />
<text font-size="6.77333" style="text-anchor: middle;" x="320.041" y="58.6741">
<tspan x="320.041" y="58.6741">Handler enabled for</tspan>
<tspan x="320.041" y="67.1407">level of record?</tspan>
</text>
</g>
<g>
<polygon fill-rule="evenodd" points="320.041,105.448 386.002,135.748 320.041,166.047 254.08,135.748 " />
<text font-size="6.77333" style="text-anchor: middle;" x="320.041" y="129.335">
<tspan x="320.041" y="132.508">Does a filter attached</tspan>
<tspan x="320.041" y="140.970">to handler reject the</tspan>
<tspan x="320.041" y="149.436">record?</tspan>
</text>
</g>
<g>
<rect x="409.532" y="52.4436" width="24.4" height="17.4667" rx="6" ry="6" />
<text font-size="6.77333" style="text-anchor: middle;" x="421.732" y="63.2307">
<tspan x="421.732" y="63.2307">Stop</tspan>
</text>
</g>
<g>
<rect x="271.091" y="185.519" width="97.9" height="17.4667" rx="6" ry="6" />
<text font-size="6.77333" style="text-anchor: middle;" x="320.041" y="196.306">
<tspan x="320.041" y="196.306">Emit (includes formatting)</tspan>
</text>
</g>
<text font-size="6.77333" style="font-weight: 700;" x="241.002" y="18.9277">
<tspan x="241.002" y="18.9277">Handler flow</tspan>
</text>
<g>
<polygon fill-rule="evenodd" points="103.778,34.4935 155.62,58.6834 103.778,82.8734 51.9368,58.6834 " />
<text font-size="6.77333" style="text-anchor: middle;" x="103.778" y="56.5039">
<tspan x="103.778" y="57.560">Logger enabled for</tspan>
<tspan x="103.778" y="66.026">level of call?</tspan>
</text>
</g>
<g>
<line x1="103.778" y1="223.603" x2="103.778" y2="245.36" />
<polygon class="filled" fill-rule="evenodd" points="103.778,248.36 101.778,244.36 103.778,245.36 105.778,244.36 " />
</g>
<g>
<line x1="103.778" y1="283.878" x2="103.778" y2="322.451" />
<polygon class="filled" fill-rule="evenodd" points="103.778,325.451 101.778,321.451 103.778,322.451 105.778,321.451 " />
</g>
<g>
<line x1="103.778" y1="378.229" x2="103.778" y2="395.782" />
<polygon class="filled" fill-rule="evenodd" points="103.778,398.782 101.778,394.782 103.778,395.782 105.778,394.782 " />
</g>
<g>
<polyline points="56.984,422.994 34.3885,422.994 34.3885,326.693 " />
<polygon class="filled" fill-rule="evenodd" points="34.3885,323.693 36.3885,327.693 34.3885,326.693 32.3885,327.693 " />
</g>
<g>
<polyline points="34.3885,295.984 34.3885,266.678 71.0853,266.678 " />
<polygon class="filled" fill-rule="evenodd" points="74.0853,266.678 70.0853,268.678 71.0853,266.678 70.0853,264.678 " />
</g>
<g>
<polyline points="150.573,422.994 200.261,422.994 200.261,71.5451 " />
<polygon class="filled" fill-rule="evenodd" points="200.261,68.5451 202.261,72.5451 200.261,71.5451 198.261,72.5451 " />
</g>
<g>
<line style="stroke-dasharray: 5" x1="132.353" y1="266.678" x2="221.767" y2="266.685" />
<polygon class="filled" fill-rule="evenodd" points="224.767,266.686 220.766,268.685 221.767,266.685 220.767,264.685 " />
</g>
<g>
<polyline style="stroke-dasharray: 5" points="278.422,293.169 278.422,325.224 294.845,325.224 " />
<polygon class="filled" fill-rule="evenodd" points="297.845,325.224 293.845,327.224 294.845,325.224 293.845,323.224 " />
</g>
<g>
<polyline points="169.307,193.042 200.261,193.042 200.261,71.5451 " />
<polygon class="filled" fill-rule="evenodd" points="200.261,68.5451 202.261,72.5451 200.261,71.5451 198.261,72.5451 " />
</g>
<g>
<polyline points="158.193,352.399 200.261,352.399 200.261,71.5451 " />
<polygon class="filled" fill-rule="evenodd" points="200.261,68.5451 202.261,72.5451 200.261,71.5451 198.261,72.5451 " />
</g>
<g>
<line x1="319.981" y1="4.27261" x2="320.033" y2="31.6127" />
<polygon class="filled" fill-rule="evenodd" points="320.039,34.6127 318.031,30.6165 320.033,31.6127 322.031,30.6089 " />
</g>
<g>
<line x1="320.041" y1="85.9765" x2="320.041" y2="101.33" />
<polygon class="filled" fill-rule="evenodd" points="320.041,104.33 318.041,100.33 320.041,101.33 322.041,100.33 " />
</g>
<g>
<line x1="320.041" y1="166.047" x2="320.041" y2="181.401" />
<polygon class="filled" fill-rule="evenodd" points="320.041,184.401 318.041,180.401 320.041,181.401 322.041,180.401 " />
</g>
<g>
<polyline points="386.002,135.748 421.732,135.748 421.732,74.0283 " />
<polygon class="filled" fill-rule="evenodd" points="421.732,71.0283 423.732,75.0283 421.732,74.0283 419.732,75.0283 " />
</g>
<g>
<line x1="373.377" y1="60.8536" x2="405.415" y2="61.1401" />
<polygon class="filled" fill-rule="evenodd" points="408.414,61.1669 404.397,63.1311 405.415,61.1401 404.433,59.1312 " />
</g>
<text font-size="6.77333" x="164.96" y="55.5649">
<tspan x="164.96" y="55.5649">No</tspan>
</text>
<text font-size="6.77333" x="106.571" y="97.8453">
<tspan x="106.571" y="97.8453">Yes</tspan>
</text>
<text font-size="6.77333" x="173.856" y="188.452">
<tspan x="173.856" y="188.452">Yes</tspan>
</text>
<text font-size="6.77333" x="107.446" y="239.221">
<tspan x="107.446" y="239.221">No</tspan>
</text>
<text font-size="6.77333" x="174.731" y="349.418">
<tspan x="174.731" y="349.418">No</tspan>
</text>
<text font-size="6.77333" x="106.571" y="390.507">
<tspan x="106.571" y="390.507">Yes</tspan>
</text>
<text font-size="6.77333" x="39.4722" y="417.667">
<tspan x="39.4722" y="417.667">Yes</tspan>
</text>
<text font-size="6.77333" x="174.731" y="417.667">
<tspan x="174.731" y="417.667">No</tspan>
</text>
<text font-size="6.77333" x="281.451" y="313.406">
<tspan x="281.451" y="313.406">No</tspan>
</text>
<text font-size="6.77333" x="333.909" y="263.96">
<tspan x="333.909" y="263.96">Yes</tspan>
</text>
<text font-size="6.77333" x="333.307" y="105.598">
<tspan x="333.307" y="105.598"></tspan>
</text>
<text font-size="6.77333" x="385.766" y="56.9098">
<tspan x="385.766" y="56.9098">No</tspan>
</text>
<text font-size="6.77333" x="333.307" y="105.598">
<tspan x="333.307" y="105.598"></tspan>
</text>
<text font-size="6.77333" x="333.307" y="105.598">
<tspan x="333.307" y="105.598"></tspan>
</text>
<text font-size="6.77333" x="397.102" y="130.471">
<tspan x="397.102" y="130.471">Yes</tspan>
</text>
<text font-size="6.77333" x="323.563" y="178.785">
<tspan x="323.563" y="178.785">No</tspan>
</text>
<text font-size="6.77333" x="333.307" y="105.598">
<tspan x="333.307" y="105.598"></tspan>
</text>
<text font-size="6.77333" x="323.75" y="99.0042">
<tspan x="323.75" y="99.0042">Yes</tspan>
</text>
<text font-size="6.77323" style="text-anchor: middle;" x="355.762" y="18.2449">
<tspan x="355.762" y="18.2449">Record passed</tspan>
<tspan x="355.762" y="26.7116">to handler</tspan>
</text>
<line style="stroke-dasharray: 5" x1="330.96" y1="266.686" x2="377.733" y2="267.908" />
<g>
<polyline style="fill: none; stroke-opacity: 1; stroke-dasharray: 5" points="356.713,325.224 377.733,325.224 377.733,214.711 " />
<polygon class="filled" fill-rule="evenodd" points="377.733,211.711 379.733,215.711 377.733,214.711 375.733,215.711 " />
</g>
</g>
</svg>
<script>
/*
* This snippet is needed to handle the case where a light or dark theme is
* chosen via the theme is selected in the page. We call the existing handler
* and then add a dark-theme class to the body when the dark theme is selected.
* The SVG styling (above) then does the rest.
*
* If the pydoc theme is updated to set the dark-theme class, this snippet
* won't be needed any more.
*/
(function() {
var oldActivateTheme = activateTheme;
function updateBody(theme) {
let elem = document.body;
elem.classList.remove('dark-theme');
elem.classList.remove('light-theme');
if (theme === 'dark') {
elem.classList.add('dark-theme');
}
else if (theme === 'light') {
elem.classList.add('light-theme');
}
}
activateTheme = function(theme) {
oldActivateTheme(theme);
updateBody(theme);
};
/*
* If the page is refreshed, make sure we update the body - the overriding
* of activateTheme won't have taken effect yet.
*/
updateBody(localStorage.getItem('currentTheme') || 'auto');
})();
</script></section>
<section id="loggers">
<h3>Loggers<a class="headerlink" href="#loggers" title="Link to this heading">¶</a></h3>
<p><a class="reference internal" href="../library/logging.html#logging.Logger" title="logging.Logger"><code class="xref py py-class docutils literal notranslate"><span class="pre">Logger</span></code></a> objects have a threefold job. First, they expose several
methods to application code so that applications can log messages at runtime.
Second, logger objects determine which log messages to act upon based upon
severity (the default filtering facility) or filter objects. Third, logger
objects pass along relevant log messages to all interested log handlers.</p>
<p>The most widely used methods on logger objects fall into two categories:
configuration and message sending.</p>
<p>These are the most common configuration methods:</p>
<ul class="simple">
<li><p><a class="reference internal" href="../library/logging.html#logging.Logger.setLevel" title="logging.Logger.setLevel"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Logger.setLevel()</span></code></a> specifies the lowest-severity log message a logger
will handle, where debug is the lowest built-in severity level and critical
is the highest built-in severity. For example, if the severity level is
INFO, the logger will handle only INFO, WARNING, ERROR, and CRITICAL messages
and will ignore DEBUG messages.</p></li>
<li><p><a class="reference internal" href="../library/logging.html#logging.Logger.addHandler" title="logging.Logger.addHandler"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Logger.addHandler()</span></code></a> and <a class="reference internal" href="../library/logging.html#logging.Logger.removeHandler" title="logging.Logger.removeHandler"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Logger.removeHandler()</span></code></a> add and remove
handler objects from the logger object. Handlers are covered in more detail
in <a class="reference internal" href="#handler-basic"><span class="std std-ref">Handlers</span></a>.</p></li>
<li><p><a class="reference internal" href="../library/logging.html#logging.Logger.addFilter" title="logging.Logger.addFilter"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Logger.addFilter()</span></code></a> and <a class="reference internal" href="../library/logging.html#logging.Logger.removeFilter" title="logging.Logger.removeFilter"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Logger.removeFilter()</span></code></a> add and remove filter
objects from the logger object. Filters are covered in more detail in
<a class="reference internal" href="../library/logging.html#filter"><span class="std std-ref">Filter Objects</span></a>.</p></li>
</ul>
<p>You don't need to always call these methods on every logger you create. See the
last two paragraphs in this section.</p>
<p>With the logger object configured, the following methods create log messages:</p>
<ul class="simple">
<li><p><a class="reference internal" href="../library/logging.html#logging.Logger.debug" title="logging.Logger.debug"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Logger.debug()</span></code></a>, <a class="reference internal" href="../library/logging.html#logging.Logger.info" title="logging.Logger.info"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Logger.info()</span></code></a>, <a class="reference internal" href="../library/logging.html#logging.Logger.warning" title="logging.Logger.warning"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Logger.warning()</span></code></a>,
<a class="reference internal" href="../library/logging.html#logging.Logger.error" title="logging.Logger.error"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Logger.error()</span></code></a>, and <a class="reference internal" href="../library/logging.html#logging.Logger.critical" title="logging.Logger.critical"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Logger.critical()</span></code></a> all create log records with
a message and a level that corresponds to their respective method names. The
message is actually a format string, which may contain the standard string
substitution syntax of <code class="docutils literal notranslate"><span class="pre">%s</span></code>, <code class="docutils literal notranslate"><span class="pre">%d</span></code>, <code class="docutils literal notranslate"><span class="pre">%f</span></code>, and so on. The
rest of their arguments is a list of objects that correspond with the
substitution fields in the message. With regard to <code class="docutils literal notranslate"><span class="pre">**kwargs</span></code>, the
logging methods care only about a keyword of <code class="docutils literal notranslate"><span class="pre">exc_info</span></code> and use it to
determine whether to log exception information.</p></li>
<li><p><a class="reference internal" href="../library/logging.html#logging.Logger.exception" title="logging.Logger.exception"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Logger.exception()</span></code></a> creates a log message similar to
<a class="reference internal" href="../library/logging.html#logging.Logger.error" title="logging.Logger.error"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Logger.error()</span></code></a>. The difference is that <code class="xref py py-meth docutils literal notranslate"><span class="pre">Logger.exception()</span></code> dumps a
stack trace along with it. Call this method only from an exception handler.</p></li>
<li><p><a class="reference internal" href="../library/logging.html#logging.Logger.log" title="logging.Logger.log"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Logger.log()</span></code></a> takes a log level as an explicit argument. This is a
little more verbose for logging messages than using the log level convenience