Besseres logging
This commit is contained in:
parent
a7c619c057
commit
46da7600ef
7
Makefile
Normal file
7
Makefile
Normal 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
42
app.py
@ -3,9 +3,30 @@
|
||||
from flask import Flask, render_template, request, redirect, url_for
|
||||
from flask_bootstrap import Bootstrap
|
||||
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
|
||||
import logging
|
||||
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__)
|
||||
Bootstrap(app)
|
||||
@ -17,6 +38,9 @@ nav.register_element("frontend_top", Navbar(View("Tasmota SML Decoder", ".index"
|
||||
|
||||
@app.route("/")
|
||||
def index():
|
||||
logger.info(
|
||||
f'{request.remote_addr} - - - "{request.method} {request.path} {request.scheme}"'
|
||||
)
|
||||
return render_template("index.html")
|
||||
|
||||
|
||||
@ -37,8 +61,19 @@ def decode():
|
||||
messages.append({"msg": details, "tas": tasmota_script})
|
||||
|
||||
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(
|
||||
"decode.html",
|
||||
smldump=data,
|
||||
@ -49,4 +84,5 @@ def decode():
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
logger.info("Startup")
|
||||
app.run(debug=True)
|
||||
|
||||
@ -4,4 +4,5 @@ flask-nav==0.6
|
||||
#smllib==1.1
|
||||
# SML Lib with grid freq and current power
|
||||
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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user