添加日志到数据库

lzh
Kenneth 3 weeks ago
parent cd209ff913
commit 16f89ac361

@ -444,3 +444,69 @@
2024-10-28 22:53:07,402 - INFO - * Restarting with watchdog (windowsapi)
2024-10-28 22:53:09,649 - WARNING - * Debugger is active!
2024-10-28 22:53:09,663 - INFO - * Debugger PIN: 266-144-430
2024-10-30 08:33:33,262 - INFO - WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:8000
* Running on http://10.128.54.153:8000
2024-10-30 08:33:33,264 - INFO - Press CTRL+C to quit
2024-10-30 08:33:33,280 - INFO - * Restarting with watchdog (windowsapi)
2024-10-30 08:33:34,080 - WARNING - * Debugger is active!
2024-10-30 08:33:34,084 - INFO - * Debugger PIN: 266-144-430
2024-10-30 08:33:39,633 - INFO - * Detected change in 'C:\\Users\\Lenovo\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python312\\site-packages\\MySQLdb\\cursors.py', reloading
2024-10-30 08:33:39,633 - INFO - * Detected change in 'C:\\Users\\Lenovo\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python312\\site-packages\\MySQLdb\\connections.py', reloading
2024-10-30 08:33:39,647 - INFO - 127.0.0.1 - - [30/Oct/2024 08:33:39] "GET /login HTTP/1.1" 500 -
2024-10-30 08:33:39,947 - INFO - 127.0.0.1 - - [30/Oct/2024 08:33:39] "GET /login?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
2024-10-30 08:33:39,953 - INFO - 127.0.0.1 - - [30/Oct/2024 08:33:39] "GET /login?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
2024-10-30 08:33:40,217 - INFO - 127.0.0.1 - - [30/Oct/2024 08:33:40] "GET /login?__debugger__=yes&cmd=resource&f=console.png&s=g5Bt9T56K8N0Wkux9k09 HTTP/1.1" 200 -
2024-10-30 08:33:40,381 - INFO - * Restarting with watchdog (windowsapi)
2024-10-30 08:33:41,203 - WARNING - * Debugger is active!
2024-10-30 08:33:41,207 - INFO - * Debugger PIN: 266-144-430
2024-10-30 08:33:41,365 - INFO - 127.0.0.1 - - [30/Oct/2024 08:33:41] "GET /login?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
2024-10-30 08:36:50,450 - INFO - WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:8000
* Running on http://10.128.54.153:8000
2024-10-30 08:36:50,450 - INFO - Press CTRL+C to quit
2024-10-30 08:36:50,461 - INFO - * Restarting with watchdog (windowsapi)
2024-10-30 08:36:51,232 - WARNING - * Debugger is active!
2024-10-30 08:36:51,237 - INFO - * Debugger PIN: 266-144-430
2024-10-30 08:36:54,580 - INFO - * Detected change in 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.12_3.12.2032.0_x64__qbz5n2kfra8p0\\Lib\\_strptime.py', reloading
2024-10-30 08:36:54,580 - INFO - Handling login request
2024-10-30 08:36:54,642 - INFO - 127.0.0.1 - - [30/Oct/2024 08:36:54] "GET /login HTTP/1.1" 200 -
2024-10-30 08:36:54,823 - INFO - 127.0.0.1 - - [30/Oct/2024 08:36:54] "GET /favicon.ico HTTP/1.1" 404 -
2024-10-30 08:36:55,464 - INFO - * Restarting with watchdog (windowsapi)
2024-10-30 08:36:56,171 - WARNING - * Debugger is active!
2024-10-30 08:36:56,175 - INFO - * Debugger PIN: 266-144-430
2024-10-30 08:36:58,211 - INFO - Handling login request
2024-10-30 08:36:58,280 - INFO - User admin logged in with role Ö¸»ÓÕß
2024-10-30 08:36:58,280 - INFO - 127.0.0.1 - - [30/Oct/2024 08:36:58] "POST /login HTTP/1.1" 302 -
2024-10-30 08:36:58,299 - INFO - Accessing commander page
2024-10-30 08:36:58,309 - INFO - * Detected change in 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.12_3.12.2032.0_x64__qbz5n2kfra8p0\\Lib\\encodings\\unicode_escape.py', reloading
2024-10-30 08:36:58,309 - INFO - 127.0.0.1 - - [30/Oct/2024 08:36:58] "GET /commander HTTP/1.1" 200 -
2024-10-30 08:36:58,349 - INFO - Accessing uploaded file: 145922dab6954fd8968c426200198b17_sb.jpg
2024-10-30 08:36:58,356 - INFO - Accessing uploaded file: 1eaf03ab0fdf409289d26eb2e282955c__20241022150445.png
2024-10-30 08:36:58,389 - INFO - Accessing uploaded file: 2d7d87c11e2f45c49a8e4ff7a699661a__20241015171033.png
2024-10-30 08:36:58,523 - INFO - 127.0.0.1 - - [30/Oct/2024 08:36:58] "GET /uploads/145922dab6954fd8968c426200198b17_sb.jpg HTTP/1.1" 304 -
2024-10-30 08:36:58,558 - INFO - 127.0.0.1 - - [30/Oct/2024 08:36:58] "GET /uploads/1eaf03ab0fdf409289d26eb2e282955c__20241022150445.png HTTP/1.1" 304 -
2024-10-30 08:36:58,617 - INFO - 127.0.0.1 - - [30/Oct/2024 08:36:58] "GET /uploads/2d7d87c11e2f45c49a8e4ff7a699661a__20241015171033.png HTTP/1.1" 304 -
2024-10-30 08:36:59,341 - INFO - * Restarting with watchdog (windowsapi)
2024-10-30 08:37:00,125 - WARNING - * Debugger is active!
2024-10-30 08:37:00,130 - INFO - * Debugger PIN: 266-144-430
2024-10-30 08:37:03,492 - INFO - Handling login request
2024-10-30 08:37:03,554 - INFO - User scout logged in with role Õì²éÕß
2024-10-30 08:37:03,557 - INFO - 127.0.0.1 - - [30/Oct/2024 08:37:03] "POST /login HTTP/1.1" 302 -
2024-10-30 08:37:03,571 - INFO - Accessing scout page
2024-10-30 08:37:03,577 - INFO - 127.0.0.1 - - [30/Oct/2024 08:37:03] "GET /scout HTTP/1.1" 200 -
2024-10-30 08:37:04,860 - INFO - Handling send message request
2024-10-30 08:37:04,864 - INFO - 127.0.0.1 - - [30/Oct/2024 08:37:04] "GET /send_message HTTP/1.1" 200 -
2024-10-30 08:37:15,534 - INFO - Accessing commander page
2024-10-30 08:37:15,536 - WARNING - Unauthorized access to commander page
2024-10-30 08:37:15,538 - INFO - 127.0.0.1 - - [30/Oct/2024 08:37:15] "GET /commander HTTP/1.1" 302 -
2024-10-30 08:37:15,547 - INFO - Handling login request
2024-10-30 08:37:15,555 - INFO - 127.0.0.1 - - [30/Oct/2024 08:37:15] "GET /login HTTP/1.1" 200 -
2024-10-30 08:37:28,544 - INFO - * Detected change in 'e:\\_Ufo\\0000jiegou\\TheBattleCar\\app.py', reloading
2024-10-30 08:37:28,544 - INFO - * Detected change in 'e:\\_Ufo\\0000jiegou\\TheBattleCar\\app.py', reloading
2024-10-30 08:37:28,544 - INFO - * Detected change in 'e:\\_Ufo\\0000jiegou\\TheBattleCar\\app.py', reloading
2024-10-30 08:37:28,903 - INFO - * Restarting with watchdog (windowsapi)
2024-10-30 08:37:29,662 - WARNING - * Debugger is active!
2024-10-30 08:37:29,667 - INFO - * Debugger PIN: 266-144-430

@ -1,6 +1,7 @@
import os
import uuid
import logging
from datetime import datetime
from flask import Flask, current_app, session, redirect, url_for, request, flash, render_template, jsonify, send_from_directory
from flask_sqlalchemy import SQLAlchemy
from flask_mysqldb import MySQL
@ -26,13 +27,33 @@ app.config['MYSQL_DB'] = 'mydatabase'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
mysql = MySQL(app)
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
# 配置日志记录器
logging.basicConfig(filename='app.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# 自定义日志处理器类
class DatabaseLogHandler(logging.Handler):
def emit(self, record):
log_entry = self.format(record)
# 去掉时间戳中的毫秒部分
timestamp = datetime.strptime(record.asctime, '%Y-%m-%d %H:%M:%S,%f').strftime('%Y-%m-%d %H:%M:%S')
cursor = mysql.connection.cursor()
cursor.execute(
"INSERT INTO logs (timestamp, level, message) VALUES (%s, %s, %s)",
(timestamp, record.levelname, log_entry)
)
mysql.connection.commit()
cursor.close()
# 添加自定义日志处理器
db_handler = DatabaseLogHandler()
db_handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
db_handler.setFormatter(formatter)
logger.addHandler(db_handler)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@ -64,7 +85,6 @@ def index():
current_app.logger.info("Index route accessed")
return "Welcome to the Home Page!"
@app.route('/view_logs')
@login_required
def view_logs():

Loading…
Cancel
Save