The Python logging module organizes loggers in a hierarchy. All loggers are descendants of the root logger. Each logger passes log messages on to its parent. So in effect, you have two ways of configuring logging.
1. Setup the configuration at a particular logger level.
2. Setup the configuration at the root level. If a particular logger doesn't have a configuration, the default(root) level configuration will be used.
To configure the root level logger, use the following snippet in your main entry script.
import logging
LOG_LEVEL = logging.DEBUG
logger = logging.getLogger() # Gets the root logger
logger.setLevel(LOG_LEVEL)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(LOG_LEVEL)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
handler.setFormatter(formatter)
logger.addHandler(handler)
Then in all other modules, you can add two simple lines to create a module level logger. As the module level logger doesn't have any configuration, it will use the root level logger configuration.
import logging
logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())
You can configure the root logger by using the logging.basicConfig() function.
logging.getLogger(name)
Return a logger with the specified name or, if no name is specified, return a logger which is the root logger of the hierarchy. If specified, the name is typically a dot-separated hierarchical name like “a”, “a.b” or “a.b.c.d”. Choice of these names is entirely up to the developer who is using logging.
Showing posts with label python. Show all posts
Showing posts with label python. Show all posts
Monday, February 2, 2015
Friday, December 12, 2014
Upgrading all install packages with pip
See upgrades available
The following commands will show all the packages which have an upgrade available.
$ pip list --outdated
argparse (Current: 1.2.1 Latest: 1.2.2)
astroid (Current: 1.2.1 Latest: 1.3.2)
ipython (Current: 2.3.0 Latest: 2.3.1)
$pip freeze | cut -d = -f 1 | xargs -n 1 pip search | grep -B2 'LATEST:'
(standard input)-4-argparse - Python command-line parsing library
(standard input)-5- INSTALLED: 1.2.1
(standard input):6: LATEST: 1.2.2
--
(standard input)-42-astroid - An abstract syntax tree for Python with inference support.
(standard input)-43- INSTALLED: 1.2.1
(standard input):44: LATEST: 1.3.2
--
(standard input)-48-ipython - IPython: Productive Interactive Computing
(standard input)-49- INSTALLED: 2.3.0
(standard input):50: LATEST: 2.3.1
Upgrade all packages
Use this command to install all the installed packages using pip.
pip install -U `pip list --outdated | awk '!/Could not|ignored/ { print $1}'`
The following commands will show all the packages which have an upgrade available.
$ pip list --outdated
argparse (Current: 1.2.1 Latest: 1.2.2)
astroid (Current: 1.2.1 Latest: 1.3.2)
ipython (Current: 2.3.0 Latest: 2.3.1)
$pip freeze | cut -d = -f 1 | xargs -n 1 pip search | grep -B2 'LATEST:'
(standard input)-4-argparse - Python command-line parsing library
(standard input)-5- INSTALLED: 1.2.1
(standard input):6: LATEST: 1.2.2
--
(standard input)-42-astroid - An abstract syntax tree for Python with inference support.
(standard input)-43- INSTALLED: 1.2.1
(standard input):44: LATEST: 1.3.2
--
(standard input)-48-ipython - IPython: Productive Interactive Computing
(standard input)-49- INSTALLED: 2.3.0
(standard input):50: LATEST: 2.3.1
Upgrade all packages
Use this command to install all the installed packages using pip.
pip install -U `pip list --outdated | awk '!/Could not|ignored/ { print $1}'`
Install python-ldap on macos
When install python-ldap on macos, you may get an error about sasl.h. To resolve this error, use the following commands,
$ xcrun --show-sdk-path
$ xcrun --show-sdk-path
$ sudo ln -s /usr/include /usr/include
$ pip install python-ldap
More details on the issue can be found here.
Sunday, March 30, 2014
Print the attributes of an object in Python
This can be done easily using,
print object.__dict__
You can also write a function as below.
print object.__dict__
You can also write a function as below.
def dump_an_object(obj):
for attr in dir(obj):
print "obj.%s = %s" % (attr, getattr(obj, attr))
Monday, March 24, 2014
Getting rid of unicode pain in Python
Python programming has its quirks and one of them revolves around Unicode. This article provides a very useful explanation of the issues and ways to go around it.
http://nedbatchelder.com/text/unipain.html
http://nedbatchelder.com/text/unipain.html
Sunday, November 10, 2013
Good explanation of python decorators
By Simeon Franklin
http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/
http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/
Thursday, November 7, 2013
Installing python-ldap in Ubuntu
$ sudo apt-get install python-dev libldap2-dev libsasl2-dev libssl-dev
$ pip install python-ldap
Wednesday, November 6, 2013
Checking the PYTHONPATH interactively
You can see the value of your Python path using the Python interactive interpreter and tying this:
>>> import sys
>>> import sys
>>> print sys.path
Subscribe to:
Posts (Atom)