Get an invest logfile when running from a python script

Hi!

I have been running the urban flood risk mitigation model through Python scripting and each time it finishes there are no log files generated. I believe there is an example of saving log messages to the log file here: Tutorial: Batch Processing on Windows — InVEST 3.9.0.post226+g36cc3d2e documentation, however this only creates the logfile and it is still empty.

I am wondering if the reference for the urban flood risk model to log the steps of the calculation is
‘MODEL_LOGGER = logging.getLogger(‘natcap.invest.urban_flood_risk_mitigation’)’ because this did not work for me.

If there are any other implementations or ways to get a logfile in order to help debug, please let me know.

Thanks!

Hi @rxchelzhxng ,

If you created your script by saving it through the User Interface (DevelopmentSave to Python script) you should see something like this at the top of the file:

# coding=UTF-8
# -----------------------------------------------
# Generated by InVEST 3.9.0 on Thu Mar 25 13:46:05 2021
# Model: UrbanFloodRiskMitigation

import logging
import sys

import natcap.invest.urban_flood_risk_mitigation
import natcap.invest.utils

LOGGER = logging.getLogger(__name__)
root_logger = logging.getLogger()

#handler = logging.StreamHandler(sys.stdout)
handler = logging.FileHandler("logfile.txt")

formatter = logging.Formatter(
    fmt=natcap.invest.utils.LOG_FMT,
    datefmt='%m/%d/%Y %H:%M:%S ')
handler.setFormatter(formatter)
logging.basicConfig(
    level=logging.DEBUG, handlers=[handler])

We default to logging everything to the console but you can switch that to a file if you’d like. Note the differences between the lines:

#handler = logging.StreamHandler(sys.stdout)
handler = logging.FileHandler("logfile.txt")

That should do the trick! Thanks for also pointing out that some of our docs might be a little out dated.

Cheers,

Doug

Thanks for the updated API call to get the logfile, but in my case I am not creating by script by saving it through the User Interface. Instead, I am running the urban flood risk mitigation model by calling the API natcap.invest.urban_flood_risk_mitigation.execute(args).

Would adding this before the execution of the API call of the flood risk model work?

Thanks!

Rachel

Hey Rachel,

Yep, it should do the trick! That above code I posted sits above, ultimately, this call:

if __name__ == '__main__':
    natcap.invest.urban_flood_risk_mitigation.execute(args)

Doug

Yep that did! Thanks so much Doug :slight_smile:

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.