-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy path09-Logging.html
70 lines (64 loc) · 45.9 KB
/
09-Logging.html
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
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title>09-Logging</title>
<script type="text/javascript" src="09-Logging_files/MathJax.js" charset="utf-8"></script>
<script type="text/javascript">
// MathJax disabled, set as null to distingish from *missing* MathJax,
// where it will be undefined, and should prompt a dialog later.
window.mathjax_url = "mathjax/MathJax.js";
</script>
<link rel="stylesheet" href="09-Logging_files/jquery-wijmo.css" type="text/css">
<link rel="stylesheet" href="09-Logging_files/codemirror.css">
<link rel="stylesheet" href="09-Logging_files/markdown.css">
<link rel="stylesheet" href="09-Logging_files/rst.css">
<link rel="stylesheet" href="09-Logging_files/ipython.css">
<link rel="stylesheet" href="09-Logging_files/default.css">
<link rel="stylesheet" href="09-Logging_files/prettify.css">
<link rel="stylesheet" href="09-Logging_files/boilerplate.css" type="text/css">
<link rel="stylesheet" href="09-Logging_files/layout.css" type="text/css">
<link rel="stylesheet" href="09-Logging_files/base.css" type="text/css">
<link rel="stylesheet" href="09-Logging_files/notebook.css" type="text/css">
<link rel="stylesheet" href="09-Logging_files/renderedhtml.css" type="text/css">
<meta name="read_only" content="False">
<style type="text/css">#MathJax_Zoom {position: absolute; background-color: #F0F0F0; overflow: auto; display: block; z-index: 301; padding: .5em; border: 1px solid black; margin: 0; font-family: serif; font-size: 85%; font-weight: normal; font-style: normal; text-align: left; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; box-shadow: 5px 5px 15px #AAAAAA; -webkit-box-shadow: 5px 5px 15px #AAAAAA; -moz-box-shadow: 5px 5px 15px #AAAAAA; -khtml-box-shadow: 5px 5px 15px #AAAAAA; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
#MathJax_ZoomOverlay {position: absolute; left: 0; top: 0; z-index: 300; display: inline-block; width: 100%; height: 100%; border: 0; padding: 0; margin: 0; background-color: white; opacity: 0; filter: alpha(opacity=0)}
</style><style type="text/css">#MathJax_About {position: fixed; left: 50%; width: auto; text-align: center; border: 3px outset; padding: 1em 2em; background-color: #DDDDDD; cursor: default; font-family: message-box; font-size: 120%; font-style: normal; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; z-index: 201; border-radius: 15px; -webkit-border-radius: 15px; -moz-border-radius: 15px; -khtml-border-radius: 15px; box-shadow: 0px 10px 20px #808080; -webkit-box-shadow: 0px 10px 20px #808080; -moz-box-shadow: 0px 10px 20px #808080; -khtml-box-shadow: 0px 10px 20px #808080; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
.MathJax_Menu {position: absolute; background-color: white; color: black; width: auto; padding: 5px 0px; border: 1px solid #CCCCCC; margin: 0; cursor: default; font: menu; text-align: left; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; z-index: 201; border-radius: 5px; -webkit-border-radius: 5px; -moz-border-radius: 5px; -khtml-border-radius: 5px; box-shadow: 0px 10px 20px #808080; -webkit-box-shadow: 0px 10px 20px #808080; -moz-box-shadow: 0px 10px 20px #808080; -khtml-box-shadow: 0px 10px 20px #808080; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
.MathJax_MenuItem {padding: 1px 2em; background: transparent}
.MathJax_MenuTitle {background-color: #CCCCCC; margin: -5px 0 0 0; text-align: center; font-style: italic; font-size: 80%; color: #444444; padding: 2px 0; overflow: hidden}
.MathJax_MenuArrow {position: absolute; right: .5em; color: #666666}
.MathJax_MenuActive .MathJax_MenuArrow {color: white}
.MathJax_MenuCheck {position: absolute; left: .7em}
.MathJax_MenuRadioCheck {position: absolute; left: .7em}
.MathJax_MenuLabel {padding: 1px 2em 3px 1.33em; font-style: italic}
.MathJax_MenuRule {border-top: 1px solid #DDDDDD; margin: 4px 3px}
.MathJax_MenuDisabled {color: GrayText}
.MathJax_MenuActive {background-color: #606872; color: white}
</style><style type="text/css">#MathJax_Message {position: fixed; left: 1px; bottom: 2px; background-color: #E6E6E6; border: 1px solid #959595; margin: 0px; padding: 2px 8px; z-index: 102; color: black; font-size: 80%; width: auto; white-space: nowrap}
#MathJax_MSIE_Frame {position: absolute; top: 0; left: 0; width: 0px; z-index: 101; border: 0px; margin: 0px; padding: 0px}
.MathJax_Error {color: #CC0000; font-style: italic}
</style><style type="text/css">@media print { body { overflow: visible !important; } }.ui-widget-content { border: 0px; }</style><style type="text/css">#MathJax_Zoom {position: absolute; background-color: #F0F0F0; overflow: auto; display: block; z-index: 301; padding: .5em; border: 1px solid black; margin: 0; font-family: serif; font-size: 85%; font-weight: normal; font-style: normal; text-align: left; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; box-shadow: 5px 5px 15px #AAAAAA; -webkit-box-shadow: 5px 5px 15px #AAAAAA; -moz-box-shadow: 5px 5px 15px #AAAAAA; -khtml-box-shadow: 5px 5px 15px #AAAAAA; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
#MathJax_ZoomOverlay {position: absolute; left: 0; top: 0; z-index: 300; display: inline-block; width: 100%; height: 100%; border: 0; padding: 0; margin: 0; background-color: white; opacity: 0; filter: alpha(opacity=0)}
</style><style type="text/css">#MathJax_About {position: fixed; left: 50%; width: auto; text-align: center; border: 3px outset; padding: 1em 2em; background-color: #DDDDDD; cursor: default; font-family: message-box; font-size: 120%; font-style: normal; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; z-index: 201; border-radius: 15px; -webkit-border-radius: 15px; -moz-border-radius: 15px; -khtml-border-radius: 15px; box-shadow: 0px 10px 20px #808080; -webkit-box-shadow: 0px 10px 20px #808080; -moz-box-shadow: 0px 10px 20px #808080; -khtml-box-shadow: 0px 10px 20px #808080; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
.MathJax_Menu {position: absolute; background-color: white; color: black; width: auto; padding: 5px 0px; border: 1px solid #CCCCCC; margin: 0; cursor: default; font: menu; text-align: left; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; z-index: 201; border-radius: 5px; -webkit-border-radius: 5px; -moz-border-radius: 5px; -khtml-border-radius: 5px; box-shadow: 0px 10px 20px #808080; -webkit-box-shadow: 0px 10px 20px #808080; -moz-box-shadow: 0px 10px 20px #808080; -khtml-box-shadow: 0px 10px 20px #808080; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
.MathJax_MenuItem {padding: 1px 2em; background: transparent}
.MathJax_MenuTitle {background-color: #CCCCCC; margin: -5px 0 0 0; text-align: center; font-style: italic; font-size: 80%; color: #444444; padding: 2px 0; overflow: hidden}
.MathJax_MenuArrow {position: absolute; right: .5em; color: #666666}
.MathJax_MenuActive .MathJax_MenuArrow {color: white}
.MathJax_MenuCheck {position: absolute; left: .7em}
.MathJax_MenuRadioCheck {position: absolute; left: .7em}
.MathJax_MenuLabel {padding: 1px 2em 3px 1.33em; font-style: italic}
.MathJax_MenuRule {border-top: 1px solid #DDDDDD; margin: 4px 3px}
.MathJax_MenuDisabled {color: GrayText}
.MathJax_MenuActive {background-color: #606872; color: white}
</style><style type="text/css">#MathJax_Message {position: fixed; left: 1px; bottom: 2px; background-color: #E6E6E6; border: 1px solid #959595; margin: 0px; padding: 2px 8px; z-index: 102; color: black; font-size: 80%; width: auto; white-space: nowrap}
#MathJax_MSIE_Frame {position: absolute; top: 0; left: 0; width: 0px; z-index: 101; border: 0px; margin: 0px; padding: 0px}
.MathJax_Error {color: #CC0000; font-style: italic}
</style></head><body style="overflow: auto;"><div tabindex="2" class="cell text_cell border-box-sizing ui-widget-content ui-corner-all"><div style="display: none;" class="text_cell_input"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-default"><div style="position: relative; height: 4px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre>x</pre></div><div style="position: relative"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative" draggable="true"><pre class="CodeMirror-cursor"> </pre><div></div></div></div></div></div></div></div></div><div tabindex="-1" class="text_cell_render rendered_html"><h1>Logging</h1>
<p>Python provides a standard and configurable logging facility. You can set up the collection
of loggers & handlers separately from their actual <em>use</em> in your program.</p></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [1]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 100px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>log = logging.getLogger()</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-keyword">import</span><span class="cm-null"> </span><span class="cm-variable">logging</span></pre><pre><span class="cm-variable">logging.basicConfig</span><span class="cm-null">()</span></pre><pre> </pre><pre><span class="cm-variable">log</span><span class="cm-null"> = </span><span class="cm-variable">logging.getLogger</span><span class="cm-null">()</span></pre><pre><span class="cm-variable">log</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt output_prompt">Out[1]:</div><div class="box_flex1 output_subarea output_text"><pre><logging.RootLogger at 0x25a34d0></pre></div></div></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [2]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 47px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>log.log(logging.CRITICAL, 'This is a critical message')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">log.log</span><span class="cm-null">(</span><span class="cm-variable">logging.CRITICAL</span><span class="cm-null">, </span><span class="cm-string">'This is a critical message'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">log.log</span><span class="cm-null">(</span><span class="cm-variable">logging.FATAL</span><span class="cm-null">, </span><span class="cm-string">'This is a fatal message'</span><span class="cm-null">)</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stderr"><pre>CRITICAL:root:This is a critical messageCRITICAL:root:This is a fatal message</pre></div></div></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [3]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 30px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>logging.CRITICAL, logging.ERROR, logging.WARN, logging.INFO, logging.DEBUG</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">logging.CRITICAL</span><span class="cm-null">, </span><span class="cm-variable">logging.ERROR</span><span class="cm-null">, </span><span class="cm-variable">logging.WARN</span><span class="cm-null">, </span><span class="cm-variable">logging.INFO</span><span class="cm-null">, </span><span class="cm-variable">logging.DEBUG</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt output_prompt">Out[3]:</div><div class="box_flex1 output_subarea output_text"><pre>(50, 40, 30, 20, 10)</pre></div></div></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [4]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 30px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>log.critical('This is critical')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">log.critical</span><span class="cm-null">(</span><span class="cm-string">'This is critical'</span><span class="cm-null">)</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stderr"><pre>CRITICAL:root:This is critical</pre></div></div></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [5]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 82px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>log.error('This is an error')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">log.error</span><span class="cm-null">(</span><span class="cm-string">'This is an error'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">log.warn</span><span class="cm-null">(</span><span class="cm-string">'This is a warning'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">log.info</span><span class="cm-null">(</span><span class="cm-string">'This is info'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">log.debug</span><span class="cm-null">(</span><span class="cm-string">'This is debug'</span><span class="cm-null">)</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stderr"><pre>ERROR:root:This is an errorWARNING:root:This is a warning</pre></div></div></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [6]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 30px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>log.setLevel(logging.DEBUG)</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">log.setLevel</span><span class="cm-null">(</span><span class="cm-variable">logging.DEBUG</span><span class="cm-null">)</span></pre></div></div></div></div></div></div></div></div></div><div style="display: none;" class="output vbox"></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [7]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 82px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>log.error('This is an error')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">log.error</span><span class="cm-null">(</span><span class="cm-string">'This is an error'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">log.warn</span><span class="cm-null">(</span><span class="cm-string">'This is a warning'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">log.info</span><span class="cm-null">(</span><span class="cm-string">'This is info'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">log.debug</span><span class="cm-null">(</span><span class="cm-string">'This is debug'</span><span class="cm-null">)</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stderr"><pre>ERROR:root:This is an errorWARNING:root:This is a warningINFO:root:This is infoDEBUG:root:This is debug</pre></div></div></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [8]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 30px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>log.info('This is a message with an argument %r', 'The argument')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">log.info</span><span class="cm-null">(</span><span class="cm-string">'This is a message with an argument %r'</span><span class="cm-null">, </span><span class="cm-string">'The argument'</span><span class="cm-null">)</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stderr"><pre>INFO:root:This is a message with an argument 'The argument'</pre></div></div></div></div><div tabindex="2" class="cell text_cell border-box-sizing"><div style="display: none;" class="text_cell_input"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-default"><div style="position: relative; height: 4px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre>x</pre></div><div style="position: relative"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative" draggable="true"><pre class="CodeMirror-cursor"> </pre><div></div></div></div></div></div></div></div></div><div tabindex="-1" class="text_cell_render rendered_html"><h1>Sub-loggers</h1>
<p>We can configure "child loggers" of the root logger by passing a name to the <code>getLogger</code>
function:</p></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [9]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 100px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>root.debug('The root logger will print debug information')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">root</span><span class="cm-null"> = </span><span class="cm-variable">logging.getLogger</span><span class="cm-null">()</span></pre><pre><span class="cm-variable">mylogger</span><span class="cm-null"> = </span><span class="cm-variable">logging.getLogger</span><span class="cm-null">(</span><span class="cm-string">'mylogger'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">mylogger.setLevel</span><span class="cm-null">(</span><span class="cm-variable">logging.INFO</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">root.debug</span><span class="cm-null">(</span><span class="cm-string">'The root logger will print debug information'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">mylogger.debug</span><span class="cm-null">(</span><span class="cm-string">'mylogger will not'</span><span class="cm-null">)</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stderr"><pre>DEBUG:root:The root logger will print debug information</pre></div></div></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [10]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 65px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>mylogger.info('Information will propagate up to other loggers')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">mylogger.info</span><span class="cm-null">(</span><span class="cm-string">'Information will propagate up to other loggers'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">mylogger.propagate</span><span class="cm-null"> = </span><span class="cm-number">0</span></pre><pre><span class="cm-variable">mylogger.info</span><span class="cm-null">(</span><span class="cm-string">'But not if we set propagate to 0'</span><span class="cm-null">)</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stderr"><pre>INFO:mylogger:Information will propagate up to other loggersNo handlers could be found for logger "mylogger"</pre></div></div></div></div><div tabindex="2" class="cell text_cell border-box-sizing"><div style="display: none;" class="text_cell_input"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-default"><div style="position: relative; height: 1px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre>x</pre></div><div style="position: relative"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative" draggable="true"><pre class="CodeMirror-cursor"> </pre><div></div></div></div></div></div></div></div></div><div tabindex="-1" class="text_cell_render rendered_html"><h2>Handlers and formatters</h2></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [11]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 65px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>mylogger.info('Now this is being handled by mylogger')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">handler</span><span class="cm-null"> = </span><span class="cm-variable">logging.StreamHandler</span><span class="cm-null">()</span></pre><pre><span class="cm-variable">mylogger.handlers</span><span class="cm-null"> = [</span><span class="cm-variable">handler</span><span class="cm-null">]</span></pre><pre><span class="cm-variable">mylogger.info</span><span class="cm-null">(</span><span class="cm-string">'Now this is being handled by mylogger'</span><span class="cm-null">)</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stderr"><pre>Now this is being handled by mylogger</pre></div></div></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [12]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 47px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>mylogger.info('Now this is suppressed by the handler')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">handler.setLevel</span><span class="cm-null">(</span><span class="cm-variable">logging.WARN</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">mylogger.info</span><span class="cm-null">(</span><span class="cm-string">'Now this is suppressed by the handler'</span><span class="cm-null">)</span></pre></div></div></div></div></div></div></div></div></div><div style="display: none;" class="output vbox"></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [13]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 65px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>handler.formatter = logging.Formatter('%(levelname)s:%(message)s')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">handler.setLevel</span><span class="cm-null">(</span><span class="cm-variable">logging.INFO</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">handler.formatter</span><span class="cm-null"> = </span><span class="cm-variable">logging.Formatter</span><span class="cm-null">(</span><span class="cm-string">'%(levelname)s:%(message)s'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">mylogger.info</span><span class="cm-null">(</span><span class="cm-string">'This is a message'</span><span class="cm-null">)</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stderr"><pre>INFO:This is a message</pre></div></div></div></div><div tabindex="2" class="cell text_cell border-box-sizing"><div style="display: none;" class="text_cell_input"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-default"><div style="position: relative; height: 1px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre>x</pre></div><div style="position: relative"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative" draggable="true"><pre class="CodeMirror-cursor"> </pre><div></div></div></div></div></div></div></div></div><div tabindex="-1" class="text_cell_render rendered_html"><p>If we set propagate back to 1, we'll see "doubled" messages:</p></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [14]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 47px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>mylogger.info('Hello, there')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">mylogger.propagate</span><span class="cm-null"> = </span><span class="cm-number">1</span></pre><pre><span class="cm-variable">mylogger.info</span><span class="cm-null">(</span><span class="cm-string">'Hello, there'</span><span class="cm-null">)</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stderr"><pre>INFO:Hello, thereINFO:mylogger:Hello, there</pre></div></div></div></div><div tabindex="2" class="cell text_cell border-box-sizing"><div style="display: none;" class="text_cell_input"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-default"><div style="position: relative; height: 1px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre>x</pre></div><div style="position: relative"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative" draggable="true"><pre class="CodeMirror-cursor"> </pre><div></div></div></div></div></div></div></div></div><div tabindex="-1" class="text_cell_render rendered_html"><h2>Logging Configuration</h2></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [27]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 751px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span> 'format': '%(asctime)s %(levelname)-8s %(name)-15s %(message)s',</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-keyword">import</span><span class="cm-null"> </span><span class="cm-variable">sys</span></pre><pre><span class="cm-keyword">import</span><span class="cm-null"> </span><span class="cm-variable">logging.config</span></pre><pre> </pre><pre> </pre><pre><span class="cm-variable">config</span><span class="cm-null"> = {</span></pre><pre><span class="cm-null"> </span><span class="cm-string">'version'</span><span class="cm-null">: </span><span class="cm-number">1</span><span class="cm-null">,</span></pre><pre><span class="cm-null"> </span><span class="cm-string">'loggers'</span><span class="cm-null">: {</span></pre><pre><span class="cm-null"> </span><span class="cm-string">'root'</span><span class="cm-null">: {</span></pre><pre><span class="cm-null"> </span><span class="cm-string">'level'</span><span class="cm-null">: </span><span class="cm-variable">logging.ERROR</span><span class="cm-null">,</span></pre><pre><span class="cm-null"> </span><span class="cm-string">'handlers'</span><span class="cm-null">: [</span><span class="cm-string">'stream'</span><span class="cm-null"> ] },</span></pre><pre><span class="cm-null"> </span><span class="cm-string">'mylogger2'</span><span class="cm-null">: {</span></pre><pre><span class="cm-null"> </span><span class="cm-string">'level'</span><span class="cm-null">: </span><span class="cm-variable">logging.INFO</span><span class="cm-null">,</span></pre><pre><span class="cm-null"> </span><span class="cm-string">'handlers'</span><span class="cm-null">: [ </span><span class="cm-string">'stream'</span><span class="cm-null">, </span><span class="cm-string">'file'</span><span class="cm-null">] } },</span></pre><pre><span class="cm-null"> </span><span class="cm-string">'handlers'</span><span class="cm-null">: {</span></pre><pre><span class="cm-null"> </span><span class="cm-string">'stream'</span><span class="cm-null">: {</span></pre><pre><span class="cm-null"> </span><span class="cm-string">'class'</span><span class="cm-null">: </span><span class="cm-string">'logging.StreamHandler'</span><span class="cm-null">,</span></pre><pre><span class="cm-null"> </span><span class="cm-string">'formatter'</span><span class="cm-null">: </span><span class="cm-string">'basic'</span><span class="cm-null">,</span></pre><pre><span class="cm-null"> </span><span class="cm-string">'stream'</span><span class="cm-null">: </span><span class="cm-variable">sys.stdout</span><span class="cm-null"> },</span></pre><pre><span class="cm-null"> </span><span class="cm-string">'file'</span><span class="cm-null">: {</span></pre><pre><span class="cm-null"> </span><span class="cm-string">'class'</span><span class="cm-null">: </span><span class="cm-string">'logging.FileHandler'</span><span class="cm-null">,</span></pre><pre><span class="cm-null"> </span><span class="cm-string">'formatter'</span><span class="cm-null">: </span><span class="cm-string">'precise'</span><span class="cm-null">,</span></pre><pre><span class="cm-null"> </span><span class="cm-string">'filename'</span><span class="cm-null">: </span><span class="cm-string">'/tmp/logfile.log'</span><span class="cm-null">,</span></pre><pre><span class="cm-null"> </span><span class="cm-string">'mode'</span><span class="cm-null">: </span><span class="cm-string">'w'</span><span class="cm-null"> } },</span></pre><pre><span class="cm-null"> </span><span class="cm-string">'formatters'</span><span class="cm-null">: {</span></pre><pre><span class="cm-null"> </span><span class="cm-string">'basic'</span><span class="cm-null">: {</span></pre><pre><span class="cm-null"> </span><span class="cm-string">'format'</span><span class="cm-null">: </span><span class="cm-string">'%(levelname)-8s %(message)s'</span><span class="cm-null"> },</span></pre><pre><span class="cm-null"> </span><span class="cm-string">'precise'</span><span class="cm-null">: {</span></pre><pre><span class="cm-null"> </span><span class="cm-string">'format'</span><span class="cm-null">: </span><span class="cm-string">'%(asctime)s %(levelname)-8s %(name)-15s %(message)s'</span><span class="cm-null">,</span></pre><pre><span class="cm-null"> </span><span class="cm-string">'datefmt'</span><span class="cm-null">: </span><span class="cm-string">'%Y-%m-%d %H:%M:%S'</span><span class="cm-null"> } }</span></pre><pre><span class="cm-null">}</span></pre><pre> </pre><pre> </pre><pre><span class="cm-variable">logging.config.dictConfig</span><span class="cm-null">(</span><span class="cm-variable">config</span><span class="cm-null">)</span></pre><pre> </pre><pre><span class="cm-variable">root</span><span class="cm-null"> = </span><span class="cm-variable">logging.getLogger</span><span class="cm-null">()</span></pre><pre><span class="cm-variable">mylogger2</span><span class="cm-null"> = </span><span class="cm-variable">logging.getLogger</span><span class="cm-null">(</span><span class="cm-string">'mylogger2'</span><span class="cm-null">)</span></pre><pre> </pre><pre><span class="cm-variable">root.error</span><span class="cm-null">(</span><span class="cm-string">'error from root'</span><span class="cm-null">)</span></pre><pre><span class="cm-keyword">print</span></pre><pre><span class="cm-variable">mylogger2.error</span><span class="cm-null">(</span><span class="cm-string">'error from mylogger'</span><span class="cm-null">)</span></pre><pre><span class="cm-keyword">print</span></pre><pre><span class="cm-variable">mylogger2.info</span><span class="cm-null">(</span><span class="cm-string">'info from mylogger'</span><span class="cm-null">)</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stderr"><pre>ERROR:root:error from root</pre></div></div><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stdout"><pre>ERROR error from mylogger</pre></div></div><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stderr"><pre>ERROR:mylogger2:error from mylogger</pre></div></div><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stdout"><pre>INFO info from mylogger</pre></div></div><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stderr"><pre>INFO:mylogger2:info from mylogger</pre></div></div></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [28]:</div><div class="input_area box-flex1"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 30px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>print open('/tmp/logfile.log').read()</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-keyword">print</span><span class="cm-null"> </span><span class="cm-variable">open</span><span class="cm-null">(</span><span class="cm-string">'/tmp/logfile.log'</span><span class="cm-null">)</span><span class="cm-variable">.read</span><span class="cm-null">()</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stdout"><pre>2012-10-07 16:47:15 ERROR mylogger2 error from mylogger
2012-10-07 16:47:15 INFO mylogger2 info from mylogger
</pre></div></div></div></div><div style="height: 30%;" class="end_space"></div></body></html>