A Simple Log Helper for Python

Just wanted to share this simple log helper method I came up with after some research, which can help you quickly setup Python logging and focus on your main app logic instead of dangling around in logging documentation.

The method is very simple, takes a filename and two other optional arguments, and returns ‘logging’ object, and you can start logging in your Python projects in no time!

Here’s the code:

#file log_helper.py
import os, sys
import logging

def logHelper(fileName, logLevel=logging.INFO, useConsole=True):
    """
    Simple Logging Helper. Retuens logger reference.

    Paramsmeters:
    fileName: Filename, may include full path, or will open a file in default folder
    logLevel: Pass logging.INFO, logging.DEBUG or other enums for logging level
    useConsole: If Ture, will also dump log to console
    """

    ##### init logging
    log = logging.getLogger()
    log.setLevel(logLevel)
    logFormatter = logging.Formatter("%(asctime)s | %(threadName)-12.12s | %(levelname)-5.5s | %(message)s")

    ##### file handler
    fileOut = logging.FileHandler(fileName)
    fileOut.setFormatter(logFormatter)
    log.addHandler(fileOut)

    ##### console handler
    if useConsole:
        consoleOut = logging.StreamHandler(sys.stdout)
        consoleOut.setFormatter(logFormatter)
        log.addHandler(consoleOut)

    ##### return reference
    return log

And usage is simple. Passing just filename defaults to ‘logging.INFO’ log level, and console output turned on:

#import module
import log_helper as log

# get logger
log = log.logHelper('logfile.log')
log.info("Got logger")

Or use optional arguments to set up logging level and console output:

#import module
import log_helper as log

# get logger
log = log.logHelper('logfile.log', logging.WARN, False)
log.warn("this is a warning message")

Additionally, you can always tweak the method as per your needs, for rotating file handler, additional output handlers and log output format.

Check out Python’s documentation for ‘logging’ module here for more info.

Bugs reports? Suggestions? Let me know if you found this useful. Leave a comment 🙂

Cheers,

// Sohail

Deploying Network Services behind Corporate Firewalls: Net.TCP Port Sharing in WCF

Just found a good feature of WCF, which seems quiet useful for distributed application developing/deployment practices. Through Net.TCP Port Sharing Service, Windows Communication Foundation (WCF) allows different processes to share the same TCP Ports. Now you can deploy your .Net based network services side-by-side with IIS on port 80 or 8080, the standard HTTP and HTTP Proxy ports you find open in many firewalls by default. No need to mess around with client

uTorrent for Mobile

Wait a minute… you think you can leech some bits of torrents from every public hotspot that comes your way? No sir, that’s not the case (at least for now). This neat piece of software is not a torrent client, it actually lets you control the uTorrent from your mobile, ideally on Wi-Fi in home or office, where you have uTorrent running with WebUI installed.

I just happen to setup WebUI for myself and had this wild idea to get the remote control on my mobile, and guess what, “Your best idea is already copyrighted.” proves true again!

These guys here have done a great job actually. The uTorrent Remote Control is done in J2ME, with a simple interface, lets you control and monitor torrent activity on your uTorrent application.

I also found some other useful bits here on the same site, but I’m in love with this torrent thingy I tell you that. Go try it for yourself!

Cheers,

// ch3ckmat3

More e-paper, more e-book readers

The scene has now become interesting with a number of e-paper based portable e-book readers on the market, and we are getting a brand new gadget every once in a while in this pool now.

Just like BeBook, yet another cool e-book reader on the block…but still no luck with the prices…the asking price for this gizmo is huge 329.95 EUR with converts to almost 510 USD!!!

The success of Sony Reader and Amazon Kindle Reader proves that even latest large screen mobile phones and other hand-held media gadgets are insufficient for reading books, specifically for mature readers.

For other options available, here is a good Feature Matrix on E-book Readers I found on MobileRead Wiki.

cheers,

// ch3ckmat3

Eid Mubarik & my brand new blog theme :)

Eid Mubarik to all Muslims around the world 🙂 Eid-ul-Fitr is truly a family event and my best wishes to those who are abroad and for any reason, couldn’t make it home with their families. 

Me and Faisal has also upgraded our blogs to WP2.3 with brand new themes from N.Design Studio… And these are really cool Web 2.0 designs. Thank you Nick La for great themes. Your work is really cool.

Enjoy your Eid Moments and Happy surfing!

// ch3ckmat3

e-book: for the bookworms

iRex Technologies has come up with yet another cool application of e-paper… iLiad, a portable tablet device which also lets you write on its e-paper display.

Software features are cool, technical specifications look promising but its price, which is US$699, is a little too high for a gadget like this. I’ll definitely buy  it when it will come under US$300 🙂

Features suggest that this device is the most ultimate one in the electronic reading/writing devices available on the market.

Seems like this e-paper stuff is going to replace conventional paper and “analog recoding devices” (pen and pencils) with their digital counterparts pretty soon.

Cheers

// ch3ckmat3