FeedBurner |
Python Logging: RotatingFileHandler Whenever you write a computer program that is going be a service is recommended that this program saves somewhere what is doing, most of the popular services do it (Apache, Cherokee, MySQL, Put your service here:___________________), or maybe you have a desktop application where you want to write what is doing maybe for debug purposes.Fortunately this is an easy task if you use Python's Logging module. This module have several handlers, some of them to write to the standardError, to a Socket, to the syslog and many others One of my favorite handler is the RotatingFileHandler This module creates a log file, when this file is full (if you set the maximum bytes per file flag) it is renamed, appending the .1 and a new file is created with the same name as main log file. You can set up the max number of files to be created. This module (logging) is good, easy to use, but you have to use the RotatingFileHandler with care, or you will have two, three or more files being filled and then, your logs will be in N files.. When you create your Logger Object you can set the log level, which can be INFO, WARNING, DEBUG, ERROR, CRITICAL and EXCEPTION, you already realized which level prints what. You can set the Handler, and this is where using the RotatingFileHandler becomes a bit trickier. In your applications you may want to have several loggers, one for each module you have in your application. If you plan to use the RotatingFileHandler you have to use the same handler for every Logger you are creating if you plan this Logger to use the same file. If you don't do this then you will end with something saving logs in your main file, and every other data will be stored somewhere else. This is because when you try to save a something in your log, the handler check the log size, and if the file size is >= maxbytes makes the rollover, if you are using several handlers related to the same file, then the handler that realize first that the size of the log file reach its max size will do the rollover but this will not take effect in the other handlers, so, one handler will be logging in the newly created file while the others will be logging to the old file and now you have two files growing. To avoid this, just use the same fucking handler for every logger you are creating if you plan to use the same file. Another issue I face with logger what the fact that if you call two time s to the same logger.. I mean:
a = logging.getLogger('chanchanchan')
When you make something like this:b = logging.getLogger('chanchanchan')
a.info('This is a test')
You'll se this:
This is a test
Yes, you are calling a to write "This is a test" but in your log file appears two times (or N times you have created a logger with the same name). So, its better for you to have something like a manager that gives you the logger already created with that name if exists or create it for you.This is a test Let's do an example: This is a small program that logs something in a endless cycle:
#!/usr/bin/env python # -*- encoding: latin-1 -*- import sys import time from logger1 import LoggerManager a = LoggerManager().getLogger('a') b = LoggerManager().getLogger('b') c = LoggerManager().getLogger('c') while 1: t = time.time() for index,i in enumerate((a,b,c)): msg = str(index) + repr(t) i.debug(msg) time.sleep(0.005)
#!/usr/bin/env python import logging import logging.handlers from Singleton import Singleton import os if os.name == 'nt': LOGPATH = 'C:\\' else: LOGPATH = './' class LoggerManager(Singleton): def __init__(self): self.loggers = {} formatter = logging.Formatter('%(asctime)s:%(levelname)-8s:%(name)-10s:%(lineno)4s: %(message)-80s') level = 'DEBUG' nlevel = getattr(logging, level, None) if nlevel != None: self.LOGGING_MODE = nlevel else: self.LOGGING_MODE = logging.DEBUG self.LOGGING_HANDLER = logging.handlers.RotatingFileHandler( os.path.join(LOGPATH, 'log_event.log'),'a',524288, 10) self.ERROR_HANDLER = logging.handlers.RotatingFileHandler( os.path.join(LOGPATH,'log_error.log'),'a',524288, 10) self.LOGGING_HANDLER.setFormatter(formatter) self.LOGGING_HANDLER.setLevel(self.LOGGING_MODE) def getLogger(self, loggername): if not self.loggers.has_key(loggername): logger = Logger(loggername, logging_handler= self.LOGGING_HANDLER, error_handler = self.ERROR_HANDLER, logging_mode = self.LOGGING_MODE) self.loggers[loggername] = logger return self.loggers[loggername] class Logger: ''' Implements the christine logging facility. ''' def __init__(self, loggername, type = 'event', logging_handler= '', error_handler = '', logging_mode = ''): ''' Constructor, construye una clase de logger. @param loggername: Nombre que el logger tendra. @param type: Tipo de logger. Los valores disponibles son : event y error por defecto apunta a event. En caso de utilizarse otro que no sea event o error se apuntara a event. ''' # Creating two logger, one for the info, debug and warnings and #other for errors, criticals and exceptions self.__Logger = logging.getLogger(loggername) self.__ErrorLogger = logging.getLogger('Error'+ loggername) # Setting Logger properties self.__Logger.addHandler(logging_handler) self.__Logger.setLevel(logging_mode) self.__ErrorLogger.addHandler(error_handler) self.__ErrorLogger.setLevel(logging_mode) self.info = self.__Logger.info self.debug = self.__Logger.debug self.warning = self.__Logger.warning self.critical = self.__ErrorLogger.critical self.error = self.__ErrorLogger.error self.exception = self.__ErrorLogger.exception Vim != vim
Vim != vim Originally uploaded by markuz From fabaff.blogspot.com WTF Evolution!!
WTF Evolution!! Originally uploaded by markuz No unread mails in my Inbox, but, in the Unread folder looks like there are 5 unread... select the folder and surprise! there is no messages in that folder.. As a plus in the window title it says that there are 5 unread messages from a total of -142 (yes, a negative value!) messages in the folder. Christine 0.2.0 alpha 7 months ago I make the 0.1.6 release of Christine media player. The project continues working on my free time and the free time of some friends. I'd like to work more on it but I just don't have all that time, and wait until everything gets perfect will take a lot. So I'm giving to the christine users the 0.2.0 alpha version. I have been using this "version" a couple weeks ago and seems to be working fine, but I'd like you to check it and report everything you find wrong in it. There are source packages in bz2 and gz and a .deb package for Ubuntu Intrepid. This release include the SQite3 as data storage engine, the drop of gconf dependency, early support for plugins and more optimizations that make christine load and work faster. C'mon, go and Get it!. Typical desktop
Typical desktop Originally uploaded by markuz This is more or less the number of applications open in every day work. Many people say that using window managers like ion3 or awesome improves productivity... I'm fine with compiz maybe it's aimed to eye candy, but some plugins help a lot. Call for testers and translatorsHi, this is a call for contributors for the new christine release, the christine development team have been working on improving christine, and think it is time to make a new release, but before, we want you to test it and report every single fail you detect. We also make use of launchpad's translation page.
You can find the code Here, the page for tracking bugs is Here, and the translations are Here. Netbeans 6.5Glad to hear that Netbeans 6.5 release shows an Early Access Netbeans IDE for Python
2008-11-16Hi again readers!.
In this week I have been pretty busy, bugfixing the Juvi desktop application. We don't want Banana products (those that mature with the user) and the test team is doing their best to hit every bug and report it to have a very stable application. I have been also busy "porting" the Supramax Evo4 driver to run on windows, the port is quite easy to do since the program was written in Python and I have to fix some paths. In my tests I was doing to the driver I found a We have a parameter in the driver, as most Unix programs do the "-v" do a verbose run, spitting to the stdout the logs, if you don't use the verbose parameter then it saves it somewhere. Well, running on verbose mode was easy, it just run perfectly, storing the logs wasn't I had an error with OpenSSL... but wait.. we where talking about logs and not about OpenSSL.. what does OpenSSL has to be with logs?. Well, there is an issue with M2Crypto and Python where it doesn't ship a special file and this causes OpenSSL crash if you use I/O. M2Crypto let you load a RSA key in pem format that you can use to crypt/decrypt something this method is called load_key and is part of the M2Crypto's RSA package, you only have to pass the path to the key as first argument and in the second argument a function (anything callable) that returns the password for the key (if it is private). Well, this I/O was crashing M2Crypto. There are two workarrounds for this. The first, is to compile at least M2Crypto (I didn't do it, some blogs says that you have to compile python) against OpenSSL and include this specific file... at least Google points to blogs that says that... The other and easier way is just use RSA's function load_key_string where you pass as first argument the string that holds the key instead the path, doing this you will eliminate the I/O done by OpenSSL and then, there is no crash. Last Thursday Cristina and I went to Maquiavelo's house to an informal meeting with xbitcarry, unfortunately Maquiavelo was too busy and the meeting wasn't really done but was nice to see xbitcarry again. I think that's all for today my readers. Fortunately I don't have to work tomorrow and now I have to work a bit on christine Christine with visualizationsChristine with visualizations Originally uploaded by markuz Book memeSiguiendo a Rodrigo Garcia
- Nuevo libro del embarazo y nacimiento dra. Miriam Stoppard Let me check my Address book :-P
Let me check my Address book Originally uploaded by markuz I remember when I went to Villahermosa.... I went to see cristina, but in this picture I was looking Liliana's phone number. Anyone who knows me knows that my memory is not good with numbers, names or dates. In the picture is my first laptop "voladora" (because I bought it in Papantla, Veracruz). 2008 11 11
dsc08337.jpg Originally uploaded by markuz Time to post again, and again I have almost nothing interesting to say, but I don't care, I will not dig in my memories to entertain you, Just want to say what I have done in the past days. Visitors Statistics3 772 595368 Technorati |
Recent Comments On Blog