Wednesday, August 17, 2011

Log (logging) in Python

Logging is always an important topic when you try to develop a "real-world" app. I once wrote my own logging system, but apparently the best practice is to use something provided by Python Standard Library, not to reinvent the wheel.

Here, I have no intent to write a complete tutorial of teaching the python's logging framework. I extract the most useful/simple part(s) of logging from my viewpoints, which I believe it solves 90% cases I(you) need.


Firstly, you should try to build a customized logger. It is impractical to use the logging module directly via changing basicConfig() because you cannot tell where the log comes from if you have multiple modules in your app. This is one of the reason why I write this article.

To get a "customized" logger, firstly you should assign a name to it.
mlogging = logging.getLogger("test_log") #Get a logging for this module

If you would like to have some beautiful format on logging records, logging.Formatter is something you need to set it up. Sure you should do it because the default one seems to be a little clumsy. To change the date format (that is, %(asctime)s), please refer to things used in time.strftime(). And to create a format for a record, the related variables can be found in attributes of LogRecord.

# -- Set up Formatter --
log_format = '%(asctime)s(%(name)s--%(levelname)s):%(message)s'
log_date_format = '[%m/%d/%y %H:%M:%S]'
uniform_fmt = logging.Formatter(fmt=log_format, datefmt=log_date_format)

Mostly, we store logs in a file, so you need a file handler.
# -- Log to File --
hdlr = logging.FileHandler('OnlyOneFile.log')   # or "mlogger.name + 'log' if you would like to separate logs
hdlr.setFormatter(uniform_fmt)

If you would like to see log in your screen, say, for debugging, you have to link it to STDOUT.
# -- Log to Console --
hdlrConsole = logging.StreamHandler(sys.__stdout__)
hdlrConsole.setFormatter(uniform_fmt)

Do not forget to link your handler with your logger.
mlogging.addHandler(hdlr)
mlogging.addHandler(hdlrConsole)

Finally, you have to assign a log level so that you can get all the details. Otherwise, The default level is WARNING. (All default levels)
mlogging.setLevel(logging.DEBUG) #this is the most detailed level

Putting all together, here is an example:
import sys
import logging

mlogging = logging.getLogger("test_log") #Get a logging for this module
"""
logging.basicConfig(
    #filename=mlogging.name + '.log', #Use the name of logger as log file name
    filename='onefile.log', #Use the name of logger as log file name
    level=logging.DEBUG,
    format='%(asctime)s(%(name)s--%(levelname)s):%(message)s',
    datefmt='[%m/%d/%y %H:%M:%S]'
"""

# -- Set up Formatter --
log_format = '%(asctime)s(%(name)s--%(levelname)s):%(message)s'
log_date_format = '[%m/%d/%y %H:%M:%S]'
uniform_fmt = logging.Formatter(fmt=log_format, datefmt=log_date_format)

# -- Log to File --
hdlr = logging.FileHandler('OnlyOneFile.log')
hdlr.setFormatter(uniform_fmt)

# -- Log to Console --
hdlrConsole = logging.StreamHandler(sys.__stdout__)
hdlrConsole.setFormatter(uniform_fmt)

mlogging.addHandler(hdlr)
mlogging.addHandler(hdlrConsole)
mlogging.setLevel(logging.DEBUG)

mlogging.debug('This is debug msg')
mlogging.info('Ok~ info now')
mlogging.warning('Warning here')

try:
    x = 4/0
except Exception as e:
    mlogging.exception(e)

Before we leave the discussion, you should pay attention to mlogging.exception(e). This is the way we log an exception, including its trace-back information.

Tuesday, July 12, 2011

Git Version Control Resource

I'm a heavily Subversion (SVN) developer. Recently, I join several projects that use git as their version control frameworks. Thus, I document some useful resource as follows:


To learn more about git:

Firefox 5 on ubuntu 10.04 LTS

For people who are using ubuntu 10.04, the default version of Firefox is 3.6. (Seriously?) It's suggested that you update to the latest stable version of ubuntu.

To install the latest "stable" version of ubuntu, add the following ppa repository:

sudo add-apt-repository ppa:mozillateam/firefox-stable


This is nothing more than add a mozillateam-firefox-stable-lucid.list to "/etc/apt/sources.list.d".

Than, do "sudo apt-get update && sudo apt-get upgrade" and you are done.

Tuesday, March 8, 2011

MySQL Remote Access

By default, the mysql (5.x) allows login within localhost. What if you need to access a MySQL database from another computer?

I found this tutorial is quite helpful:
how-do-i-enable-remote-access-to-mysql-database-server

Unfortunately, I still get the following errors
chucheng@laptop:~$ mysql -ufoo -pcrap mysql.ucla.edu
ERROR 1044 (42000): Access denied for user 'foo'@'laptop' to...

After trying several solution, I finally realized that in the statement GRANT ALL ON db_name.* TO foo@'%' IDENTIFIED BY 'PASSWORD'; you cannot skip "IDENTIFIED BY password".

It turns out that you will "override" existing passwords with an empty password if you did not provide corresponding inputs.

Just in case that you still have problems:
Please make sure that
(1) Your bind-address in /etc/mysql/my.cnf has been changed to "real" IP.
(2) your port is listening
netstat -an | grep 3306
(3) You grant remote access permission to the user accounts.
p.s if you encounter any problems when login from localhost, instead of granting foo@'%', please grant foo@'localhost' as well.

Friday, February 4, 2011

Ubuntu Application

I would like to document several apps that, I believe, are very useful as follows. Though there are many choices in WWW. The followings are really handy ones.

Programming Tools:
  • WingIDE (Python)
  • IntelliJ IDEA (Java)
  • Eclipse (C++/Java)
  • Rabbit VCS (Version Control)

MISC:
  • Mendeley
  • Stardict (Dictionary)

Tuesday, February 1, 2011

Brightness Control on T410 (T410s)

I find the brightness control seems to be not working after installing ubuntu 10.04 on my Thinkpad T410s.

Here are two solutions:

(1) Switch to console (CTRL+ALT+F2) and adjust the brightness. Then switch back to GNOME (CTRL+ALT+F7)

(2) Edit /etc/X11/xorg.conf
Under the "DEVICE" section, add the following OPTION
Option "RegistryDwords" "EnableBrightnessControl=1"


Done! Enjoy it!

Tuesday, January 25, 2011

Install Sun Java SDK 6 on Ubutnu 10.04

Since Ubuntu 10.04, Sun(Oracle?) SDK is not existed in multiverse anymore.

If you would like to install Sun Java SDK, you have to use "partner" repository.


sudo add-apt-repository "deb http://archive.canonical.com/ lucid partner"
sudo apt-get update
sudo apt-get install sun-java6-jdk


Is that all? Not quitely.

Choosing the default Java to use



Just installing new Java flavours does not change the default Java pointed to by /usr/bin/java. You must explicitly set this:


* Open a Terminal window
* Run sudo update-java-alternatives -l to see the current configuration and possibilities.
* Run sudo update-java-alternatives -s XXXX to set the XXX java version as default. For Sun Java 6 this would be sudo update-java-alternatives -s java-6-sun
* Run java -version to ensure that the correct version is being called.

You can also use the following command to interactively make the change;

* Open a Terminal window
* Run sudo update-alternatives --config java
* Follow the onscreen prompt

Ref: https://help.ubuntu.com/community/Java