Basic file logging - changing filename periodically

This is the place for queries that don't fit in any of the other categories.

Basic file logging - changing filename periodically

Postby halian » Thu Mar 28, 2013 8:00 am

Hi

I am using the "logging" module - in a simple manner - to log activities of a daemon process to a file. The process is expected to be running for days - and I would like the log filename to be different for each date -dproc.yyyy-mm-dd.log - e.g.
dproc.2013-03-24.log
dproc.2013-03-25.log
dproc.2013-03-26.log
etc...

In my code I periodically call a set logging function:-

Code: Select all
import logging...
etc...

def setLogging( logdir ):
   """
      Log file is <logdir>/dproc.yyyy-mm-dd.log
   """
   today = datetime.date.today()
   log_dt = str(today)
   logfile = logdir + "/dproc." + log_dt + ".log"
   logging.basicConfig(filename=logfile, format='%(asctime)s %(message)s', level=logging.INFO)


However this does NOT change when the date changes. I now understand that logging.basicConfig() has no effect once a default handler is setup and active.

Questions : Can I change the filename in some way - and if so how? I suppose a supplementary question is - should I be using another approach? (I'm not particular keen on setting up log rotation unless it can be by date).

Thanks
halian
 
Posts: 3
Joined: Thu Mar 28, 2013 7:41 am

Re: Basic file logging - changing filename periodically

Postby metulburr » Thu Mar 28, 2013 8:33 am

I have never used the logging module before, so not sure how much i could help

Are you running this function once upon setup, or are you running it every time?
Code: Select all
def setLogging( logdir ):

because if you run it on initial set up, it is getting the date of the intial set up, but never updating
Code: Select all
today = datetime.date.today()
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1469
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Basic file logging - changing filename periodically

Postby halian » Thu Mar 28, 2013 8:39 am

Sorry - I should have been clearer.

It gets called more than once. Not every time any logging is to be performed, but once each time the program goes through its main loop (in a realistic situation this will be once every 30 minutes or so). This does mean that some messages for e.g. 25/03 might end up at the tail end of the log for 24/03 - but that's fine.
halian
 
Posts: 3
Joined: Thu Mar 28, 2013 7:41 am

Re: Basic file logging - changing filename periodically

Postby metulburr » Thu Mar 28, 2013 9:01 am

i checked the docs, but what it said didnt work for me, however someone did say to reload the module, which that did work

python 3.x for reloading
Code: Select all
import logging
import imp

for index in ['test.log','test2.log']:
   imp.reload(logging)
   logging.basicConfig(filename=index,level=logging.DEBUG)

   logging.debug('This message should go to the log file in {}'.format(index))
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1469
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Basic file logging - changing filename periodically

Postby halian » Thu Mar 28, 2013 9:21 am

I think that might be the way I have to go - I can track the current date/time against the value set in "logging" - and use reload if the date has changed.

I'm surprised it's not easier to do than this - however I realise I'm using "logging" in its simplest form.

Thanks for the quick replies.
halian
 
Posts: 3
Joined: Thu Mar 28, 2013 7:41 am


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 4 guests