use logging library

This commit is contained in:
Jeffrey C. Ollie 2023-09-01 13:30:54 -05:00
parent b0628543f8
commit 8cc696c43d
No known key found for this signature in database
GPG key ID: F936E4DCB7E25F15
4 changed files with 95 additions and 2 deletions

17
poetry.lock generated
View file

@ -1,5 +1,20 @@
# This file is automatically @generated by Poetry 1.4.2 and should not be changed by hand. # This file is automatically @generated by Poetry 1.4.2 and should not be changed by hand.
[[package]]
name = "arrow"
version = "1.2.3"
description = "Better dates & times for Python"
category = "main"
optional = false
python-versions = ">=3.6"
files = [
{file = "arrow-1.2.3-py3-none-any.whl", hash = "sha256:5a49ab92e3b7b71d96cd6bfcc4df14efefc9dfa96ea19045815914a6ab6b1fe2"},
{file = "arrow-1.2.3.tar.gz", hash = "sha256:3934b30ca1b9f292376d9db15b19446088d12ec58629bc3f0da28fd55fb633a1"},
]
[package.dependencies]
python-dateutil = ">=2.7.0"
[[package]] [[package]]
name = "black" name = "black"
version = "23.7.0" version = "23.7.0"
@ -785,4 +800,4 @@ test = ["pytest", "pytest-grpc"]
[metadata] [metadata]
lock-version = "2.0" lock-version = "2.0"
python-versions = "^3.11" python-versions = "^3.11"
content-hash = "9756b224be33434f1cf2d6702163ed6e6ffb37f40d608ea6be5e0a832d5e8bfe" content-hash = "9018e6f85ff65e2974f8b58e32d6ba6d30f0ba362b6f65f76ade31cd1e83378d"

View file

@ -18,6 +18,7 @@ influxdb-client = "^1.37.0"
pypng = "^0.20220715.0" pypng = "^0.20220715.0"
typing-extensions = "^4.7.1" typing-extensions = "^4.7.1"
prometheus-client = "^0.17.1" prometheus-client = "^0.17.1"
arrow = "^1.2.3"
[tool.poetry.group.dev.dependencies] [tool.poetry.group.dev.dependencies]
black = "^23.7.0" black = "^23.7.0"

View file

@ -20,6 +20,9 @@ from prometheus_client import Info
from prometheus_client import MetricsHandler from prometheus_client import MetricsHandler
import starlink_grpc_tools.dish_common as dish_common import starlink_grpc_tools.dish_common as dish_common
from starlink_grpc_tools.logging import setup_logging
logger = logging.getLogger(__name__)
class Terminated(Exception): class Terminated(Exception):
@ -316,6 +319,7 @@ class GatherMetrics(threading.Thread):
def main(): def main():
setup_logging()
opts = parse_args() opts = parse_args()
logging.basicConfig(format="%(levelname)s: %(message)s", stream=sys.stderr) logging.basicConfig(format="%(levelname)s: %(message)s", stream=sys.stderr)
@ -331,7 +335,7 @@ def main():
signal.signal(signal.SIGTERM, handle_sigterm) signal.signal(signal.SIGTERM, handle_sigterm)
print("HTTP listening on port", opts.port) logger.info(f"HTTP listening on port {opts.port}")
try: try:
httpd.serve_forever() httpd.serve_forever()
except (KeyboardInterrupt, Terminated): except (KeyboardInterrupt, Terminated):

View file

@ -0,0 +1,73 @@
"""Configure logging."""
import logging
import pathlib
import sys
import traceback
import arrow
import urllib3
class Formatter(logging.Formatter):
def __init__(self):
# fmt = formatter_message(fmt, use_color)
logging.Formatter.__init__(self, "")
def format(self, record: logging.LogRecord):
message = record.msg
if len(record.args) > 0:
message = record.msg % record.args
created = arrow.get(record.created).to("America/Chicago")
name = record.name
if len(name) > 25:
name = name[:24] + "\u2026"
pathname = record.pathname
if len(pathname) > 15:
pathname = "\u2026" + pathname[-14:]
prefix = f"{created:YYYY-MM-DD HH:mm:ss.SSSSSS Z} [{name:^25s}] {pathname}:{record.lineno:<4d} {record.levelname:8s}" # noqa: E501
msg = f"{prefix} {message:s}"
if record.exc_info is not None:
formatted = traceback.format_exception(*record.exc_info)
for chunk in formatted:
line = chunk.splitlines()
for line in chunk.splitlines():
msg += f"\n{prefix} {line}"
return msg
def setup_logging(filename: pathlib.Path | None = None):
"""Set up logging."""
formatter = Formatter()
console = logging.StreamHandler(stream=sys.stderr)
console.setFormatter(formatter)
console.setLevel(logging.DEBUG)
if filename is not None:
file = logging.FileHandler(filename=filename, encoding="utf-8")
file.setFormatter(formatter)
file.setLevel(logging.DEBUG)
root = logging.getLogger()
for handler in root.handlers:
root.removeHandler(handler)
handler.close()
root.addHandler(console)
if filename is not None:
root.addHandler(file)
root.setLevel(logging.DEBUG)
urllib3.disable_warnings()
logging.getLogger("urllib3.connectionpool").setLevel(logging.INFO)
logging.captureWarnings(True)