how to write log data to file

Hi,
I am trying to write the log messages when PRODIGY_LOGGING=verbose into file, but it does not work.
In the documentation it says python logging module is used. But I can not use its methods.
Thanks,
Ati

1 Like

What have you tried? And does the following work? This should write all output (including what’s printed) to a file:

PRODIGY_LOGGING=verbose prodigy [...] &> file.log

Thanks Ines.
I want to do this inside a python script not on the command line.

Thanks for your reply Ines. More specifically, how can prodigy.log be accessed through python logging package, so it can be written to a FileHandler object?

1 Like

What happens if you set the logging config for level="INFO" after you import prodigy?

The file.log is empty and I see all the log messages in the terminal where I run the code from:

    import prodigy
    import os
   
    os.environ['PRODIGY_LOGGING'] = 'basic'

    logger = logging.getLogger('prodigy.log')
    logger.setLevel(logging.INFO)

    # create a file handler
    handler = logging.FileHandler('file.log')
    handler.setLevel(logging.INFO)

    # add the handlers to the logger
    logger.addHandler(handler)
    
    # message to test
    log('test')

This is what I see in the terminal
18:03:05 - test

Ahhh, I think the problem here is that the logger isn’t actually called prodigy.log – that’s just Prodigy’s logging utility function, so you can do from prodigy import log.

I just checked and our logger doesn’t actually have a name, which is bad and should probably be fixed for the next release. Can you use logging.basicConfig after importing Prodigy to override the config?

This is what I did and still failing to have log messages in the file. file.log is created but it is empty, and I get 09:53:44 - test in the command line.

import logging
import os
from prodigy import log

handler = logging.FileHandler('file.log')
handler.setLevel(logging.INFO)
logging.basicConfig(level='INFO', handler=handler)

os.environ['PRODIGY_LOGGING'] = 'basic'
log('test')

logging and log from prodigy look disconnected and two separate entities.

Prodigy’s log really just calls logging.info, nothing more. What’s currently unideal is that we’re not naming the logger and calling into logging.basicConfig directly. I’ll fix that for the next version.

In the meantime, you’d have to set your config first, because the first config that gets registered “wins”. If I run the following, it works for me:

import os
import logging
logging.basicConfig(level=logging.INFO, filename="file.log", format="%(asctime)s - %(message)s", datefmt="%H:%M:%S")

from prodigy import log
os.environ['PRODIGY_LOGGING'] = 'basic'
log("hello world!")
1 Like

Thanks a lot Ines.
It is working finally :slight_smile:
You had said after importing prodigy :wink:

Ahhh, sorry, my bad! But glad it works now :+1:

Just a quick update: v1.8 should now have a logger named "prodigy"!

1 Like