C3.logger for python code

Is there an equivalent of C3.logger or C3.log for python environment code?

2 Likes

Yes, you can use native python logging library. (https://docs.python.org/3.6/library/logging.html)

import logging
logger = logging.getLogger('name_of_your_logger')
logger.setLevel(logging.INFO)

followed later in your code by statements like

logger.info("Just did something awesome")

Similar to any language, in case you log the content of an object, be mindful that:

  1. The logger can raise an Exception that will break the execution of your code
  2. If the thing you want to log requires heavy processing (a large object to serialize, or whatever), you should protect your logger with a check on its level
if logger.isEnabledFor('DEBUG'): 
    logger.debug("A big object: {}".format(myObj.toJson()))

I think this is only part of the solution, how can I see the log after a python method execution?
Is it available on Splunk? If so I’m not able to find it.

yes, for example you can find the output for the logger defined above using: logger=c.e.a.PythonActionEngine name_of_your_logger

@Nicola can you provide the logger you have defined ? I’m afraid your user do not have permission to see some logs.
Get in touch via mail so I can check if I see your logs - it this is the case then you have to add some in your log string like below

log.info("t_tenant="+ c3Context().tenant + " t_tag="+ c3Context().tag + "dhkahdahdakdhksjdh" )

Hi Marco, I’ll try adding that string.
I realized that I’m having an error when importing the logging library in Python that breaks the execution.
I’m importing at the beginning of the .py file, outside of the methods implementation:

import logging
logger = logging.getLogger('LogUseCaseClc_v1')
logger.setLever(logging.INFO) 

and then inside a method:

logger.info('Test Message 1' )

Hi Nicola,

The error comes from
logger.setLever
which should in fact be
logger.setLevel

Whenever you encounter such error message No output from python for action, it probably means that there is a typo in the python code.

Thanks