Permalink
Browse files

Initial commit of python logging examples

  • Loading branch information...
0 parents commit 74238b498c93c48867351736b953d5b9eccc89ac @karlp committed Aug 29, 2011
Showing with 103 additions and 0 deletions.
  1. +18 −0 importonly.py
  2. +10 −0 nologging.py
  3. +26 −0 off_by_default.py
  4. +20 −0 on_by_default.py
  5. +14 −0 simplelogging.py
  6. +15 −0 verboselib/__init__.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()
@@ -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()
@@ -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()
@@ -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()
@@ -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()
@@ -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.