https://docs.python.org/2.7/library/logging.html https://docs.python.org/2.7/library/logging.config.html https://docs.python.org/2.7/whatsnew/2.7.html#pep-391-dictionary-based-configuration-for-logging
-
-
Save SatishGodaPearl/710af13a210c7570c653dab2e3c29104 to your computer and use it in GitHub Desktop.
Python basic logging boilerplate for console log
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
import logging | |
log = logging.getLogger() | |
handler = logging.StreamHandler() | |
handler.setFormatter(logging.Formatter('%(levelname)s: %(message)s')) | |
log.addHandler(handler) | |
log.setLevel(logging.INFO) | |
log.info('Hello World!') |
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
import logging | |
log = logging.getLogger() | |
handler = logging.StreamHandler() | |
formatter = logging.Formatter('%(levelname)s: %(message)s') | |
handler.setFormatter(formatter) | |
log.addHandler(handler) | |
log.setLevel(logging.INFO) | |
class Number(object): | |
def __init__(self, default=0): | |
self._value = default | |
def getValue(self): | |
return self._value | |
def setValue(self, value): | |
self._value = value | |
class Add(object): | |
number1 = Number() | |
number2 = Number() | |
output = Number() | |
_cache = { | |
} | |
@classmethod | |
def cache(cls, inputs): | |
if inputs in cls._cache: | |
return cls._cache[inputs] | |
log.debug("Cache miss: input {0}".format(inputs)) | |
return None | |
@classmethod | |
def function(cls, number1, number2): | |
return number1 + number2 | |
def execute(self): | |
number1 = self.number1.getValue() | |
number2 = self.number2.getValue() | |
result = self.cache((number1, number2)) | |
if result is None: | |
result = self.function(number1, number2) | |
self._cache[(number1, number2)] = result | |
self.output.setValue(result) | |
add = Add() | |
add.execute() | |
print add._cache | |
add.number1.setValue(100) | |
log.setLevel(logging.DEBUG) | |
add.execute() | |
print add._cache | |
log.setLevel(logging.INFO) | |
add.number2.setValue(300) | |
add.execute() | |
print add._cache |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment