Besseres logging

This commit is contained in:
Andreas Thienemann 2022-02-26 23:17:58 +01:00
parent a7c619c057
commit 46da7600ef
3 changed files with 48 additions and 4 deletions

7
Makefile Normal file
View File

@ -0,0 +1,7 @@
BRANCH := $(shell git branch --show-current)
dev-deploy:
git push azure-dev $(BRANCH):master
prod-deploy:
git push azure-prod $(BRANCH):master

42
app.py
View File

@ -3,9 +3,30 @@
from flask import Flask, render_template, request, redirect, url_for from flask import Flask, render_template, request, redirect, url_for
from flask_bootstrap import Bootstrap from flask_bootstrap import Bootstrap
from flask_nav import Nav from flask_nav import Nav
from flask_nav.elements import Navbar, View, Subgroup, Link, Text, Separator from flask_nav.elements import Navbar, View
from sml_decoder import TasmotaSMLParser from sml_decoder import TasmotaSMLParser
import logging
import json import json
import os
app = Flask(__name__)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
# Check if running on Azure AppService
if os.getenv("ORYX_ENV_TYPE") == "AppService":
from opencensus.ext.azure.log_exporter import AzureLogHandler
from opencensus.ext.flask.flask_middleware import FlaskMiddleware
logger.addHandler(AzureLogHandler())
middleware = FlaskMiddleware(app, excludelist_paths=[])
else:
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
logger.addHandler(ch)
app = Flask(__name__) app = Flask(__name__)
Bootstrap(app) Bootstrap(app)
@ -17,6 +38,9 @@ nav.register_element("frontend_top", Navbar(View("Tasmota SML Decoder", ".index"
@app.route("/") @app.route("/")
def index(): def index():
logger.info(
f'{request.remote_addr} - - - "{request.method} {request.path} {request.scheme}"'
)
return render_template("index.html") return render_template("index.html")
@ -37,8 +61,19 @@ def decode():
messages.append({"msg": details, "tas": tasmota_script}) messages.append({"msg": details, "tas": tasmota_script})
messages = sorted(messages, key=lambda x: x["msg"]["obis"]) messages = sorted(messages, key=lambda x: x["msg"]["obis"])
print(json.dumps(messages)) logger.info(
f'{request.remote_addr} - - - "{request.method} {request.path} {request.scheme}"',
extra={
"custom_dimensions": {
"remote_addr": request.headers.get(
"X-Forwarded-For", request.remote_addr
),
"path": request.path,
"messages": json.dumps(messages),
"smldump": json.dumps(data),
}
},
)
return render_template( return render_template(
"decode.html", "decode.html",
smldump=data, smldump=data,
@ -49,4 +84,5 @@ def decode():
if __name__ == "__main__": if __name__ == "__main__":
logger.info("Startup")
app.run(debug=True) app.run(debug=True)

View File

@ -4,4 +4,5 @@ flask-nav==0.6
#smllib==1.1 #smllib==1.1
# SML Lib with grid freq and current power # SML Lib with grid freq and current power
git+https://github.com/spacemanspiff2007/SmlLib.git@3d4a106#egg=smllib git+https://github.com/spacemanspiff2007/SmlLib.git@3d4a106#egg=smllib
git+https://github.com/census-instrumentation/opencensus-python/@refs/pull/1072/merge#subdirectory=contrib/opencensus-ext-flask
opencensus-ext-azure