<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>logging Archives - Learning to Pi</title>
	<atom:link href="https://www.learningtopi.com/tag/logging/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.learningtopi.com/tag/logging/</link>
	<description>Programming Python on SBC&#039;s and Microcontrollers</description>
	<lastBuildDate>Sat, 27 May 2023 05:38:13 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://www.learningtopi.com/wp-content/uploads/2022/04/cropped-circuit-150x150.png</url>
	<title>logging Archives - Learning to Pi</title>
	<link>https://www.learningtopi.com/tag/logging/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Python logging_handler released!</title>
		<link>https://www.learningtopi.com/sbc/raspberry-pi/python_logging_handler/</link>
					<comments>https://www.learningtopi.com/sbc/raspberry-pi/python_logging_handler/#respond</comments>
		
		<dc:creator><![CDATA[tdunteman]]></dc:creator>
		<pubDate>Mon, 14 Nov 2022 22:00:26 +0000</pubDate>
				<category><![CDATA[Raspberry Pi SBC]]></category>
		<category><![CDATA[SBC]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[logging_handler]]></category>
		<category><![CDATA[python]]></category>
		<guid isPermaLink="false">https://www.learningtopi.com/?p=275</guid>

					<description><![CDATA[<p>The &#8220;logging_handler&#8221; provides a quick and easy way to create a logging object in Python. The Python logging is an incredibly powerful tool, however the options are extensive and the defaults I find to be less than adaquate. The the code in this library I have been using for years on every Python script I...</p>
<p>The post <a href="https://www.learningtopi.com/sbc/raspberry-pi/python_logging_handler/">Python logging_handler released!</a> appeared first on <a href="https://www.learningtopi.com">Learning to Pi</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>The &#8220;logging_handler&#8221; provides a quick and easy way to create a logging object in Python.  The Python logging is an incredibly powerful tool, however the options are extensive and the defaults I find to be less than adaquate.  The the code in this library I have been using for years on every Python script I have written.</p>



<table style="border-collapse: collapse; width: 100%; height: 110px;">
<tbody>
<tr style="height: 37px;">
<td style="width: 5.83331%; height: 37px;"><a href="https://www.learningtopi.com/python-modules-applications/python_logging_handler/"><img decoding="async" class="alignnone wp-image-24" src="https://www.learningtopi.com/wp-content/uploads/2022/04/cropped-circuit-300x300.png" alt="" width="32" height="32" srcset="https://www.learningtopi.com/wp-content/uploads/2022/04/cropped-circuit-300x300.png 300w, https://www.learningtopi.com/wp-content/uploads/2022/04/cropped-circuit-150x150.png 150w, https://www.learningtopi.com/wp-content/uploads/2022/04/cropped-circuit.png 512w" sizes="(max-width: 32px) 100vw, 32px" /></a></td>
<td style="width: 94.1667%; height: 37px;">Module Homepage: <a href="https://www.learningtopi.com/python-modules-applications/python_logging_handler/">Python logging_handler</a></td>
</tr>
<tr style="height: 37px;">
<td style="width: 5.83331%; height: 37px;"><a href="https://github.com/LearningToPi/logging_handler"><img decoding="async" class="alignnone wp-image-277 size-full" src="https://www.learningtopi.com/wp-content/uploads/GitHub-Mark-32px.png" alt="" width="32" height="32" /></a></td>
<td style="width: 94.1667%; height: 37px;">Git: <a style="pointer-events: none;" href="https://github.com/LearningToPi/logging_handler" target="_blank" rel="noreferrer noopener" data-rich-text-format-boundary="true">https://github.com/LearningToPi/logging_handler</a></td>
</tr>
<tr style="height: 36px;">
<td style="width: 5.83331%; height: 36px;"><a href="https://pypi.org/project/logging-handler/"><img decoding="async" class="alignnone wp-image-279" src="https://www.learningtopi.com/wp-content/uploads/python-logo-only-248x300.png" alt="" width="26" height="32" srcset="https://www.learningtopi.com/wp-content/uploads/python-logo-only-248x300.png 248w, https://www.learningtopi.com/wp-content/uploads/python-logo-only-124x150.png 124w, https://www.learningtopi.com/wp-content/uploads/python-logo-only.png 269w" sizes="(max-width: 26px) 100vw, 26px" /></a></td>
<td style="width: 94.1667%; height: 36px;">PyPi: <a href="https://pypi.org/project/logging-handler/" target="_blank" rel="noreferrer noopener">https://pypi.org/project/logging-handler/</a></td>
</tr>
</tbody>
</table>



<h2 class="wp-block-heading">Install logging_handler</h2>



<p>The latest release can be downloaded from the Git Repo (<a href="https://github.com/LearningToPi/logging_handler/releases" target="_blank" rel="noreferrer noopener">https://github.com/LearningToPi/logging_handler/releases</a>) or from PyPi (<a href="https://pypi.org/project/logging-handler/#files" target="_blank" rel="noreferrer noopener">https://pypi.org/project/logging-handler/#files</a>).  I keep releases in both locations up to date.  You can also install directly using Pip:</p>



<pre class="wp-block-code"><code>pip3 install -U logging_handler</code></pre>



<p>(NOTE: -U will upgrade if a newer version is available)</p>



<h2 class="wp-block-heading">Why I Created logging_handler</h2>



<p>I always make sure I have a good reason for publishing code that I use.  This particular library has a single function &#8220;create_logger&#8217; that creates a python logging object using some standard output that I find far more useful than the default logging settings.  You may ask, why not just use print()?  I frequently use theaded or multiprocess scripts, and have found the python logging to be incredibly powerful, but somewhat difficult to manage.</p>



<pre class="wp-block-code"><code>Python 3.9.2 (default, Mar 12 2021, 04:06:34) 
&#91;GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; <strong>import logging</strong>
&gt;&gt;&gt; <strong>logging.warning('this is a warning!')</strong>
<mark>WARNING:root:this is a warning!</mark>
&gt;&gt;&gt; <strong>logging.info('just useful info')</strong>
&gt;&gt;&gt; <strong>logging.debug('debug message!')</strong>
&gt;&gt;&gt; </code></pre>



<p>With the default logging configuraiton shown above, we can see that only the &#8220;warning&#8221; message was printed on the screen.  By default only warning and above messages are output on the screen.  Also, you&#8217;ll notice that the message is tagged as &#8220;root&#8221;, and there is no timestamp!  Reviewing logs without any timestamp I&#8217;ve found to be less than useful.  </p>



<p>This was the initial driver to make adding timestamps quick and easy.  Once I had timestamps, adding logging to the console as well as to file (using different logging levels) was the next add.  I also found cases with Raspberry Pi&#8217;s that logging to the local syslog facility was beneficial and would allow me to correlate errors with system events (and also let me aggregate the script messages on my central syslog server).  The last addition was to create logs based on the date and handle rotation (this was specifically added for a script that ran daily that had a large amount of output that I didn&#8217;t want to store forever).</p>



<h2 class="wp-block-heading">How It Works</h2>



<p>Using the create_logger function from logging_handler is quick and simple.  Simply import the function (you may also the level names DEBUG, INFO, WARNING, ERROR, and CRITICAL as well, but this is optional).  The function will configure the logger with console / file / syslog logging and return it.  The returned logger is simply a Python logging object that can be used just like the example above.</p>



<pre class="wp-block-code"><code>&gt;&gt;&gt; <strong>from logging_handler import create_logger</strong>
&gt;&gt;&gt; <strong>logger = create_logger(console_level='DEBUG')</strong>
&gt;&gt;&gt; <strong>logger.warning('this is a warning!')</strong>
<mark>2022-11-10 13:46:43,371 - root - WARNING - this is a warning!</mark>
&gt;&gt;&gt; <strong>logger.info('just useful info')</strong>
<mark>2022-11-10 13:46:52,419 - root - INFO - just useful info</mark>
&gt;&gt;&gt; <strong>logger.debug('debug message!')</strong>
<mark>2022-11-10 13:47:10,435 - root - DEBUG - debug message!</mark></code></pre>



<p>We can also get more complex and add logging to file and syslog:</p>



<pre class="wp-block-code"><code>&gt;&gt;&gt; from logging_handler import create_logger
&gt;&gt;&gt; logger = create_logger(console_level='DEBUG', log_file='test.log', file_level='INFO', syslog=True)
&gt;&gt;&gt; 
&gt;&gt;&gt; logger.warning('this is a warning!')
2022-11-10 13:49:41,264 - root - WARNING - this is a warning!
&gt;&gt;&gt; logger.info('just useful info')
2022-11-10 13:49:47,901 - root - INFO - just useful info
&gt;&gt;&gt; logger.debug('debug message!')
2022-11-10 13:49:53,290 - root - DEBUG - debug message!
&gt;&gt;&gt; quit()</code></pre>



<p>The log file and syslog will now contain the messages that were at or above the file level (syslog uses the same setting as the file_level):</p>



<pre class="wp-block-code"><code>(venv) pi@raspberrypi:~ $ <strong>cat test.log </strong>
<mark>2022-11-10 13:49:41,264 - root - WARNING - this is a warning!
2022-11-10 13:49:47,901 - root - INFO - just useful info</mark>
(venv) pi@raspberrypi:~ $ <strong>sudo tail /var/log/syslog</strong>
&lt;...output omitted...&gt;
<mark>Nov 10 13:49:41 raspberrypi &#91;2686]: WARNING: this is a warning!
Nov 10 13:49:47 raspberrypi &#91;2686]: INFO: just useful info</mark>
(venv) pi@raspberrypi:~ $ 
</code></pre>



<p>For a breakdown of all the functions available in the module, check out the git repo (<a href="https://github.com/LearningToPi/logging_handler" target="_blank" rel="noreferrer noopener">https://github.com/LearningToPi/logging_handler</a>) for the latest documentation.</p>



<p>Happy coding!</p>
<p>The post <a href="https://www.learningtopi.com/sbc/raspberry-pi/python_logging_handler/">Python logging_handler released!</a> appeared first on <a href="https://www.learningtopi.com">Learning to Pi</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.learningtopi.com/sbc/raspberry-pi/python_logging_handler/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
