Skip to content

Commit

Permalink
1. info: add logger
Browse files Browse the repository at this point in the history
  • Loading branch information
bboysoulcn committed Jul 10, 2024
1 parent ab2e664 commit 276a9e2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
10 changes: 10 additions & 0 deletions logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from loguru import logger
import sys

logger.configure(
handlers=[{
"sink": sys.stdout,
"serialize": False,
"format": "{time} - {level} - {message} "
}]
)
9 changes: 4 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)


Expand All @@ -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)
Expand Down
45 changes: 44 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 276a9e2

Please sign in to comment.