python logger close file
1
logger = ??
handler = ???
formatter = ???
http://docs.python.org/howto/logging-cookbook.html
Multiple calls to logging.getLogger('someLogger') return a reference to the same logger object. This is true not only within the same module, but also across modules as long as it is in the same Python interpreter process. It is true for references to the same object; additionally, application code can define and configure a parent logger in one module and create (but not configure) a child logger in a separate module, and all logger calls to the child will pass up to the parent. Here is a main module:
http://docs.activestate.com/activepython/3.1/python/library/logging.html
http://docs.python.org/library/logging.html
2 Close logger file
http://stackoverflow.com/questions/5296130/restart-logging-to-a-new-file-python
You can manually re-assign the handler if you want using the
removeHandler and addHandler OR, you can access logger.handlers[index_of_handler_here].stream and replace the stream manually, but I'd recommend the former over the latter.
| |||
http://rlabs.wordpress.com/2009/04/09/python-closing-logging-file-getlogger/
Now, make two different classes and call this function. Even though destroying the class and calling the other one, the records will be duplicated during your second call.
To make it work, it´s a bit complicated to find, and here is the solution:
Inside your __del__(self) function, include:
x = logging._handlers.copy() for i in x: log.removeHandler(i) i.flush() i.close()
PS: I´m supposing that “log” is your return for the function, your in other words, the result of logging.getLogger(..) .
I hope it saves some hours of your work
Python: closing logging file (logging.getLogger)
9042009
This one was a bit hard to figure out. I´m doing some spiders and using logging.getLogger interface to make a good log file.
I´m calling on wrapper to generate a log file, and calling this wrapper twice, makes the logger generate duplicated lines.
Just to make the things a bit clear, take a look at this routine:
import logging import logging.handlers def makelog(filepath): logger = logging.getLogger('applog') # remove diretorio dir = os.path.split(filepath) dir = diretorio[0] # cria dir tree if not os.path.exists(dir): os.makedirs( dir ) # rotate 2mb de log, in 5 files hdl = logging.handlers.RotatingFileHandler( filepath, maxBytes=2097152, backupCount=5) formatter = logging.Formatter('%(asctime)s %(module)s, line %(lineno)d %(levelname)s %(message)s') hdl.setFormatter(formatter) logger.addHandler(hdl) logger.setLevel(logging.DEBUG) return logger
Now, make two different classes and call this function. Even though destroying the class and calling the other one, the records will be duplicated during your second call.
To make it work, it´s a bit complicated to find, and here is the solution:
Inside your __del__(self) function, include:
x = logging._handlers.copy() for i in x: log.removeHandler(i) i.flush() i.close()
3 http://www.mechanicalcat.net/richard/log/Python/Simple_usage_of_Python_s_logging_module
Richard Jones' Log: Simple usage of Python's logging module
Tue, 09 Mar 2004
Several people have posted about Python's new logging module. Most are confused by its complexity and inaccessible documentation (great reference, where's the introduction?) Well, I've used it once now, and here's the simplest I could make it:
import logging logger = logging.getLogger('myapp') hdlr = logging.FileHandler('var/myapp.log') formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) logger.addHandler(hdlr) logger.setLevel(logging.INFO)
And then to use it:
logger.info('a log message')
... of course, for most people, the ideal usage would be:
import logging logger = logging.open('var/myapp.log')
Gee, that'd be nice. Maybe I'll submit that as a patch, in one of my spare moments.
I fully sympathise with Richard's view that the common use case of logging to a file should be made easier. I will try to come up with something easier. However, a logger is not analogous to a file - rather, it is an event sink which can be routed to many different listeners, one of which might be a file. Hence comparisons with Twisted's logging do not seem accurate to me (based on a quick reading of Twisted's logging API in epydoc format). From what I can see, in Twisted's view, a logger appears analogous to a file or stream. If I'm wrong, please point me in the right direction.
Response to Stuart Bishop's comment about the config file format, which Richard says is "horribly complicated":
You could just use the Tkinter-based GUI application to set up a configuration for your application, and call fileConfig() to make the configuration effective. Why should you care what "cruft" is in the .ini file? You need never look at it. Some of the apparent cruft is there only to support the GUI configurator. Of course if someone can suggest a better config file format (while still sticking with ConfigParser), all suggestions will be gratefully accepted. I'm not suggesting that what's there now is optimal. If you have Copious Free Time to look at alternative configuration implementations, I'd be happy to hear suggestions.
Response to john's comment:
Please file a bug report on SourceForge. Closing handlers used to be problematic, but I am not aware of specific problems with current Python CVS or standalone version 0.4.9.2 (http://www.red-dove.com/python_logging.html#download).
Response to Jp Calderone's comment:
Twisted's logging package may well be easier for certain specific setups. But can you substantiate that "twisted.python.log is capable of all the same things the logging package is"? For example, emailing logs, logging to HTTP, TCP, UDP, SOAP servers, logging to Unix syslog and NT's event log? Perhaps the logging package is over-engineered for your needs, but that's not to say it's a commonly held view - not everyone's needs are the same.
Response to Stuart Bishop's comment about the config file format, which Richard says is "horribly complicated":
You could just use the Tkinter-based GUI application to set up a configuration for your application, and call fileConfig() to make the configuration effective. Why should you care what "cruft" is in the .ini file? You need never look at it. Some of the apparent cruft is there only to support the GUI configurator. Of course if someone can suggest a better config file format (while still sticking with ConfigParser), all suggestions will be gratefully accepted. I'm not suggesting that what's there now is optimal. If you have Copious Free Time to look at alternative configuration implementations, I'd be happy to hear suggestions.
Response to john's comment:
Please file a bug report on SourceForge. Closing handlers used to be problematic, but I am not aware of specific problems with current Python CVS or standalone version 0.4.9.2 (http://www.red-dove.com/python_logging.html#download).
Response to Jp Calderone's comment:
Twisted's logging package may well be easier for certain specific setups. But can you substantiate that "twisted.python.log is capable of all the same things the logging package is"? For example, emailing logs, logging to HTTP, TCP, UDP, SOAP servers, logging to Unix syslog and NT's event log? Perhaps the logging package is over-engineered for your needs, but that's not to say it's a commonly held view - not everyone's needs are the same.
logger.handlers[0].stream.close()
first. – Mahmoud Abdelkader Mar 14 '11 at 19:06