Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit of python logging examples
- Loading branch information
0 parents
commit 74238b4
Showing
6 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") |