<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	
	>
<channel>
	<title>
	Comments on: Python logging best practices	</title>
	<atom:link href="https://www.robg3d.com/2012/02/python-logging-best-practices/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.robg3d.com/2012/02/python-logging-best-practices/</link>
	<description>Blog of Rob Galanakis (@robgalanakis)</description>
	<lastBuildDate>Fri, 26 Jan 2018 07:43:12 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.4.1</generator>
	<item>
		<title>
		By: Vinay Sajip		</title>
		<link>https://www.robg3d.com/2012/02/python-logging-best-practices/#comment-8158</link>

		<dc:creator><![CDATA[Vinay Sajip]]></dc:creator>
		<pubDate>Sat, 11 Feb 2012 18:57:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.robg3d.com/?p=906#comment-8158</guid>

					<description><![CDATA[&lt;blockquote cite=&quot;#comment-content-8156&quot;&gt;
&lt;strong&gt;&lt;a href=&quot;#comment-8156&quot; rel=&quot;nofollow&quot;&gt;Rob Galanakis&lt;/a&gt; :&lt;/strong&gt;
    Regarding that Windows domain, was the library itself doing IO to authenticate? 
  &lt;/blockquote&gt;

Well, it was getting the credentials (including domain) across the network.

&lt;blockquote cite=&quot;#comment-content-8156&quot;&gt;
&lt;strong&gt;&lt;a href=&quot;#comment-8156&quot; rel=&quot;nofollow&quot;&gt;Rob Galanakis&lt;/a&gt; :&lt;/strong&gt;
In response to logging and re-raising: What I meant is, this is pointless (though not uncommon) code:
  &lt;/blockquote&gt;
[example snipped]

I agree with that - no need to log and just &quot;raise&quot;.]]></description>
			<content:encoded><![CDATA[<blockquote cite="#comment-content-8156"><p>
<strong><a href="#comment-8156" rel="nofollow">Rob Galanakis</a> :</strong><br />
    Regarding that Windows domain, was the library itself doing IO to authenticate?
  </p></blockquote>
<p>Well, it was getting the credentials (including domain) across the network.</p>
<blockquote cite="#comment-content-8156"><p>
<strong><a href="#comment-8156" rel="nofollow">Rob Galanakis</a> :</strong><br />
In response to logging and re-raising: What I meant is, this is pointless (though not uncommon) code:
  </p></blockquote>
<p>[example snipped]</p>
<p>I agree with that &#8211; no need to log and just &#8220;raise&#8221;.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Rob Galanakis		</title>
		<link>https://www.robg3d.com/2012/02/python-logging-best-practices/#comment-8156</link>

		<dc:creator><![CDATA[Rob Galanakis]]></dc:creator>
		<pubDate>Sat, 11 Feb 2012 15:50:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.robg3d.com/?p=906#comment-8156</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.robg3d.com/2012/02/python-logging-best-practices/#comment-8154&quot;&gt;Vinay Sajip&lt;/a&gt;.

Regarding that Windows domain, was the library itself doing IO to authenticate? As I should have said in the article, IO is a good thing to log, and is definitely one place you want to log more than just the exception cases, like you say. For example I always log everything I send/receive to/from several of the local services we run our tools against.

In response to logging and re-raising: What I meant is, this is pointless (though not uncommon) code:
&lt;code&gt;try:
    foo()
except Exception:
    logger.error(...)
    raise&lt;/code&gt;
If you have more than the naked &#039;raise&#039; statement that raises the current exception (well whatever the precise technical description is for what it does), clearly there is a reason to log.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.robg3d.com/2012/02/python-logging-best-practices/#comment-8154">Vinay Sajip</a>.</p>
<p>Regarding that Windows domain, was the library itself doing IO to authenticate? As I should have said in the article, IO is a good thing to log, and is definitely one place you want to log more than just the exception cases, like you say. For example I always log everything I send/receive to/from several of the local services we run our tools against.</p>
<p>In response to logging and re-raising: What I meant is, this is pointless (though not uncommon) code:<br />
<code>try:<br />
    foo()<br />
except Exception:<br />
    logger.error(...)<br />
    raise</code><br />
If you have more than the naked &#8216;raise&#8217; statement that raises the current exception (well whatever the precise technical description is for what it does), clearly there is a reason to log.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Vinay Sajip		</title>
		<link>https://www.robg3d.com/2012/02/python-logging-best-practices/#comment-8154</link>

		<dc:creator><![CDATA[Vinay Sajip]]></dc:creator>
		<pubDate>Sat, 11 Feb 2012 13:12:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.robg3d.com/?p=906#comment-8154</guid>

					<description><![CDATA[&lt;blockquote cite=&quot;#comment-content-8151&quot;&gt;
&lt;strong&gt;&lt;a href=&quot;#comment-8151&quot; rel=&quot;nofollow&quot;&gt;Rob Galanakis&lt;/a&gt; :&lt;/strong&gt;
On point #1, I agree. But if you are using functional styles, and are minimizing your use of mutable state, I think you should have less need of logging (see next point).
Regarding logging in libraries (point #2) ... The less state your library stores and/or mutates, the better, and if you’re not storing or mutating, you can generally get away with raising errors that convey the failed information and not having to log. b) Likewise, your library should have clear contracts where logging in the case of a failure and not raising is never acceptable.
  &lt;/blockquote&gt;

I&#039;m not sure that logging should be tied to whether you store or mutate state. In the PPTP example I gave above, there was no storing or mutating of state: however, the client was passing an implicit Windows domain, which was causing authentication failure even though the username and password were correct. Now an authentication failure is not even an exception-raising condition: however, setting verbose logging allowed me to see what the problem was.

I would say that the usage of logging should be wider than just the exception cases (of course, based on the developer&#039;s discretion).

On the question of logging and re-raising: I think it&#039;s best to log and handle at one level if possible, but sometimes it&#039;s not unreasonable to log at a low level and raise a conceptually higher-level exception which makes sense at the relevant level (for example, low level SQL exception is logged and a higher level ORM exception is raised. The logging of the SQL exception aids the ORM library developer - knowing &lt;em&gt;why&lt;/em&gt; the ORM exception was raised - but the ORM-level exception aids the ORM user, because it is couched in ORM concepts).]]></description>
			<content:encoded><![CDATA[<blockquote cite="#comment-content-8151"><p>
<strong><a href="#comment-8151" rel="nofollow">Rob Galanakis</a> :</strong><br />
On point #1, I agree. But if you are using functional styles, and are minimizing your use of mutable state, I think you should have less need of logging (see next point).<br />
Regarding logging in libraries (point #2) &#8230; The less state your library stores and/or mutates, the better, and if you’re not storing or mutating, you can generally get away with raising errors that convey the failed information and not having to log. b) Likewise, your library should have clear contracts where logging in the case of a failure and not raising is never acceptable.
  </p></blockquote>
<p>I&#8217;m not sure that logging should be tied to whether you store or mutate state. In the PPTP example I gave above, there was no storing or mutating of state: however, the client was passing an implicit Windows domain, which was causing authentication failure even though the username and password were correct. Now an authentication failure is not even an exception-raising condition: however, setting verbose logging allowed me to see what the problem was.</p>
<p>I would say that the usage of logging should be wider than just the exception cases (of course, based on the developer&#8217;s discretion).</p>
<p>On the question of logging and re-raising: I think it&#8217;s best to log and handle at one level if possible, but sometimes it&#8217;s not unreasonable to log at a low level and raise a conceptually higher-level exception which makes sense at the relevant level (for example, low level SQL exception is logged and a higher level ORM exception is raised. The logging of the SQL exception aids the ORM library developer &#8211; knowing <em>why</em> the ORM exception was raised &#8211; but the ORM-level exception aids the ORM user, because it is couched in ORM concepts).</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Rob Galanakis		</title>
		<link>https://www.robg3d.com/2012/02/python-logging-best-practices/#comment-8151</link>

		<dc:creator><![CDATA[Rob Galanakis]]></dc:creator>
		<pubDate>Sat, 11 Feb 2012 09:56:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.robg3d.com/?p=906#comment-8151</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.robg3d.com/2012/02/python-logging-best-practices/#comment-8143&quot;&gt;Vinay Sajip&lt;/a&gt;.

Vinay, when you comment on logging it isn&#039;t two cents, it is more like several dollars :) Glad you read the post and commented.

On point #1, I agree. But if you are using functional styles, and are minimizing your use of mutable state, I think you should have less need of logging (see next point).

Regarding logging in libraries (point #2), I suppose like many &#039;best practices&#039; we establish, they are reactions against clearly bad practices and replacing them with the opposite is rarely the best thing. So the discouragement of logging in libraries comes down to two things: a) The less state your library stores and/or mutates, the better, and if you&#039;re not storing or mutating, you can generally get away with raising errors that convey the failed information and not having to log. b) Likewise, your library should have clear contracts where logging in the case of a failure and not raising is never acceptable. But of course this is all relative and imperfect, and people should use their best judgement- if people are going to write crappy libraries, the advice about logging isn&#039;t going to make it much better or worse.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.robg3d.com/2012/02/python-logging-best-practices/#comment-8143">Vinay Sajip</a>.</p>
<p>Vinay, when you comment on logging it isn&#8217;t two cents, it is more like several dollars :) Glad you read the post and commented.</p>
<p>On point #1, I agree. But if you are using functional styles, and are minimizing your use of mutable state, I think you should have less need of logging (see next point).</p>
<p>Regarding logging in libraries (point #2), I suppose like many &#8216;best practices&#8217; we establish, they are reactions against clearly bad practices and replacing them with the opposite is rarely the best thing. So the discouragement of logging in libraries comes down to two things: a) The less state your library stores and/or mutates, the better, and if you&#8217;re not storing or mutating, you can generally get away with raising errors that convey the failed information and not having to log. b) Likewise, your library should have clear contracts where logging in the case of a failure and not raising is never acceptable. But of course this is all relative and imperfect, and people should use their best judgement- if people are going to write crappy libraries, the advice about logging isn&#8217;t going to make it much better or worse.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Vinay Sajip		</title>
		<link>https://www.robg3d.com/2012/02/python-logging-best-practices/#comment-8143</link>

		<dc:creator><![CDATA[Vinay Sajip]]></dc:creator>
		<pubDate>Fri, 10 Feb 2012 20:23:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.robg3d.com/?p=906#comment-8143</guid>

					<description><![CDATA[I agree with many of the things you say, but disagree on some points:

1. Python does not force you to use OO, and for those people who use functional styles (or a mixture), using __name__ as the logger name is the best practice. Using the type(self).__name__ gives you the unqualified name of the class, which in a large system may occur in a number of different packages. Possibly &#039;%s.%s&#039; % (type(self).__module__, type(self).__name__) is more useful, as it allows filtering of all classes in a module using just the module name.

2. Logging in libraries shouldn&#039;t be discouraged. For example I was just looking at a problem with PPTP connections working to a server from one machine but not another. The PPTP protocol code handling is in a library, but luckily it uses logging, so I could see what was happening under the hood.

A library is meant to be used as a black box, but library developers are just like the rest of us in terms of not necessarily having thought of all corner cases. Faced with imperfect software, logging is one of the few ways you can look into a running system in production without bringing it down. That goes for both the application and its libraries.

Just my 2 cents.]]></description>
			<content:encoded><![CDATA[<p>I agree with many of the things you say, but disagree on some points:</p>
<p>1. Python does not force you to use OO, and for those people who use functional styles (or a mixture), using __name__ as the logger name is the best practice. Using the type(self).__name__ gives you the unqualified name of the class, which in a large system may occur in a number of different packages. Possibly &#8216;%s.%s&#8217; % (type(self).__module__, type(self).__name__) is more useful, as it allows filtering of all classes in a module using just the module name.</p>
<p>2. Logging in libraries shouldn&#8217;t be discouraged. For example I was just looking at a problem with PPTP connections working to a server from one machine but not another. The PPTP protocol code handling is in a library, but luckily it uses logging, so I could see what was happening under the hood.</p>
<p>A library is meant to be used as a black box, but library developers are just like the rest of us in terms of not necessarily having thought of all corner cases. Faced with imperfect software, logging is one of the few ways you can look into a running system in production without bringing it down. That goes for both the application and its libraries.</p>
<p>Just my 2 cents.</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
