Draft Forbes Group Website (Build by Nikola). The official site is hosted at:
License: GPL3
ubuntu2004
Kernel: Python 3
In this post, we discuss how to use the Python logging facilities in your code.
TL;DR
In [15]:
Out[15]:
1
In [3]:
In [15]:
In [13]:
Out[13]:
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-13-a14d51296448> in <module>()
90
91 a = A()
---> 92 a.run()
<ipython-input-13-a14d51296448> in run(self)
76 with self.task("Sprinting"):
77 time.sleep(0.1)
---> 78 raise Exception()
79
80
Exception:
In [9]:
Out[9]:
'[{levelname[0]} {asctime} {name}] {message}'
In [14]:
In [4]:
Out[4]:
6
In [12]:
Out[12]:
Hi
In [42]:
Out[42]:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/data/apps/anaconda/envs/jupyter/lib/python3.6/site-packages/IPython/core/formatters.py in __call__(self, obj)
700 type_pprinters=self.type_printers,
701 deferred_pprinters=self.deferred_printers)
--> 702 printer.pretty(obj)
703 printer.flush()
704 return stream.getvalue()
/data/apps/anaconda/envs/jupyter/lib/python3.6/site-packages/IPython/lib/pretty.py in pretty(self, obj)
398 if cls is not object \
399 and callable(cls.__dict__.get('__repr__')):
--> 400 return _repr_pprint(obj, self, cycle)
401
402 return _default_pprint(obj, self, cycle)
/data/apps/anaconda/envs/jupyter/lib/python3.6/site-packages/IPython/lib/pretty.py in _repr_pprint(obj, p, cycle)
693 """A pprint that just redirects to the normal repr function."""
694 # Find newlines and replace them with p.break_()
--> 695 output = repr(obj)
696 for idx,output_line in enumerate(output.splitlines()):
697 if idx:
/data/apps/anaconda/envs/jupyter/lib/python3.6/logging/__init__.py in __repr__(self)
1003 name = getattr(self.stream, 'name', '')
1004 if name:
-> 1005 name += ' '
1006 return '<%s %s(%s)>' % (self.__class__.__name__, name, level)
1007
TypeError: unsupported operand type(s) for +=: 'int' and 'str'
In [13]:
Recomendations
Setup the loggers at the point of use, not at the start of a module etc. Getting the logger too early makes it difficult for the user to configure the logging attributes:
References
In [ ]: