mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 21:13:50 +00:00
4.8.6 fix (#1963)
* feat: log store * fix: full text search match query * perf: mongo schema import, Avoid duplicate import
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { SystemConfigsType } from '@fastgpt/global/common/system/config/type';
|
||||
import { connectionMongo, type Model } from '../../../common/mongo';
|
||||
import { connectionMongo, getMongoModel, type Model } from '../../../common/mongo';
|
||||
import { SystemConfigsTypeMap } from '@fastgpt/global/common/system/config/constants';
|
||||
|
||||
const { Schema, model, models } = connectionMongo;
|
||||
@@ -27,6 +27,7 @@ try {
|
||||
console.log(error);
|
||||
}
|
||||
|
||||
export const MongoSystemConfigs: Model<SystemConfigsType> =
|
||||
models[collectionName] || model(collectionName, systemConfigSchema);
|
||||
MongoSystemConfigs.syncIndexes();
|
||||
export const MongoSystemConfigs = getMongoModel<SystemConfigsType>(
|
||||
collectionName,
|
||||
systemConfigSchema
|
||||
);
|
||||
|
@@ -1,13 +1,9 @@
|
||||
import dayjs from 'dayjs';
|
||||
import chalk from 'chalk';
|
||||
import { isProduction } from './constants';
|
||||
import { LogLevelEnum } from './log/constant';
|
||||
// import { MongoLog } from './log/schema';
|
||||
import connectionMongo from '../mongo/index';
|
||||
|
||||
enum LogLevelEnum {
|
||||
debug = 0,
|
||||
info = 1,
|
||||
warn = 2,
|
||||
error = 3
|
||||
}
|
||||
const logMap = {
|
||||
[LogLevelEnum.debug]: {
|
||||
levelLog: chalk.green('[Debug]')
|
||||
@@ -23,23 +19,26 @@ const logMap = {
|
||||
}
|
||||
};
|
||||
const envLogLevelMap: Record<string, number> = {
|
||||
debug: 0,
|
||||
info: 1,
|
||||
warn: 2,
|
||||
error: 3
|
||||
debug: LogLevelEnum.debug,
|
||||
info: LogLevelEnum.info,
|
||||
warn: LogLevelEnum.warn,
|
||||
error: LogLevelEnum.error
|
||||
};
|
||||
|
||||
const logLevel = (() => {
|
||||
if (!isProduction) return LogLevelEnum.debug;
|
||||
const envLogLevel = (process.env.LOG_LEVEL || 'info').toLocaleLowerCase();
|
||||
if (!envLogLevel || envLogLevelMap[envLogLevel] === undefined) return LogLevelEnum.info;
|
||||
return envLogLevelMap[envLogLevel];
|
||||
const { LOG_LEVEL, STORE_LOG_LEVEL } = (() => {
|
||||
const LOG_LEVEL = (process.env.LOG_LEVEL || 'info').toLocaleLowerCase();
|
||||
const STORE_LOG_LEVEL = (process.env.STORE_LOG_LEVEL || '').toLocaleLowerCase();
|
||||
|
||||
return {
|
||||
LOG_LEVEL: envLogLevelMap[LOG_LEVEL] || LogLevelEnum.info,
|
||||
STORE_LOG_LEVEL: envLogLevelMap[STORE_LOG_LEVEL] ?? 99
|
||||
};
|
||||
})();
|
||||
|
||||
/* add logger */
|
||||
export const addLog = {
|
||||
log(level: LogLevelEnum, msg: string, obj: Record<string, any> = {}) {
|
||||
if (level < logLevel) return;
|
||||
if (level < LOG_LEVEL) return;
|
||||
|
||||
const stringifyObj = JSON.stringify(obj);
|
||||
const isEmpty = Object.keys(obj).length === 0;
|
||||
@@ -52,35 +51,15 @@ export const addLog = {
|
||||
|
||||
level === LogLevelEnum.error && console.error(obj);
|
||||
|
||||
const lokiUrl = process.env.LOKI_LOG_URL as string;
|
||||
if (!lokiUrl) return;
|
||||
|
||||
try {
|
||||
fetch(lokiUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
streams: [
|
||||
{
|
||||
stream: {
|
||||
level
|
||||
},
|
||||
values: [
|
||||
[
|
||||
`${Date.now() * 1000000}`,
|
||||
JSON.stringify({
|
||||
message: msg,
|
||||
...obj
|
||||
})
|
||||
]
|
||||
]
|
||||
}
|
||||
]
|
||||
})
|
||||
});
|
||||
} catch (error) {}
|
||||
// store
|
||||
// if (level >= STORE_LOG_LEVEL && connectionMongo.connection.readyState === 1) {
|
||||
// // store log
|
||||
// MongoLog.create({
|
||||
// text: msg,
|
||||
// level,
|
||||
// metadata: obj
|
||||
// });
|
||||
// }
|
||||
},
|
||||
debug(msg: string, obj?: Record<string, any>) {
|
||||
this.log(LogLevelEnum.debug, msg, obj);
|
||||
|
10
packages/service/common/system/log/constant.ts
Normal file
10
packages/service/common/system/log/constant.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export enum LogLevelEnum {
|
||||
debug = 0,
|
||||
info = 1,
|
||||
warn = 2,
|
||||
error = 3
|
||||
}
|
||||
|
||||
export enum LogSignEnum {
|
||||
slowOperation = 'slowOperation'
|
||||
}
|
27
packages/service/common/system/log/schema.ts
Normal file
27
packages/service/common/system/log/schema.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { getMongoModel, Schema } from '../../../common/mongo';
|
||||
import { SystemLogType } from './type';
|
||||
import { LogLevelEnum } from './constant';
|
||||
|
||||
export const LogCollectionName = 'system_logs';
|
||||
|
||||
const SystemLogSchema = new Schema({
|
||||
text: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
level: {
|
||||
type: String,
|
||||
required: true,
|
||||
enum: Object.values(LogLevelEnum)
|
||||
},
|
||||
time: {
|
||||
type: Date,
|
||||
default: () => new Date()
|
||||
},
|
||||
metadata: Object
|
||||
});
|
||||
|
||||
SystemLogSchema.index({ time: 1 }, { expires: '15d' });
|
||||
SystemLogSchema.index({ level: 1 });
|
||||
|
||||
export const MongoLog = getMongoModel<SystemLogType>(LogCollectionName, SystemLogSchema);
|
9
packages/service/common/system/log/type.d.ts
vendored
Normal file
9
packages/service/common/system/log/type.d.ts
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
import { LogLevelEnum, LogSignEnum } from './constant';
|
||||
|
||||
export type SystemLogType = {
|
||||
_id: string;
|
||||
text: string;
|
||||
level: LogLevelEnum;
|
||||
time: Date;
|
||||
metadata?: Record<string, any>;
|
||||
};
|
@@ -1,4 +1,4 @@
|
||||
import { connectionMongo, type Model } from '../../mongo';
|
||||
import { connectionMongo, getMongoModel, type Model } from '../../mongo';
|
||||
import { timerIdMap } from './constants';
|
||||
const { Schema, model, models } = connectionMongo;
|
||||
import { TimerLockSchemaType } from './type.d';
|
||||
@@ -24,6 +24,4 @@ try {
|
||||
console.log(error);
|
||||
}
|
||||
|
||||
export const MongoTimerLock: Model<TimerLockSchemaType> =
|
||||
models[collectionName] || model(collectionName, TimerLockSchema);
|
||||
MongoTimerLock.syncIndexes();
|
||||
export const MongoTimerLock = getMongoModel<TimerLockSchemaType>(collectionName, TimerLockSchema);
|
||||
|
Reference in New Issue
Block a user