Skip to content

Commit

Permalink
feat: set log level with env variable (#24)
Browse files Browse the repository at this point in the history
* feat: set log level with env variable
  • Loading branch information
inspirit941 authored Feb 9, 2024
1 parent fd0503b commit 86b84d8
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions parliament/server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import sys
import traceback
import logging

from flask import Flask, request
from cloudevents.http import CloudEvent, from_http, to_binary
Expand All @@ -22,6 +23,7 @@ def create(func):
Create a Flask app with kube health endpoints, exposing 'func' at /
"""
app = Flask(__name__)
set_log_level(app, os.environ.get("LOG_LEVEL", "WARNING"))

@app.route("/", methods=["POST"])
def handle_post():
Expand Down Expand Up @@ -62,3 +64,22 @@ def invoke(func, context):
traceback.print_exc()
print("caught", err)
return f"Function raised {err}", 500


def set_log_level(app, level):
if level == "CRITICAL":
app.logger.setLevel(logging.CRITICAL)
elif level == "DEBUG":
app.logger.setLevel(logging.DEBUG)
elif level == "INFO":
app.logger.setLevel(logging.INFO)
elif level == "ERROR":
app.logger.setLevel(logging.ERROR)
elif level == "NOTSET":
app.logger.setLevel(logging.NOTSET)
elif level == "WARNING":
app.logger.setLevel(logging.WARNING)
elif level == "DISABLED":
app.logger.disabled = True
else: # default
app.logger.setLevel(logging.WARNING)

0 comments on commit 86b84d8

Please sign in to comment.