Forums

Looking for help redirecting log output

Hi,

I run the same script several times a day which generates a separate log for each run. Each day the log corresponding to the scripts run time gets appended to. This makes it awkward to track events through time as I have to open each log in turn and find the relevant section that follows from a time-stamp in the previous log.

I am trying to output logs for all runs on the same day to one daily file using this code

my_home = '/home/myUserName/'
my_log_date = datetime.date.today().strftime('%Y%m%d_%A_')
my_log_file = os.path.join(my_home, "%s%s" %(my_log_date, 'sms_scheduler.log'))
logging.basicConfig(filename=my_log_file, level=logging.DEBUG)
logger = logging.getLogger("web2py.app.myApp")
logger.setLevel(logging.DEBUG)

logger.info("log file is < %s >" % my_log_file)

All my logger.info() output appears in usual scheduler log files (great!) but not in my_log_file which isn't even created.

The output for the last line shows my filename as

< /home/myUserName/20170824_Thursday_sms_scheduler.log >

What do I need to do to get logging redirected to my log file?

Many thanks for any help!

Hi there, That all looks correct to me -- I'm not sure what the actual problem might be, but I will say that I always have headaches with the python standard logging module. It's not quite an answer to your question, but maybe this is of interest? https://julien.danjou.info/blog/python-logging-easy-with-daiquiri

Thanks Harry!
I'll try it out over the next day or three and will report back.

Lots more to try out but for anyone else struggling with logging this worked for me...

import daiquiri
import daiquiri.formatter # not used yet!
import logging

my_log_date = datetime.date.today().strftime('%Y%m%d_%A_')
my_log_file = "%s%s" % (my_log_date, 'sms_scheduler.log')

daiquiri.setup(level=logging.INFO,
                        outputs=(daiquiri.output.File(my_log_file, 
                                       level=logging.INFO),
                        daiquiri.output.TimedRotatingFile('sms_error.log', 
                                       level=logging.INFO,
                   interval=datetime.timedelta(hours=1))
    )
)

logger = daiquiri.getLogger("myAppName")

print("Log file:  <%s> " % my_log_file)    # shows in default scheduler log
logger.info("Some log message")           # shows in my_log_file

thanks again for the pointer Harry!

My script calls a web2py controller where the default log directory turns out to be the web2py root directory and haven't checked out whats happening with the error log yet

Hope this helps someone else.
P

Many thanks for posting that!