0

How to enable log for microservice developped in Python?

I can run hello-microservice without a glitch. However, my own microservice looks not starting up after I uploaded the zip file. I tried to wait for hours, and the same. I have run the docker locally without issue.

A call to any REST end point of the microservice returns,

{"error":"Microservice/Bad gateway","message":"Microservice not available Connection refused : Connection refused: a2c-microservice-scope-xxx-prod.svc.cluster.local/80","info":"https://www.cumulocity.com/guides/reference-guide/#error_reporting"} 

I assume for some reason, the microservice I uploaded didn't start up properly. How can I enable any log to find out where goes wrong?

Bill Zhou
  • 25
  • 3
  • Turns out to be version of my instance is not new enough. Upgrade to new instance, now I can see the Logs button. @TyrManuZ was right, with the new version, Logs just show up by itself. – Bill Zhou Sep 04 '19 at 06:40

1 Answers1

0

You just need to logg to stdout and then you can access the logs through the user interface in Cumulocity IoT. It is available through the application UI in administration

LogViewer Cumulocity IoT

Here a python example how to configure the logging:

LOG_FORMATTER = logging.Formatter("%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s]  %(message)s")

LOGGER = logging.getLogger()
LOGGER.setLevel(logging.DEBUG)
CONSOLE_HANDLER = logging.StreamHandler(sys.stdout)
CONSOLE_HANDLER.setFormatter(LOG_FORMATTER)
LOGGER.addHandler(CONSOLE_HANDLER)
TyrManuZ
  • 2,039
  • 1
  • 14
  • 23
  • Good to know no special set up needed. I should have included that I did have logging and print both to console enabled, but no luck seeing the "Logs" button. I cleared cache of browser and no difference. My Cumolocity Version: 9.20.10 (backend), 9.20.10 (UI) wonder if same as yours. – Bill Zhou Aug 30 '19 at 00:53
  • Not sure but this logs not work always. Until today, I haven't understood them. Sometimes we saw them sometimes we didn't. What I have done for testing proposes is to send the logs, I want to get, as a response to the request. So, we are pretty sure what he microservice is doing. Hope this helps. – Jorge Aug 30 '19 at 15:21
  • @Jorge Good information following which I enabled SMTPHandler to send out log. However, I did't receive any email so I assume the app doesn't run at all. As the docker image can run locally, I guess the issue is related to cumulocity.json. I played with postfix as "version": "1.0.0-SNAPSHOT", and still no luck. – Bill Zhou Sep 04 '19 at 00:07
  • @BillZhou Remember when you make changes on the microservices you need to change the version on the cumulocity.json. If that doesn't work try to delete the microservice and created again! I haven't tried the SMTPHandler before so not sure how it works and can't help you with that, sorry. – Jorge Sep 04 '19 at 21:42
  • @Jorge All good now after I migrate to a new version of the back-end instance. I also implement request and response to get log as you have advised which does look more straightforward. Cheers. – Bill Zhou Sep 09 '19 at 00:36