In [2]:
from __future__ import print_function
import presentation_helper
In [8]:
from datetime import datetime
def my_division_p(dividend, divisor):
try:
print("Debug, Division : {}/{}".format(dividend,divisor))
result = dividend / divisor
return result
except (ZeroDivisionError, TypeError):
print("Error, Division Failed")
return None
def division_task_handler_p(task):
print("Handling division task,{} items".format(len(task)))
result = []
for i, task in enumerate(task):
print("Doing devision iteration {} on {:%Y}".format(i,datetime.now()))
dividend, divisor = task
result.append(my_division_p(dividend,divisor))
return result
In [9]:
task = [(3,4),(5,1.4),(2,0),(3,5),("10",1)]
division_task_handler_p(task)
Out[9]:
In [10]:
import log1; logging = log1.get_clean_logging()
logging.basicConfig(level=logging.DEBUG)
log = logging.getLogger()
def my_division(dividend, divisor):
try:
log.debug("Division : %s/%s", dividend, divisor)
result = dividend / divisor
return result
except (ZeroDivisionError, TypeError):
log.exception("Error, Division Failed")
return None
def division_task_handler(task):
log.info("Handling division task,%s items",len(task))
result = []
for i, task in enumerate(task):
log.info("Doing devision iteration %s",i)
dividend, divisor = task
result.append(my_division(dividend,divisor))
return result
In [11]:
task = [(3,4),(2,0),(3,5),("10",1)]
division_task_handler(task)
Out[11]:
In [12]:
import log1;logging = log1.get_clean_logging() # this would be import logging outside this notebook
logging.debug("Find me in the log")
logging.info("I am hidden")
logging.warn("I am here")
logging.error("As am I")
try:
1/0;
except:
logging.exception(" And I")
logging.critical("Me, of course")
In [13]:
import log1;logging = log1.get_clean_logging()
datefmt = "%Y-%m-%d %H:%M:%S"
msgfmt = "%(asctime)s,%(msecs)03d %(levelname)-10s %(name)-15s : %(message)s"
logging.basicConfig(level=logging.DEBUG, format=msgfmt, datefmt=datefmt)
logging.debug("Now I show up ")
logging.info("Now this is %s logging!","good")
logging.warn("I am here. %-4i + %-4i = %i",1,3,1+3)
logging.error("As am I")
try:
1/0;
except:
logging.exception(" And I")
In [14]:
import log1, json, logging.config;logging = log1.get_clean_logging()
datefmt = "%Y-%m-%d %H:%M:%S"
msgfmt = "%(asctime)s,%(msecs)03d %(levelname)-6s %(name)-10s : %(message)s"
log = logging.getLogger()
log.setLevel(logging.DEBUG)
lh = logging.StreamHandler()
lf = logging.Formatter(fmt=msgfmt, datefmt=datefmt)
lh.setFormatter(lf)
log.addHandler(lh)
log.info("Now this is %s logging!","good")
log.debug("A slightly more complex message %s + %s = %s",1,2,1+2)
Attribute | Description |
---|---|
args | Tuple of arguments passed to the logging call |
asctime | Log record creation time, formatted |
created | Log record creation time, seconds since the Epoch |
exc_info | Exception information / stack trace, if any |
filename | Filename portion of pathname for the logging module |
funcName | Name of function containing the logging call |
levelname | Name of Logging Level |
levelno | Number of Logging Level |
lineno | Line number in source code for the logging call |
module | Module (name portion of filename). |
message | Logged message |
name | Name of the logger used to log the call. |
pathname | pathname of source file |
process | Process ID |
processName | Process name |
... | ... |
In [15]:
import log1, json, logging.config;logging = log1.get_clean_logging()
conf_dict = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'longformat': {
'format': "%(asctime)s,%(msecs)03d %(levelname)-10s %(name)-15s : %(message)s",
'datefmt': "%Y-%m-%d %H:%M:%S"}},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': "longformat"}},
'loggers':{
'': {
'level': 'DEBUG',
'handlers': ['console']}}}
logging.config.dictConfig(conf_dict)
log = logging.getLogger()
log.info("Now this is %s logging!","good")
In [16]:
import log1, json, logging.config;logging = log1.get_clean_logging()
base_config = json.load(open("conf_dict.json"))
base_config['handlers']['logfile'] = {
'class' : 'logging.FileHandler',
'mode' : 'w',
'filename' : 'logfile.txt',
'formatter': "longformat"}
base_config['loggers']['']['handlers'].append('logfile')
logging.config.dictConfig(base_config)
log = logging.getLogger()
log.info("Now this is %s logging!","good")
!cat logfile.txt
In [1]:
import log1, json, logging.config;logging = log1.get_clean_logging()
file_config = json.load(open("conf_dict_with_file.json"))
file_config['handlers']['logfile']['level'] = "WARN"
logging.config.dictConfig(file_config)
log = logging.getLogger()
log.info("Now this is %s logging!","good")
log.warning("Now this is %s logging!","worrisome")
!cat logfile.txt
In [18]:
import log1,json,logging.config;logging = log1.get_clean_logging()
logging.config.dictConfig(json.load(open("conf_dict.json")))
log = logging.getLogger("")
child_A = logging.getLogger("A")
child_B = logging.getLogger("B")
child_B_A = logging.getLogger("B.A")
log.info("Now this is %s logging!","good")
child_A.info("Now this is more logging!")
log.warning("Now this is %s logging!","worrisome")
In [20]:
import log1,json,logging.config;logging = log1.get_clean_logging()
logging.config.dictConfig(json.load(open("conf_dict.json")))
def log_filter(rec): # Callables work with 3.2 and later
if 'please' in rec.msg.lower():
return True
return False
log = logging.getLogger("")
log.addFilter(log_filter)
child_A = logging.getLogger("A")
log.info("Just log me")
child_A.info("Just log me")
log.info("Hallo, Please log me")
In [20]:
import log1, json, logging.config;logging = log1.get_clean_logging()
datefmt = "%Y-%m-%d %H:%M:%S"
msgfmt = "%(asctime)s,%(msecs)03d %(levelname)-6s %(name)-10s : %(message)s"
log_reg = None
def handler_filter(rec): # Callables work with 3.2 and later
global log_reg
if 'please' in rec.msg.lower():
rec.msg = rec.msg + " (I am nice)" # Changing the record
rec.args = (rec.args[0].upper(), rec.args[1] + 10)
rec.__dict__['custom_name'] = "Important context information"
log_reg = rec
return True
return False
log = logging.getLogger()
lh = logging.StreamHandler()
lf = logging.Formatter(fmt=msgfmt, datefmt=datefmt)
lh.setFormatter(lf)
log.addHandler(lh)
lh.addFilter(handler_filter)
log.warn("I am a bold Logger","good")
log.warn("Hi, I am %s. I am %i seconds old. Please log me","Loggy", 1)
In [21]:
print(log_reg)
log_reg.__dict__
Out[21]:
In [4]:
import json, logging.config
config = json.load(open("conf_dict_with_file.json"))
logging.config.dictConfig(config)
import requests
import logging_tree
logging_tree.printout()
In [22]:
import log1,json,logging,logging.config;logging = log1.get_clean_logging()
#Load Config, define a child logger (could also be a module)
logging.config.dictConfig(json.load(open("conf_dict_with_file.json")))
child_log = logging.getLogger("somewhere")
#Reload Config
logging.config.dictConfig(json.load(open("conf_dict_with_file.json")))
#Our childlogger was disables
child_log.info("Now this is %s logging!","good")
In [30]:
import log1, json, logging, logging.config;logging = log1.get_clean_logging()
config = json.load(open("conf_dict_with_file.json"))
#Load Config, define a child logger (could also be a module)
logging.config.dictConfig(config)
child_log = logging.getLogger("somewhere")
config['disable_existing_loggers'] = False
#Reload Config
logging.config.dictConfig(config)
#Our childlogger was disables
child_log.info("Now this is %s logging!","good")
In [6]:
from presentation_helper import customize_settings
customize_settings()