Top-rated Python recipes tagged "meta:requires=logging"http://code.activestate.com/recipes/langs/python/tags/meta:requires=logging/top/2015-12-01T16:23:09-08:00ActiveState Code RecipesHow to use super() effectively -- Python 2.7 version (Python)
2011-06-01T19:19:37-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577721-how-to-use-super-effectively-python-27-version/
<p style="color: grey">
Python
recipe 577721
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/cooperative/">cooperative</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/multiple/">multiple</a>, <a href="/recipes/tags/next_in_mro/">next_in_mro</a>, <a href="/recipes/tags/super/">super</a>).
Revision 8.
</p>
<p>Python's super() provides a unique and amazing capability. It allows subclasses to be written to reorder a chain method calls. The recipe demonstrates all of the tradecraft needed to get super() to do your bidding.</p>
How to use super() effectively (Python)
2011-06-01T19:17:58-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577720-how-to-use-super-effectively/
<p style="color: grey">
Python
recipe 577720
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/cooperative/">cooperative</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/multiple/">multiple</a>, <a href="/recipes/tags/next_in_mro/">next_in_mro</a>, <a href="/recipes/tags/super/">super</a>).
Revision 8.
</p>
<p>Python's super() provides a unique and amazing capability. It allows subclasses to be written to reorder a chain method calls. The recipe demonstrates all of the tradecraft needed to get super() to do your bidding.</p>
Simple creation, configuration and installation of logging handlers (Python)
2011-06-01T12:13:21-07:00Nick Coghlanhttp://code.activestate.com/recipes/users/2035254/http://code.activestate.com/recipes/577496-simple-creation-configuration-and-installation-of-/
<p style="color: grey">
Python
recipe 577496
by <a href="/recipes/users/2035254/">Nick Coghlan</a>
(<a href="/recipes/tags/logging/">logging</a>).
</p>
<p>Creation of log message handler for the logging module is often a multi-step process, involving creation of the handler object, configuration of the message levels and formats, installation of any filters and then actual connection of the handler to the relevant logger object.</p>
<p>This helper function allows all of these things to be specified up front in a single function call, which then takes care of configuring the handler object appropriately.</p>
Stack-based indentation of formatted logging (Python)
2005-04-28T23:27:16-07:00Christopher Dunnhttp://code.activestate.com/recipes/users/1683375/http://code.activestate.com/recipes/412603-stack-based-indentation-of-formatted-logging/
<p style="color: grey">
Python
recipe 412603
by <a href="/recipes/users/1683375/">Christopher Dunn</a>
(<a href="/recipes/tags/debugging/">debugging</a>).
Revision 3.
</p>
<p>Some people like to sprinkle stack trace information in their code, and it is always helpful to get a visual clue to the call stack depth. inspect.stack() contains the entire call stack.</p>
<p>To make this information available conditionally, on a per-subsystem basis, the logging module is helpful. Here is one way to combine the two ideas.</p>
Using the logging module (Python)
2005-05-04T05:41:45-07:00Christopher Dunnhttp://code.activestate.com/recipes/users/1683375/http://code.activestate.com/recipes/412552-using-the-logging-module/
<p style="color: grey">
Python
recipe 412552
by <a href="/recipes/users/1683375/">Christopher Dunn</a>
(<a href="/recipes/tags/debugging/">debugging</a>).
Revision 7.
</p>
<p>This amazingly powerful module is a mystery to novices. Once you figure it out, you'll use it everywhere.</p>
<p>In addition to examples, here are a couple of useful filters.</p>
Print Logger Internals (Python)
2012-12-24T04:11:35-08:00Dave Baileyhttp://code.activestate.com/recipes/users/4168479/http://code.activestate.com/recipes/578389-print-logger-internals/
<p style="color: grey">
Python
recipe 578389
by <a href="/recipes/users/4168479/">Dave Bailey</a>
(<a href="/recipes/tags/logging/">logging</a>).
</p>
<p>This helper function simplifies the difficult task of setting up and maintaining a logging system. Changes to a logging system can cause unanticipated consequences such as lost messages or duplicates. Debugging a logging hierarchy can be a tedious task. This function overrides the internal __repr__ functions of the internal classes and allows a print statement to generate the complete logger hierarchy with its associated internals. It allows easy debugging a logger and allows changes to be easily detected.</p>
Distributed lock manager for Python (Python)
2012-07-04T21:03:32-07:00pavelhttp://code.activestate.com/recipes/users/4171837/http://code.activestate.com/recipes/578194-distributed-lock-manager-for-python/
<p style="color: grey">
Python
recipe 578194
by <a href="/recipes/users/4171837/">pavel</a>
(<a href="/recipes/tags/distributed/">distributed</a>, <a href="/recipes/tags/lock/">lock</a>, <a href="/recipes/tags/locking/">locking</a>, <a href="/recipes/tags/locks/">locks</a>, <a href="/recipes/tags/manager/">manager</a>, <a href="/recipes/tags/socket/">socket</a>, <a href="/recipes/tags/thread/">thread</a>, <a href="/recipes/tags/threading/">threading</a>, <a href="/recipes/tags/wsgi/">wsgi</a>).
Revision 2.
</p>
<p>Distributed lock manager provides mutex(es) over network. It is used to synchronize processes running on different machines, e.g. WSGI processes in case of web applications. Lock object is compatible with threading.Lock and can be used as a context manager ("with statement"). It can be easily modified to use UNIX sockets instead of TCP/IP. Communication protocol is text based.</p>
<p>First start server process:</p>
<pre class="prettyprint"><code>$ chmod +x dlm.py
$ ./dlm.py
</code></pre>
<p>Usage:</p>
<pre class="prettyprint"><code>from dlm import LockClient
client = LockClient('localhost', 27272, 'client_name')
lock = client.mkLock('lock_name')
lock.acquire()
# critical section here...
lock.release()
# using context manager
with lock:
# critical section here...
</code></pre>
Use DebugView utility with standard python logging (Python)
2010-02-10T10:35:59-08:00Christopher Prinoshttp://code.activestate.com/recipes/users/481494/http://code.activestate.com/recipes/577040-use-debugview-utility-with-standard-python-logging/
<p style="color: grey">
Python
recipe 577040
by <a href="/recipes/users/481494/">Christopher Prinos</a>
(<a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/debugview/">debugview</a>, <a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/sysinternals/">sysinternals</a>, <a href="/recipes/tags/windows/">windows</a>).
</p>
<p>This is a custom logging.Handler class that lets you use standard logging calls to output messages to SysInternals' DebugView utility.</p>
Method-based URL dispatcher for the Tornado web server (Python)
2009-11-20T11:51:47-08:00Dan McDougallhttp://code.activestate.com/recipes/users/4169722/http://code.activestate.com/recipes/576958-method-based-url-dispatcher-for-the-tornado-web-se/
<p style="color: grey">
Python
recipe 576958
by <a href="/recipes/users/4169722/">Dan McDougall</a>
(<a href="/recipes/tags/dispatcher/">dispatcher</a>, <a href="/recipes/tags/shortcuts/">shortcuts</a>, <a href="/recipes/tags/subclass/">subclass</a>, <a href="/recipes/tags/tornado/">tornado</a>, <a href="/recipes/tags/url/">url</a>, <a href="/recipes/tags/web/">web</a>).
Revision 5.
</p>
<p>The MethodDispatcher is a subclass of <a href="http://www.tornadoweb.org/">tornado</a>.web.RequestHandler that will use
the methods contained in subclasses of MethodDispatcher to handle requests. In
other words, instead of having to make a new RequestHandler class for every URL
in your application you can subclass MethodDispatcher and use the methods
contained therein <em>as</em> your URLs.</p>
<p>The MethodDispatcher also adds the convenience of automatically passing
arguments to your class methods. So there is no need to use Tornado's
get_argument() method.</p>
<h5><strong>Example</strong></h5>
<p>To demonstrate the advantages of using MethodDispatcher I'll present a standard
Tornado app with multiple URLs and re-write it using MethodDispatcher...</p>
<h5><strong>The standard Tornado way</strong></h5>
<pre class="prettyprint"><code>class Foo(tornado.web.RequestHandler):
def get(self):
self.write('foo')
class Bar(tornado.web.RequestHandler):
def get(self):
self.write('bar')
class SimonSays(tornado.web.RequestHandler):
def get(self):
say = self.get_argument("say")
self.write('Simon says, %s' % `say`)
application = tornado.web.Application([
(r"/foo", Foo),
(r"/bar", Bar),
(r"/simonsays", SimonSays),
])
</code></pre>
<h5><strong>The MethodDispatcher way</strong></h5>
<pre class="prettyprint"><code>class FooBar(MethodDispatcher):
def foo(self):
self.write("foo")
def bar(self):
self.write("bar")
def simonsays(self, say):
self.write("Simon Says, %s" % `say`)
application = tornado.web.Application([
(r"/.*", FooBar)
])
</code></pre>
<h5><strong>Notes</strong></h5>
<p>As you can see from the above example, using the MethodDispatcher can
significantly reduce the complexity of Tornado applications. Here's some other
things to keep in mind when using the MethodDispatcher:</p>
<ul>
<li>MethodDispatcher will ignore any methods that begin with an underscore (_).
This prevents builtins and private methods from being exposed to the web.</li>
<li>The '/' path is special: It always maps to self.index().</li>
<li>MethodDispatcher does not require that your methods distinquish between GET
and POST requests. Whether a GET or POST is performed the matching method
will be called with any passed arguments or POSTed data. Because of the way
this works you should not use get() and post() in your MethodDispatcher
subclasses unless you want to override this functionality.</li>
<li>When an argument is passed with a single value (/simonsays?say=hello) the
value passed to the argument will be de-listed. In other words, it will be
passed to your method like so: {'say': 'hello'}. This overrides the
default Tornado behavior which would return the value as a list:
{'say': ['hello']}. If more than one value is passed MethodDispatcher will
use the default behavior.</li>
</ul>
Python 3 WSGI Server (Python)
2009-09-22T11:36:30-07:00poops mcgeehttp://code.activestate.com/recipes/users/4171745/http://code.activestate.com/recipes/576906-python-3-wsgi-server/
<p style="color: grey">
Python
recipe 576906
by <a href="/recipes/users/4171745/">poops mcgee</a>
.
Revision 2.
</p>
<p>This is a simplified version of tornado's WSGI server implemented in python 3.</p>
<p>Example usage:</p>
<pre class="prettyprint"><code>import web
import wsgiref.simple_server
class MainHandler(web.RequestHandler):
def get(self):
self.write("Hello, world")
if __name__ == "__main__":
application = web.WSGIApplication([
(r"/", MainHandler),
])
server = wsgiref.simple_server.make_server('', 8888, application)
server.serve_forever()
</code></pre>
Error logging with context manager and decorator (Python)
2007-09-19T06:52:58-07:00Kent Johnsonhttp://code.activestate.com/recipes/users/2016182/http://code.activestate.com/recipes/531821-error-logging-with-context-manager-and-decorator/
<p style="color: grey">
Python
recipe 531821
by <a href="/recipes/users/2016182/">Kent Johnson</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
</p>
<p>A context manager and decorator that wrap common exception trapping and handling code so they may be applied with just a single line of code in the client.</p>
How to use super() effectively -- Python 3.2 version (Python)
2015-12-01T16:23:09-08:00Lance E Sloanhttp://code.activestate.com/recipes/users/4188012/http://code.activestate.com/recipes/579130-how-to-use-super-effectively-python-32-version/
<p style="color: grey">
Python
recipe 579130
by <a href="/recipes/users/4188012/">Lance E Sloan</a>
(<a href="/recipes/tags/cooperative/">cooperative</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/multiple/">multiple</a>, <a href="/recipes/tags/next_in_mro/">next_in_mro</a>, <a href="/recipes/tags/super/">super</a>).
</p>
<p>Python's super() provides a unique and amazing capability. It allows subclasses to be written to reorder a chain method calls. The recipe demonstrates all of the tradecraft needed to get super() to do your bidding.</p>
Trace decorator for debugging (Python)
2011-01-24T18:40:51-08:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577551-trace-decorator-for-debugging/
<p style="color: grey">
Python
recipe 577551
by <a href="/recipes/users/4173535/">Kevin L. Sitze</a>
(<a href="/recipes/tags/classes/">classes</a>, <a href="/recipes/tags/debug/">debug</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/functions/">functions</a>, <a href="/recipes/tags/inspection/">inspection</a>, <a href="/recipes/tags/modules/">modules</a>, <a href="/recipes/tags/trace/">trace</a>).
Revision 2.
</p>
<p>This package provides a decorator for tracing function and method calls in your applications. The tracing capabilities are managed through the logging package, and several mechanisms are provided for controlling the destination of the trace output.</p>
<p>It also provides functionality for adding decorators to existing classes or modules.</p>
One-word logging.basicConfig wrapper (Python)
2010-01-06T06:54:09-08:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/576994-one-word-loggingbasicconfig-wrapper/
<p style="color: grey">
Python
recipe 576994
by <a href="/recipes/users/57155/">Denis Barmenkov</a>
(<a href="/recipes/tags/basicconfig/">basicconfig</a>, <a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/wrapper/">wrapper</a>).
Revision 2.
</p>
<p>Every Python logging manual have this code:</p>
<pre class="prettyprint"><code>logging.basicConfig(level=logging.DEBUG, filename='debug.log',
format='%(asctime)s %(levelname)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
</code></pre>
<p>This is a simple function which call it for you.
All you need is remember one function name; useful on little scripts.</p>
pypopper - Python POP3 Server (Python)
2007-10-24T18:52:39-07:00Daniel Millerhttp://code.activestate.com/recipes/users/4016391/http://code.activestate.com/recipes/534131-pypopper-python-pop3-server/
<p style="color: grey">
Python
recipe 534131
by <a href="/recipes/users/4016391/">Daniel Miller</a>
(<a href="/recipes/tags/programs/">programs</a>).
</p>
<p>While developing <a href="http://pieces.openpolitics.com/thunderbayes/">ThunderBayes</a> (an extension for Thunderbird) I wrote this simple POP3 server to serve a message to my ThunderBayes test setup.</p>
Hex-dump port-forwarding network proxy server (Python)
2007-04-09T17:22:07-07:00Andrew Ehttp://code.activestate.com/recipes/users/4034885/http://code.activestate.com/recipes/502293-hex-dump-port-forwarding-network-proxy-server/
<p style="color: grey">
Python
recipe 502293
by <a href="/recipes/users/4034885/">Andrew E</a>
(<a href="/recipes/tags/network/">network</a>).
Revision 5.
</p>
<p>A lightweight and portable way to hex-dump network traffic between network applications.</p>
Yet another threadpool module (Python)
2007-03-25T05:37:06-07:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/502291-yet-another-threadpool-module/
<p style="color: grey">
Python
recipe 502291
by <a href="/recipes/users/2591466/">George Sakkis</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 4.
</p>
<p>A small yet flexible threadpool implementation. I wasn't quite satisfied with other existing solutions, so I rolled one on my own, building on top of the threadpool module by Christopher Arndt.</p>
<p>See the example in the end and the docstrings (in Epydoc format) for the details.</p>
TimedCompressedRotatingFileHandler (Python)
2007-02-28T08:16:44-08:00Angel Freirehttp://code.activestate.com/recipes/users/4037917/http://code.activestate.com/recipes/502265-timedcompressedrotatingfilehandler/
<p style="color: grey">
Python
recipe 502265
by <a href="/recipes/users/4037917/">Angel Freire</a>
(<a href="/recipes/tags/debugging/">debugging</a>).
</p>
<p>Python has really nice logging framework, it has too a zipfile library in the default installation that makes you able to write compressed files.</p>
<p>Several of the logging handlers "rotate" files, by size, date, etc. Here is an example of handler class for the logging framework that, when the file is rotated, it will make a .zip of the old file.</p>
Logging SimpleXMLRPCServer (Python)
2006-05-13T13:53:57-07:00John Ferrellhttp://code.activestate.com/recipes/users/2351500/http://code.activestate.com/recipes/496700-logging-simplexmlrpcserver/
<p style="color: grey">
Python
recipe 496700
by <a href="/recipes/users/2351500/">John Ferrell</a>
.
</p>
<p>This recipe adds logging support to SimpleXMLRPCServer using Python's logging module. The server will log the client IP, the client's XML request and the server's XML response.</p>
Extending the 'logging' module (Python)
2006-02-27T00:57:54-08:00Ori Peleghttp://code.activestate.com/recipes/users/2056315/http://code.activestate.com/recipes/474089-extending-the-logging-module/
<p style="color: grey">
Python
recipe 474089
by <a href="/recipes/users/2056315/">Ori Peleg</a>
(<a href="/recipes/tags/debugging/">debugging</a>).
Revision 2.
</p>
<p>Adding new format specifiers to the logging module.
In this example, it's for the user name and the name of the function that logged the message.</p>