openfaas / python-flask-template

HTTP and Flask-based OpenFaaS templates for Python 3
MIT License
85 stars 86 forks source link

logging #35

Closed yvz5 closed 4 years ago

yvz5 commented 4 years ago

Hi,

I tried passing app.logger to the function by adding

class Context:
    def __init__(self):
        self.hostname = os.getenv('HOSTNAME', 'localhost')
        self.logger = app.logger

and calling it inside the function I get no error but the function does not actually get called, so there is a silent exception somewhere.

How do you log ? print does not show up even if I have write_debug env variable set to true

junneyang commented 3 years ago

@yvz5 how to print message, have you found the solution? thanks

yvz5 commented 3 years ago

@yvz5 how to print message, have you found the solution? thanks

Nope. I am not using openfaas anymore. Moved to azure functions.

fiveddd commented 3 years ago

Guys, I had same issue here, here is my solution:

  1. First, add log config for flask app,edit tempalte/python3-flask/index.py
    
    import sys
    from logging.config import dictConfig

dictConfig({ 'version': 1, 'formatters': {'default': { 'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s', }}, 'handlers': {'wsgi': { 'class': 'logging.StreamHandler', 'stream': 'ext://sys.stdout', 'formatter': 'default' }}, 'root': { 'level': 'INFO', 'handlers': ['wsgi'] } })


1. Then, edit your function py files, import current_app, write your logging statement `current_app.logger.info(xxx)`
```py

from flask import current_app as app
app.logger.info(config)

Hope this solution helps