Skip to content

Commit

Permalink
Initial commit of python logging examples
Browse files Browse the repository at this point in the history
  • Loading branch information
karlp committed Aug 29, 2011
0 parents commit 74238b4
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 0 deletions.
18 changes: 18 additions & 0 deletions importonly.py
@@ -0,0 +1,18 @@
"""
Exactly the same as nologging
but, now you can set logging.raiseExceptions to False to hide the warning...
"""

import verboselib
import logging

logging.raiseExceptions = False

if __name__ == '__main__':
vl = verboselib.Noisy()
print "no logging configured, so no output at all here"
vl.do_debug()
vl.do_info()
print """there will be no output from here either, because we
told logging to swallow the configuration error"""
vl.do_warn()
10 changes: 10 additions & 0 deletions nologging.py
@@ -0,0 +1,10 @@

import verboselib

if __name__ == '__main__':
vl = verboselib.Noisy()
print "no logging configured, so no output at all here"
vl.do_debug()
vl.do_info()
print "warning here, because no loggers configured"
vl.do_warn()
26 changes: 26 additions & 0 deletions off_by_default.py
@@ -0,0 +1,26 @@
"""
uses logging with an explicit configuration, and a named logger
compared to on_by_default, this sets the logging only for our own app
"""

import verboselib
import logging

# Doing it this way, only WARN and higher from libraries come out,
# but you choose the level for your app.
log = logging.getLogger("my_app")
logging.basicConfig(level=logging.WARN)
logging.getLogger("my_app").setLevel(logging.DEBUG)
# no config needed for each library, unless you want to explicitly
# enable debug/info for that library


if __name__ == '__main__':
vl = verboselib.Noisy()
# note, use our log, not the class methods.
log.info("This will be output, because we set global to DEBUG.")
log.info("But not the verbose lib calls below at debug and info")
vl.do_debug()
vl.do_info()
log.warn("Watch out! about to log from noisy!")
vl.do_warn()
20 changes: 20 additions & 0 deletions on_by_default.py
@@ -0,0 +1,20 @@
""" uses logging with an explicit configuration"""

import verboselib
import logging

# This is one way, where you turn off things you don't want to see
# and everything is on by default
logging.basicConfig(level=logging.DEBUG)
logging.getLogger("verboselib").setLevel(logging.WARN)
# ... more lines for each library you want to squelch, not very pretty


if __name__ == '__main__':
vl = verboselib.Noisy()
logging.info("This will be output, because we set global to DEBUG.")
logging.info("But not the verbose lib calls below at debug and info")
vl.do_debug()
vl.do_info()
logging.warn("Watch out! about to log from noisy!")
vl.do_warn()
14 changes: 14 additions & 0 deletions simplelogging.py
@@ -0,0 +1,14 @@
""" uses logging instead of prints, but no explicit logging config"""

import verboselib
import logging


if __name__ == '__main__':
vl = verboselib.Noisy()
logging.info("This, and the logging from Noisy, will not be output.")
logging.info("because the default level is warn")
vl.do_debug()
vl.do_info()
logging.warn("Watch out! about to log from noisy!")
vl.do_warn()
15 changes: 15 additions & 0 deletions verboselib/__init__.py
@@ -0,0 +1,15 @@

import logging

class Noisy(object):
def __init__(self):
self.log = logging.getLogger("verboselib.Noisy")

def do_debug(self):
self.log.debug("logged at debug")

def do_info(self):
self.log.info("logged at info")

def do_warn(self):
self.log.warn("logged at warn")

0 comments on commit 74238b4

Please sign in to comment.