Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 53 additions & 17 deletions flask-app/application/logger.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,63 @@
import logging
from application.modules import utils as mtu
from flask import has_request_context, request
from application.modules.utils import get_instance_dir


def configure_log(app, logname: str = "app"):
class SafeFormatter(logging.Formatter):
"""A custom formatter to safely handle missing attributes."""

if any(isinstance(h, logging.FileHandler) for h in app.logger.handlers):
return
def format(self, record):
if "ip" not in record.__dict__:
record.ip = "N/A"
return super().format(record)

if app.debug:
level = logging.DEBUG
log_file = mtu.get_instance_dir(app, f"logs/{logname}_debug.log")
else:
level = logging.INFO
log_file = mtu.get_instance_dir(app, f"logs/{logname}.log")

app.logger.setLevel(level)
class RequestIPFilter(logging.Filter):
def __init__(self, max_len=15):
super().__init__()
self.max_len = max_len

def filter(self, record):
if has_request_context():
record.ip = request.remote_addr or "None"
else:
record.ip = "N/A"
# record.ip = record.ip.ljust(self.max_len)
return True


def configure_log(app, logname: str = "app"):

# Avoid duplicate setup
app.logger.handlers.clear()
app.logger.propagate = False

formatter = mtu.SafeFormatter("%(asctime)s - %(levelname)s: [%(ip)s] %(message)s")
is_dev = app.debug

handler = logging.FileHandler(log_file, encoding="utf-8")
handler.setLevel(level)
handler.setFormatter(formatter)
handler.addFilter(mtu.RequestIPFilter())
level = logging.DEBUG if is_dev else logging.INFO

app.logger.addHandler(handler)
log_file = get_instance_dir(
app, f"logs/{logname}_debug.log" if is_dev else f"logs/{logname}.log"
)

formatter = SafeFormatter("%(asctime)s - %(levelname)s: [%(ip)s] %(message)s")

# --- File handler (always) ---
file_handler = logging.FileHandler(log_file, encoding="utf-8")
file_handler.setLevel(level)
file_handler.setFormatter(formatter)
file_handler.addFilter(RequestIPFilter())

app.logger.addHandler(file_handler)

# --- Console handler (dev only) ---
if is_dev:
console_handler = logging.StreamHandler()
console_handler.setLevel(level)
console_handler.setFormatter(formatter)
console_handler.addFilter(RequestIPFilter())

app.logger.addHandler(console_handler)

# Set overall logger level
app.logger.setLevel(level)
19 changes: 9 additions & 10 deletions flask-app/application/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
sess,
) # limiter
from application.modules import utils as mtu
from application.logger import configure_log


def create_app(
Expand All @@ -43,19 +44,14 @@ def create_app(
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True

if debug and not app.debug:
app.debug = debug
elif os.getenv("FLASK_DEBUG", None):
if debug is True or os.getenv("FLASK_DEBUG", None) == "1":
app.debug = True

if app.debug:
print("MTW Config: ", config_path, " - port: ", port)

app.config.update(
dict(
APPLICATION_ROOT=url_prefix,
APP_NAME="MTW",
APP_VER="1.7.6",
APP_VER="1.7.7",
API_VER="1.0.0",
DBVERSION=1.0,
CACHE_DIR=mtu.get_instance_dir(app, "cache"),
Expand Down Expand Up @@ -90,6 +86,10 @@ def create_app(

app.app_context().push()

configure_log(app, "mtw_server")

app.logger.info(f"MTW config: {config_path} - port: {port}")

adminConfig = mtu.getConfig(app.config["admin_config_file"], admin=True)
if not adminConfig:
return
Expand Down Expand Up @@ -146,9 +146,8 @@ def create_app(
}
)

if app.debug:
print("Server host: ", app.config["SERVER_NAME"])
print("Worker host: ", app.config["WORKER_HOST"])
app.logger.info(f"Server host: {app.config['SERVER_NAME']}")
app.logger.info(f"Worker host: {app.config['WORKER_HOST']}")

# Flask Extensions init

Expand Down
5 changes: 2 additions & 3 deletions flask-app/application/modules/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@ def check_public(*args, **kwargs):

code, msg = validateRequest()

if app.debug:
print(code, msg)
app.logger.debug(f"[check_public] {code} - {msg}")

if code != 200:
app.logger.error(f"[check_public] {msg}")
app.logger.error(f"[check_public] {code} - {msg}")
abort(403)

return f(*args, **kwargs)
Expand Down
6 changes: 2 additions & 4 deletions flask-app/application/modules/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ def show_elapsed(begin, started=None, tag="", msg=False):
if started:
process = timer() - started

if app.debug:
print("%.3f" % elapsed, "%.3f" % process, tag)
app.logger.debug(f"{elapsed:.3f} {process:.3f} {tag}")

if msg:
return "%.3f" % elapsed + "s"
Expand Down Expand Up @@ -1489,8 +1488,7 @@ def search(dui, action):
qtp=session.get("qtp"),
)
if data:
# if app.debug:
# app.logger.error(json.dumps(data))
# app.logger.debug(json.dumps(data))
started = timer()
hits = sparql.parseSparqlData(data)
# pp.pprint(hits)
Expand Down
12 changes: 4 additions & 8 deletions flask-app/application/modules/sparql.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ def show_elapsed(begin, tag=""):

elapsed = timer() - begin

if app.debug:
print("Elapsed: %.3f" % elapsed, tag)
app.logger.debug(f"Elapsed: {elapsed:.3f} {tag}")

return elapsed

Expand Down Expand Up @@ -90,8 +89,7 @@ def getSparqlData(
qtp=qtp,
)

if app.debug:
print(sparql, "\n")
app.logger.debug(sparql)

endpoint = mtu.getSparqlEndpoint()

Expand Down Expand Up @@ -211,8 +209,7 @@ def updateSparqlBatch(
"Connection": "close",
}

# if app.debug:
# print(sparql, "\n")
# app.logger.debug(sparql)

try:
with closing(
Expand Down Expand Up @@ -272,8 +269,7 @@ def updateTriple(
"Connection": "close",
}

if app.debug:
print(sparql, "\n")
app.logger.debug(sparql)

try:
with closing(
Expand Down
26 changes: 1 addition & 25 deletions flask-app/application/modules/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import html
import io
import json
import logging
import os
import sys
import time
Expand All @@ -25,7 +24,7 @@
from requests_futures.sessions import FuturesSession
from urllib import parse as uparse

from flask import abort, has_request_context, request
from flask import abort
from flask import current_app as app

from application.modules import sparql
Expand All @@ -37,29 +36,6 @@
pp = pprint.PrettyPrinter(indent=2)


class SafeFormatter(logging.Formatter):
"""A custom formatter to safely handle missing attributes."""

def format(self, record):
if "ip" not in record.__dict__:
record.ip = "N/A"
return super().format(record)


class RequestIPFilter(logging.Filter):
def __init__(self, max_len=15):
super().__init__()
self.max_len = max_len

def filter(self, record):
if has_request_context():
record.ip = request.remote_addr or "None"
else:
record.ip = "N/A"
# record.ip = record.ip.ljust(self.max_len)
return True


def get_instance_dir(app, file_path):
if getattr(sys, "frozen", False):
datadir = os.path.normpath(
Expand Down
15 changes: 7 additions & 8 deletions flask-app/application/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from flask import Flask

from application.modules import utils as mtu
from application.logger import configure_log


def create_app(
Expand All @@ -22,14 +23,9 @@ def create_app(
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True

if debug and not app.debug:
app.debug = debug
elif os.getenv("FLASK_DEBUG", None):
if debug is True or os.getenv("FLASK_DEBUG", None) == "1":
app.debug = True

if app.debug:
print("MTW Config: ", config_path, " - port: ", port)

app.config.update(
dict(
APP_NAME="MTW Worker",
Expand All @@ -43,6 +39,10 @@ def create_app(

app.app_context().push()

configure_log(app, "mtw_worker")

app.logger.info(f"MTW Worker config: {config_path} - port: {port}")

adminConfig = mtu.getConfig(app.config["admin_config_file"], admin=True)

if not adminConfig:
Expand Down Expand Up @@ -72,8 +72,7 @@ def create_app(
app.config.update({"APP_HOST": app.config.get("SERVER_NAME")})
app.config.update({"SERVER_NAME": None})

if app.debug:
print("Worker host: ", app.config["WORKER_HOST"])
app.logger.info(f"Worker host: {app.config['WORKER_HOST']}")

if relax:
app.config.update({"APP_RELAXED": True})
Expand Down
5 changes: 1 addition & 4 deletions flask-app/mtw-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from waitress import serve

from application.main import create_app
from application.logger import configure_log

DEFAULT_HOST = "127.0.0.1"
DEFAULT_PORT = 55930
Expand All @@ -15,7 +14,7 @@
DEFAULT_PREFIX = "mtw"

appname = "mtw-server"
appdesc = "MTW Server 1.7.5"
appdesc = "MTW Server 1.7.7"
appusage = "Help: " + appname + " -h \n"
appauthor = "Filip Kriz"

Expand Down Expand Up @@ -92,8 +91,6 @@ def start():
)
return

configure_log(app, "mtw_server")

if getattr(sys, "frozen", False):
app.static_folder = os.path.join(os.path.dirname(sys.executable), "static")
app.template_folder = os.path.join(os.path.dirname(sys.executable), "templates")
Expand Down
5 changes: 1 addition & 4 deletions flask-app/mtw-worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
from waitress import serve

from application.worker import create_app
from application.logger import configure_log

DEFAULT_HOST = "127.0.0.1"
DEFAULT_PORT = 55933
DEFAULT_THREADS = 4
DEFAULT_CONFIG = "conf/mtw-dist.ini"

appname = "mtw-worker"
appdesc = "MTW Worker 0.1.8"
appdesc = "MTW Worker 0.1.10"
appusage = "Help: " + appname + " -h \n"
appauthor = "Filip Kriz"

Expand Down Expand Up @@ -80,8 +79,6 @@ def start():
)
return

configure_log(app, "mtw_worker")

if getattr(sys, "frozen", False):
app.static_folder = os.path.join(os.path.dirname(sys.executable), "static")
app.template_folder = os.path.join(os.path.dirname(sys.executable), "templates")
Expand Down
14 changes: 7 additions & 7 deletions flask-app/mtw_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
### Version 1.7.6 ###
### Version 1.7.7 ###
### Python 3.12 ###
arrow==1.4.0
bcrypt==5.0.0 ## NOT python-bcrypt !!!
cachelib==0.13.0
certifi==2026.1.4
certifi==2026.2.25
colorama==0.4.6
diff_match_patch==20241021
Flask==3.1.3
Expand All @@ -18,17 +18,17 @@ Jinja2==3.1.6
MarkupSafe==3.0.3
pip_audit==2.10.0 ## Run: pip freeze > requirements.txt ; pip-audit ; pip-audit -r mtw_requirements.txt
pyuca==1.2
rdflib==7.5.0
requests==2.33.0
rdflib==7.6.0
requests==2.33.1
requests_futures==1.0.2
SPARQLWrapper==2.0.0
tqdm==4.67.3
urllib3==2.6.3
waitress==3.0.2
Werkzeug==3.1.6
Werkzeug==3.1.8
### (optional) If building for Windows:
pyinstaller==6.18.0; sys_platform == "win32"
pyinstaller-hooks-contrib==2026.0; sys_platform == "win32"
pyinstaller==6.19.0; sys_platform == "win32"
pyinstaller-hooks-contrib==2026.4; sys_platform == "win32"
## Install pywin32
pywin32==311; sys_platform == "win32"
## and run in CMD as Admin with activated VENV (!):
Expand Down
Loading