<pre><codeclass="python language-python hljs">logger.add(<spanclass="hljs-string">'debug_{time}.log'</span>, colorize=<spanclass="hljs-keyword">True</span>) <spanclass="hljs-comment"># Connects a log file.</span>
logger.add(<spanclass="hljs-string">'error_{time}.log'</span>, level=<spanclass="hljs-string">'ERROR'</span>) <spanclass="hljs-comment"># Another file for errors or higher.</span>
logger.<level>(<spanclass="hljs-string">'A logging message.'</span>) <spanclass="hljs-comment"># Logs to file/s and prints to stderr.</span>
<pre><codeclass="python language-python hljs">logging.basicConfig(filename=<path>) <spanclass="hljs-comment"># Configures the root logger.</span>
logging.debug/info/warning/error/critical(<str>) <spanclass="hljs-comment"># Logs to the root logger.</span>
<Logger> = logging.getLogger(__name__) <spanclass="hljs-comment"># Creates a logger named after the module.</span>
<Logger>.<level>(<str>) <spanclass="hljs-comment"># All messages propagate to the root logger.</span>
<Logger>.exception(<str>) <spanclass="hljs-comment"># Appends caught exception and calls error().</span>
<div><h3id="exceptions-2">Exceptions</h3><p><strong>Exception description, stack trace and values of variables are appended automatically.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">try</span>:
<div><h3id="rotation">Rotation</h3><p><strong>Argument that sets a condition when a new log file is created.</strong></p><pre><codeclass="python language-python hljs">rotation=<int>|<datetime.timedelta>|<datetime.time>|<str>
filename=<spanclass="hljs-keyword">None</span>, <spanclass="hljs-comment"># Logs to console by default.</span>
format=<spanclass="hljs-string">'%(levelname)s:%(name)s:%(message)s'</span>, <spanclass="hljs-comment"># Add `%(asctime)s` for datetime.</span>
level=logging.WARNING, <spanclass="hljs-comment"># Drops messages with lower priority.</span>
handlers=[logging.StreamHandler()] <spanclass="hljs-comment"># [FileHandler(filename)] 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>) <spanclass="hljs-comment"># Creates a Handler.</span>
<Handler>.setFormatter(<Formatter>) <spanclass="hljs-comment"># Adds Formatter to the Handler.</span>
<Handler>.setLevel(<str/int>) <spanclass="hljs-comment"># Processes all messages by default. </span>
<Logger>.addHandler(<Handler>) <spanclass="hljs-comment"># Adds Handler to the Logger.</span>
<Logger>.setLevel(<str/int>) <spanclass="hljs-comment"># What is sent to handlers and parents.</span>
</code></pre>
<ul>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'<int>'</span></code> - Max file size in bytes.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'<timedelta>'</span></code> - Max age of a file.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'<time>'</span></code> - Time of day.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'<str>'</span></code> - Any of above as a string: <codeclass="python hljs"><spanclass="hljs-string">'100 MB'</span></code>, <codeclass="python hljs"><spanclass="hljs-string">'1 month'</span></code>, <codeclass="python hljs"><spanclass="hljs-string">'monday at 12:00'</span></code>, …</strong></li>
<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 supports: 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><h3id="retention">Retention</h3><p><strong>Sets a condition which old log files get deleted.</strong></p><pre><codeclass="python language-python hljs">retention=<int>|<datetime.timedelta>|<str>
<div><h4id="createsaloggerthatwritesallmessagestoafileandsendsthemtotherootloggerthatprintstostdout">Creates a logger that writes all messages to a file and sends them to the root logger that prints to stdout:</h4><pre><codeclass="python language-python hljs"><spanclass="hljs-meta">>>></span>logging.basicConfig(level=<spanclass="hljs-string">'WARNING'</span>)
<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>
<ul>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'<int>'</span></code> - Max number of files.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'<timedelta>'</span></code> - Max age of a file.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'<str>'</span></code> - Max age as a string: <codeclass="python hljs"><spanclass="hljs-string">'1 week, 3 days'</span></code>, <codeclass="python hljs"><spanclass="hljs-string">'2 months'</span></code>, …</strong></li>
</ul>
<div><h2id="scraping"><ahref="#scraping"name="scraping">#</a>Scraping</h2><div><h4id="scrapespythonsurlversionnumberandlogofromitswikipediapage">Scrapes Python's URL, version number and logo from its Wikipedia page:</h4><pre><codeclass="python language-python hljs"><spanclass="hljs-comment"># $ pip3 install requests beautifulsoup4</span>