<pre><codeclass="python language-python hljs">logging.basicConfig(filename=<path>, level=<spanclass="hljs-string">'DEBUG'</span>) <spanclass="hljs-comment"># Configures the root logger (see Setup).</span>
logging.debug/info/warning/error/critical(<str>) <spanclass="hljs-comment"># Logs to the root logger.</span>
<Logger> = logging.getLogger(__name__) <spanclass="hljs-comment"># Logger named after the module.</span>
<Logger>.<level>(<str>) <spanclass="hljs-comment"># Logs to the logger.</span>
<Logger>.exception(<str>) <spanclass="hljs-comment"># Error() that appends caught exception.</span>
<pre><codeclass="python language-python hljs">log.basicConfig(filename=<path>, level=<spanclass="hljs-string">'DEBUG'</span>)<spanclass="hljs-comment"># Configures the root logger (see Setup).</span>
log.debug/info/warning/error/critical(<str>)<spanclass="hljs-comment"># Logs to the root logger.</span>
<Logger> = log.getLogger(__name__)<spanclass="hljs-comment"># Logger named after the module.</span>
<Logger>.<level>(<str>) <spanclass="hljs-comment"># Logs to the logger.</span>
<Logger>.exception(<str>) <spanclass="hljs-comment"># Error() that appends caught exception.</span>
filename=<spanclass="hljs-keyword">None</span>, <spanclass="hljs-comment"># Logs to stderr or appends to file.</span>
format=<spanclass="hljs-string">'%(levelname)s:%(name)s:%(message)s'</span>, <spanclass="hljs-comment"># Add '%(asctime)s' for local datetime.</span>
level=log.WARNING,<spanclass="hljs-comment"># Drops messages with lower priority.</span>
handlers=[log.StreamHandler(sys.stderr)]<spanclass="hljs-comment"># Uses FileHandler if filename is set.</span>
)
</code></pre></div>
<pre><codeclass="python language-python hljs"><Formatter> = logging.Formatter(<spanclass="hljs-string">'<format>'</span>) <spanclass="hljs-comment"># Creates a Formatter.</span>
<Handler> = logging.FileHandler(<path>, mode=<spanclass="hljs-string">'a'</span>) <spanclass="hljs-comment"># Creates a Handler. Also `encoding=None`.</span>
<Handler>.setFormatter(<Formatter>) <spanclass="hljs-comment"># Adds Formatter to the Handler.</span>
<Handler>.setLevel(<int/str>) <spanclass="hljs-comment"># Processes all messages by default.</span>
<Logger>.addHandler(<Handler>) <spanclass="hljs-comment"># Adds Handler to the Logger.</span>
<Logger>.setLevel(<int/str>) <spanclass="hljs-comment"># What is sent to its/ancestors' handlers.</span>
<Logger>.propagate = <bool><spanclass="hljs-comment"># Cuts off ancestors' handlers if False.</span>
<pre><codeclass="python language-python hljs"><Formatter> = log.Formatter(<spanclass="hljs-string">'<format>'</span>)<spanclass="hljs-comment"># Creates a Formatter.</span>
<Handler> = log.FileHandler(<path>, mode=<spanclass="hljs-string">'a'</span>)<spanclass="hljs-comment"># Creates a Handler. Also `encoding=None`.</span>
<Handler>.setFormatter(<Formatter>) <spanclass="hljs-comment"># Adds Formatter to the Handler.</span>
<Handler>.setLevel(<int/str>) <spanclass="hljs-comment"># Processes all messages by default.</span>
<Logger>.addHandler(<Handler>) <spanclass="hljs-comment"># Adds Handler to the Logger.</span>
<Logger>.setLevel(<int/str>) <spanclass="hljs-comment"># What is sent to its/ancestors' handlers.</span>
<Logger>.propagate = <bool><spanclass="hljs-comment"># Cuts off ancestors' handlers if False.</span>
</code></pre>
<ul>
<li><strong>Parent logger can be specified by naming the child logger <codeclass="python hljs"><spanclass="hljs-string">'<parent>.<name>'</span></code>.</strong></li>
<li><strong>Formatter also accepts: pathname, filename, funcName, lineno, thread and process.</strong></li>
<li><strong>A <codeclass="python hljs"><spanclass="hljs-string">'handlers.RotatingFileHandler'</span></code> creates and deletes log files based on 'maxBytes' and 'backupCount' arguments.</strong></li>
</ul>
<div><h4id="createsaloggerthatwritesallmessagestofileandsendsthemtotherootshandlerthatprintswarningsorhigher">Creates a logger that writes all messages to file and sends them to the root's handler that prints warnings or higher:</h4><pre><codeclass="python language-python hljs"><spanclass="hljs-meta">>>></span>logger = logging.getLogger(<spanclass="hljs-string">'my_module'</span>)
<div><h4id="createsaloggerthatwritesallmessagestofileandsendsthemtotherootshandlerthatprintswarningsorhigher">Creates a logger that writes all messages to file and sends them to the root's handler that prints warnings or higher:</h4><pre><codeclass="python language-python hljs"><spanclass="hljs-meta">>>></span>logger = log.getLogger(<spanclass="hljs-string">'my_module'</span>)
2023-02-07 23:21:01,430 CRITICAL:my_module:Running out of disk space.
<spanclass="hljs-number">2023</span><spanclass="hljs-number">-02</span><spanclass="hljs-number">-07</span><spanclass="hljs-number">23</span>:<spanclass="hljs-number">21</span>:<spanclass="hljs-number">01</span>,<spanclass="hljs-number">430</span> CRITICAL:my_module:Running out of disk space.
</code></pre></div>
<div><h2id="introspection"><ahref="#introspection"name="introspection">#</a>Introspection</h2><pre><codeclass="python language-python hljs"><list> = dir() <spanclass="hljs-comment"># Names of local vars, functions, classes and modules.</span>