47 lines
2.1 KiB
JavaScript
47 lines
2.1 KiB
JavaScript
'use strict';
|
||
|
||
/** 请求日志表:记录请求地址、请求头、请求/响应报文、耗时、解析到的用户等 */
|
||
module.exports = {
|
||
async up(queryInterface, Sequelize) {
|
||
await queryInterface.createTable(
|
||
'request_logs',
|
||
{
|
||
id: {
|
||
type: Sequelize.BIGINT.UNSIGNED,
|
||
allowNull: false,
|
||
autoIncrement: true,
|
||
primaryKey: true,
|
||
},
|
||
method: { type: Sequelize.STRING(10), allowNull: false, comment: '请求方法' },
|
||
url: { type: Sequelize.STRING(500), allowNull: false, comment: '请求地址(含 query)' },
|
||
headers: { type: Sequelize.TEXT, allowNull: true, comment: '请求头(敏感字段已脱敏)' },
|
||
request_body: { type: Sequelize.TEXT, allowNull: true, comment: '请求报文(敏感字段已脱敏)' },
|
||
response_body: { type: Sequelize.TEXT, allowNull: true, comment: '响应报文' },
|
||
status: { type: Sequelize.INTEGER, allowNull: true, comment: 'HTTP 状态码' },
|
||
user_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: true, comment: '解析到的用户ID,未登录为空' },
|
||
ip: { type: Sequelize.STRING(45), allowNull: true, comment: '请求方IP' },
|
||
duration: { type: Sequelize.INTEGER, allowNull: true, comment: '耗时(毫秒)' },
|
||
created_at: {
|
||
type: Sequelize.DATE,
|
||
allowNull: false,
|
||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
|
||
},
|
||
updated_at: {
|
||
type: Sequelize.DATE,
|
||
allowNull: false,
|
||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
|
||
},
|
||
},
|
||
{ charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '请求日志表' }
|
||
);
|
||
|
||
// 定时清理任务按创建时间删除
|
||
await queryInterface.addIndex('request_logs', { fields: [ 'created_at' ], name: 'idx_request_logs_created_at' });
|
||
await queryInterface.addIndex('request_logs', { fields: [ 'user_id' ], name: 'idx_request_logs_user_id' });
|
||
},
|
||
|
||
async down(queryInterface) {
|
||
await queryInterface.dropTable('request_logs');
|
||
},
|
||
};
|