Connect other Accounts

Easier python logging

The python logging module is a very good option for producing output in most code and especially in modules that will distributed. It is incredibly flexible but that comes with the price of a steep learning curve. The developers recognized this and included a one-stop method logging.basicConfig() to set up a basic logger that will often suffice for application usage. However, this method only sets up the root logger and does nothing if a root logger is already configured. So it is not really appropriate for use in a module.

Following is a little snippet of code that I have in a module called clogging.py (think of custom-logging). It's inspired by basicConfig() but it requires that you set the name of the logger (something different than root) and includes parameters for the common situation of logging to the screen (stdout) and optionally a file. The file logger can have a different log level to support more detailed output there.

One issue with logging is the default feature of propagating messages to parent loggers. Imagine your module defines a logger called "mymodule" and sets the level at INFO. Then an application using your module defines a root logger and likewise sets the level at INFO. Because the root logger is the parent of "mymodule", any logging.INFO statements within your module also pass messages through the root logger, potentially generating multiple outputs. Sometimes this is very useful, but in many simple applications you want to keep the module logging distinct from the application logging. In the config_logger() routine this is controlled by the propagate parameter.

To use this in a module you could put something like the following at the module level. This will get executed when the module is imported.

logger = clogging.config_logger('mymodule', level=logging.INFO)

After this users will have the option to control or redirect output from your module with the standard logging interface, e.g.

logging.getLogger('mymodule').setLevel(logging.WARNING)

A friendlier option might be to provide some documented module methods to set the module "verbosity". This would do essentially the same as above but hide the hairy details of the logging module.

To suggest a new feed to our aggregator, fill in the Contact Form on the right !

Contribute to AstroPython

Contributions and feedback from the community are strongly encouraged.If you find a bug or wish to suggest new features for the site , file an issue on Github. If you simply have a question or comment for the AstroPython team please fill in this form and we'll get back to you as soon as possible.
If you have a new resource, question, or any other content that you'd like to post to the web site then likewise fill in the creation form. If you wish to have elevated priveleges , you can use the form to contact us.

Feedback | Contribute | Contact

We would love to hear from you but please login to access the Contact Form !

About Us : Team AstroPython

Tom Aldcroft is a Science
Operations Team member and Flight Director for the Chandra X-ray Observatory and works at the Smithsonian Astrophysical
Observatory. He uses Python for Chandra
spacecraft operations analysis as well as research on several X-ray
survey projects. He is the author of the
asciitable,
cosmocalc, and
deproject
packages. He is also
interested in Python for the web and writes Django and Google App Engine applications
for presenting large multi-wavelength survey datasets. He has been the primary mentor for AstroPython

Thomas Robitaille is a Spitzer
postdoctoral fellow at the Harvard-Smithsonian Center for Astrophysics. He works on
studying Galactic star formation, mostly through mid-infrared to mm
wavelengths. This includes modeling multi-wavelength observations of young
stars and studying the global properties of star formation in the
Milky-Way. In his spare time he is a keen Python programmer and is one of
the co-developers of the APLpy and
ATpy packages. He also developed
the IDLSave package.He has been the secondary mentor for the project.

Aman Jhunjhunwala is an undergraduate Computer Science student with a passion for Big Data,Machine Learning and Web technologies. He has been the primary developer of this site as a part of Google Summer of Code-2015 programme under the AstroPy project.