2

If I have a python app running in Google App Engine, how can I dynamically change the app's logging level during runtime? The app's logging level is currently done via a logging config file.

One brute force way is to update the logging config file locally, and run 'appcfg.py -R update path_to_product'. But this will update all modified files.

Is there a better way to do this?

I looked at dictConfig and fileConfig. But using these methods would not update the logging configuration of running processes (especially those running on different app engine instances).

5
  • 1
    I think you'd have to store a parameter in the data store. You could create a Settings entity that you modify from the console. Your logging code, would then check the settings entity and act accordingly.
    – minou
    Jul 20, 2015 at 19:56
  • 1
    just a quick note: you should use the /_ah/start handler to check this on instances start-up and store the config in an environment variable, to avoid round-trips to datastore every time the level changes. You should also provide a means for updates to the logging info to notify the instances which have already been created so they can react dynamically to changes.
    – Nick
    Jul 20, 2015 at 20:26
  • 1
    ...that or you could simply filter by log level in whatever log reader you use and log things appropriately to their level?
    – Nick
    Jul 20, 2015 at 20:27
  • Thanks Kekito and Nick. I currently use python's logging module and logs events at different levels.
    – morfys
    Jul 20, 2015 at 21:04
  • If I want to change the logging level of the root logger from INFO to DEBUG, I'm wondering how to do this so that all running processes (perhaps) on other instances will see this change. Storing the log level in an entity (or environment variable) seems like an interesting option, but I still need a means to read the entity setting periodically, which can cause overheads. Is there a way to change easily the log level in the log reader on all instances? E.g. can this be done on from the GAE Console/Playground?
    – morfys
    Jul 20, 2015 at 21:30

1 Answer 1

2

You can create a handler method and call it to dynamically set the log level at runtime.

eg:

    def logLevel(self, request):
        # ?value= NOTSET, DEBUG, INFO, WARN, ERROR, FATAL, CRITICAL
        value = request.value.upper() if request.value else None
        logging.info("Setting log level to: {}".format(value))
        logging.getLogger().setLevel(value)
        logging.debug("Test debug")
        logging.info("Test info")
        logging.warn("Test warn")
        logging.error("Test error")
        logging.critical("Test critical")

Specific loggers may also be targeted eg:

logging.getLogger('suds').setLevel(value)

(Note that in the GAE launcher 'debug' logs are not shown by default.)

2
  • Thank you, ChrisC73. Suppose the application is deployed and running in Google App Engine across multiple instances. How would I then call setLevel() or logLevel()? And would doing so affect all modules already loaded and existing processes running across all instances?
    – morfys
    Jul 21, 2015 at 17:20
  • 1
    @morfys You would probably need to call the modules directly (see here ). You will probably need to invoke this on all existing instances (see 'Instance Addressability' on the same page). Not done this myself though.
    – ChrisC73
    Jul 21, 2015 at 23:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.