diff --git a/logger.py b/logger.py new file mode 100644 index 0000000..00afbbd --- /dev/null +++ b/logger.py @@ -0,0 +1,10 @@ +from loguru import logger +import sys + +logger.configure( + handlers=[{ + "sink": sys.stdout, + "serialize": False, + "format": "{time} - {level} - {message} " + }] +) \ No newline at end of file diff --git a/main.py b/main.py index 0215674..ba1d5a9 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,7 @@ from channel.feishu import FeishuChannel from apscheduler.schedulers.background import BackgroundScheduler import time +from logger import logger # Define a class to represent a slow log entry @@ -36,11 +37,10 @@ def fetch_slow_log(): cursor.execute(sql, (time_range, query_time)) results = cursor.fetchall() for row in results: - print(row) log_entry = SlowLog(*row) slow_logs.append(log_entry) except Exception as e: - print(f"Error fetching slow logs: {e}") + logger.error(f"Error fetching slow logs: {e}") finally: connection.close() return slow_logs @@ -49,11 +49,10 @@ def fetch_slow_log(): def slow_job(): slow_logs = fetch_slow_log() if len(slow_logs) == 0: - print("No slow logs found") + logger.info("No slow logs found") else: channel = FeishuChannel() for slow_log in slow_logs: - print(slow_log.lock_time) channel.send_msg(slow_log) @@ -63,7 +62,7 @@ def slow_job(): scheduler = BackgroundScheduler() scheduler.add_job(slow_job, 'interval', seconds=int(time_range)) scheduler.start() - print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) + logger.info('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) try: while True: time.sleep(2) diff --git a/poetry.lock b/poetry.lock index 5036c4b..90ddc7f 100644 --- a/poetry.lock +++ b/poetry.lock @@ -123,6 +123,17 @@ files = [ [package.dependencies] pycparser = "*" +[[package]] +name = "colorama" +version = "0.4.6" +description = "Cross-platform colored terminal text." +optional = false +python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7" +files = [ + {file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"}, + {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, +] + [[package]] name = "cryptography" version = "42.0.8" @@ -244,6 +255,24 @@ files = [ {file = "idna-3.7.tar.gz", hash = "sha256:028ff3aadf0609c1fd278d8ea3089299412a7a8b9bd005dd08b9f8285bcb5cfc"}, ] +[[package]] +name = "loguru" +version = "0.7.2" +description = "Python logging made (stupidly) simple" +optional = false +python-versions = ">=3.5" +files = [ + {file = "loguru-0.7.2-py3-none-any.whl", hash = "sha256:003d71e3d3ed35f0f8984898359d65b79e5b21943f78af86aa5491210429b8eb"}, + {file = "loguru-0.7.2.tar.gz", hash = "sha256:e671a53522515f34fd406340ee968cb9ecafbc4b36c679da03c18fd8d0bd51ac"}, +] + +[package.dependencies] +colorama = {version = ">=0.3.4", markers = "sys_platform == \"win32\""} +win32-setctime = {version = ">=1.0.0", markers = "sys_platform == \"win32\""} + +[package.extras] +dev = ["Sphinx (==7.2.5)", "colorama (==0.4.5)", "colorama (==0.4.6)", "exceptiongroup (==1.1.3)", "freezegun (==1.1.0)", "freezegun (==1.2.2)", "mypy (==v0.910)", "mypy (==v0.971)", "mypy (==v1.4.1)", "mypy (==v1.5.1)", "pre-commit (==3.4.0)", "pytest (==6.1.2)", "pytest (==7.4.0)", "pytest-cov (==2.12.1)", "pytest-cov (==4.1.0)", "pytest-mypy-plugins (==1.9.3)", "pytest-mypy-plugins (==3.0.0)", "sphinx-autobuild (==2021.3.14)", "sphinx-rtd-theme (==1.3.0)", "tox (==3.27.1)", "tox (==4.11.0)"] + [[package]] name = "pycparser" version = "2.22" @@ -331,7 +360,21 @@ tzdata = {version = "*", markers = "platform_system == \"Windows\""} [package.extras] devenv = ["check-manifest", "pytest (>=4.3)", "pytest-cov", "pytest-mock (>=3.3)", "zest.releaser"] +[[package]] +name = "win32-setctime" +version = "1.1.0" +description = "A small Python utility to set file creation time on Windows" +optional = false +python-versions = ">=3.5" +files = [ + {file = "win32_setctime-1.1.0-py3-none-any.whl", hash = "sha256:231db239e959c2fe7eb1d7dc129f11172354f98361c4fa2d6d2d7e278baa8aad"}, + {file = "win32_setctime-1.1.0.tar.gz", hash = "sha256:15cf5750465118d6929ae4de4eb46e8edae9a5634350c01ba582df868e932cb2"}, +] + +[package.extras] +dev = ["black (>=19.3b0)", "pytest (>=4.6.2)"] + [metadata] lock-version = "2.0" python-versions = "^3.12" -content-hash = "69b40462b87e4a619c06ef44f896c6e1c49b1c34789e34328326d59075c25ac9" +content-hash = "b0b6061916587db72a87da426e1d96875f0a2cf6ae15e9f02cf8e9b7f4c3a4e6" diff --git a/pyproject.toml b/pyproject.toml index 46d0d45..93b8258 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,6 +11,7 @@ httpx = "^0.27.0" pymysql = "^1.1.1" cryptography = "^42.0.8" apscheduler = "^3.10.4" +loguru = "^0.7.2" [build-system]