Add some logging action

This commit is contained in:
573dev 2020-10-14 22:47:35 -05:00
parent 6ff677845d
commit a0227a42a7
4 changed files with 119 additions and 29 deletions

View File

@ -1 +1,2 @@
- Implement proper logging (is there some sort of flask logging, or should I just log to a file?) - Logging is implemented, so lets throw some logs around perhaps
- Fix the issue where the log directory probably shouldn't be a relative path

View File

@ -1,8 +1,10 @@
import logging
import os import os
from logging.config import dictConfig
from pathlib import Path from pathlib import Path
from typing import Optional, Union from typing import Optional, Union
from flask import Flask from flask import Flask, has_request_context, request
from v8_server.config import Development, Production from v8_server.config import Development, Production
from v8_server.utils.flask import generate_secret_key from v8_server.utils.flask import generate_secret_key
@ -10,6 +12,85 @@ from v8_server.utils.flask import generate_secret_key
from .version import __version__ from .version import __version__
class RequestFormatter(logging.Formatter):
def format(self, record):
encrypted = "None"
compressed = "None"
method = None
if has_request_context():
method = request.method
headers = request.headers
if "x-eamuse-info" in headers:
encrypted = headers["x-eamuse-info"]
if "x-compress" in headers:
compressed = headers["x-compress"]
record.encrypted = encrypted
record.compressed = "None" if compressed == "none" else compressed
record.method = method
return super().format(record)
# Define the logger
dictConfig(
{
"version": 1,
"formatters": {
"default": {
"format": "[ %(asctime)s | %(levelname)-8s | %(name)s ]\n%(message)s"
},
"detailed": {
"()": RequestFormatter,
"format": (
"[ %(asctime)s | %(levelname)-8s | %(method)-4s | %(encrypted)-15s "
"| %(compressed)-4s | %(name)s ]\n%(message)s"
),
},
},
"handlers": {
"wsgi": {
"class": "logging.StreamHandler",
"stream": "ext://flask.logging.wsgi_errors_stream",
"formatter": "default",
},
"debugfile": {
"class": "logging.handlers.TimedRotatingFileHandler",
"filename": "logs/debug.log",
"formatter": "default",
"when": "midnight",
},
"requestsfile": {
"class": "logging.handlers.TimedRotatingFileHandler",
"filename": "logs/requests.log",
"formatter": "detailed",
"when": "midnight",
},
"allfile": {
"class": "logging.handlers.TimedRotatingFileHandler",
"filename": "logs/all.log",
"formatter": "detailed",
"when": "midnight",
},
"werkzeugfile": {
"class": "logging.handlers.TimedRotatingFileHandler",
"filename": "logs/werkzeug.log",
"formatter": "detailed",
"when": "midnight",
},
},
"loggers": {
"": {"handlers": ["wsgi", "allfile"]},
"v8_server": {"level": "DEBUG", "handlers": ["debugfile"]},
"requests": {"level": "DEBUG", "handlers": ["requestsfile"]},
"werkzeug": {"level": "DEBUG", "handlers": ["werkzeugfile"]},
},
}
)
logging.getLogger("").setLevel(logging.INFO)
# Set the proper config values # Set the proper config values
config: Optional[Union[Production, Development]] = None config: Optional[Union[Production, Development]] = None
if os.environ.get("ENV", "dev") == "prod": if os.environ.get("ENV", "dev") == "prod":

View File

@ -1,3 +1,4 @@
import logging
from binascii import unhexlify from binascii import unhexlify
from pathlib import Path from pathlib import Path
from random import randint from random import randint
@ -14,6 +15,9 @@ from v8_server.utils.eamuse import get_timestamp
from v8_server.utils.lz77 import Lz77 from v8_server.utils.lz77 import Lz77
logger = logging.getLogger(__name__)
rlogger = logging.getLogger("requests")
EAMUSE_CONFIG = {"encrypted": False, "compressed": False} EAMUSE_CONFIG = {"encrypted": False, "compressed": False}
REQUESTS_PATH = Path("./requests") REQUESTS_PATH = Path("./requests")
@ -52,19 +56,15 @@ def eamuse_read_xml(request: Request) -> Tuple[str, str, str, str, str]:
with output_filename.open("w") as f: with output_filename.open("w") as f:
f.write(xml_text.decode("UTF-8")) f.write(xml_text.decode("UTF-8"))
model = root.attrib["model"] model = str(root.attrib["model"])
module = root[0].tag module = str(root[0].tag)
method = root[0].attrib["method"] if "method" in root[0].attrib else None method = str(root[0].attrib["method"] if "method" in root[0].attrib else None)
command = root[0].attrib["command"] if "command" in root[0].attrib else None command = str(root[0].attrib["command"] if "command" in root[0].attrib else None)
print( rlogger.debug(
"---- Read Request ----\n" "---- Request ----\n"
f"Headers: [{dict(headers)}]\n" f"[ {'Model':^20} | {'Module':^15} | {'Method':^15} | {'Command':^20} ]\n"
f" Model: [{model}]\n" f"[ {model:^20} | {module:^15} | {method:^15} | {command:^20} ]\n"
f" Module: [{module}]\n"
f" Method: [{method}]\n"
f"Command: [{command}]\n"
" Data:\n"
f"{xml_text.decode('UTF-8')[:-1]}\n" f"{xml_text.decode('UTF-8')[:-1]}\n"
) )
@ -110,11 +110,9 @@ def eamuse_prepare_xml(xml: str) -> Tuple[bytes, Dict[str, str]]:
else: else:
data = xml_bin data = xml_bin
print( rlogger.debug(
"---- Write Response ----\n" "---- Response ----\n"
" Data: [\n"
f"{ET.tostring(xml_root, pretty_print=True).decode('UTF-8')[:-1]}\n" f"{ET.tostring(xml_root, pretty_print=True).decode('UTF-8')[:-1]}\n"
f"Headers: [{headers}]\n"
) )
return data, headers return data, headers

View File

@ -15,16 +15,21 @@ def catch_all(path: str) -> str:
This is currently my catch all route, for whenever a new endpoint pops up that isn't This is currently my catch all route, for whenever a new endpoint pops up that isn't
implemented implemented
""" """
d = f""" headers = request.headers
{request.args!r}
{request.form!r} header_str = ""
{request.files!r} for header in headers:
{request.values!r} header_str += f"{header[0]:>15}: {header[1]}\n"
{request.json!r}
{request.data!r} args = "None" if len(request.args) == 0 else request.args
{request.headers!r} data = request.data
""" d = (
print(d) "*** Unknown Request ***\n"
f" Args: {args}\n"
f" Data Length: {len(data)}\n"
f"{header_str[:-1]}\n"
)
app.logger.debug(d)
return "You want path: %s" % path return "You want path: %s" % path
@ -140,6 +145,11 @@ def package() -> Tuple[bytes, Dict[str, str]]:
@app.route("/local/service", methods=["POST"]) @app.route("/local/service", methods=["POST"])
def local() -> Tuple[bytes, Dict[str, str]]: def local() -> Tuple[bytes, Dict[str, str]]:
"""
This is probably a big chunk of implementation. Handle all "local" service requests
which might have a whole bunch of stuff going on
"""
xml, model, module, method, command = eamuse_read_xml(request) xml, model, module, method, command = eamuse_read_xml(request)
if module == "shopinfo": if module == "shopinfo":
@ -155,7 +165,7 @@ def local() -> Tuple[bytes, Dict[str, str]]:
) )
elif module == "demodata": elif module == "demodata":
if method == "get": if method == "get":
response = E.response() response = E.response(E.demodata())
else: else:
response = base_response(module) response = base_response(module)