mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 07:48:37 +01:00
add logger
This commit is contained in:
@@ -9,6 +9,7 @@ export async function getUserProjectFromId(project_id: string, user: AuthContext
|
|||||||
return project;
|
return project;
|
||||||
} else {
|
} else {
|
||||||
if (!user?.logged) return;
|
if (!user?.logged) return;
|
||||||
|
if (!project_id) return;
|
||||||
const project = await ProjectModel.findById(project_id);
|
const project = await ProjectModel.findById(project_id);
|
||||||
if (!project) return;
|
if (!project) return;
|
||||||
const [hasAccess, role] = await hasAccessToProject(user.id, project_id, project);
|
const [hasAccess, role] = await hasAccessToProject(user.id, project_id, project);
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ export const logger = winston.createLogger({
|
|||||||
],
|
],
|
||||||
transports: [
|
transports: [
|
||||||
new winston.transports.Console({
|
new winston.transports.Console({
|
||||||
|
level: 'debug',
|
||||||
format: combine(
|
format: combine(
|
||||||
winston.format.colorize({ all: true }),
|
winston.format.colorize({ all: true }),
|
||||||
errors({ stack: true }),
|
errors({ stack: true }),
|
||||||
@@ -36,6 +37,10 @@ export const logger = winston.createLogger({
|
|||||||
})
|
})
|
||||||
),
|
),
|
||||||
}),
|
}),
|
||||||
new winston.transports.File({ filename: 'winston-logs.ndjson' })
|
new winston.transports.File({ filename: 'winston-logs.ndjson' }),
|
||||||
|
new winston.transports.File({
|
||||||
|
level: 'debug',
|
||||||
|
filename: 'winston-debug.ndjson'
|
||||||
|
})
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
7
dashboard/server/middleware/00-performance-start.ts
Normal file
7
dashboard/server/middleware/00-performance-start.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import { logger } from "../Logger"
|
||||||
|
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const start = Date.now();
|
||||||
|
event.context['performance-start'] = start.toString();
|
||||||
|
});
|
||||||
@@ -24,29 +24,21 @@ async function authorizationMiddleware(event: H3Event<EventHandlerRequest>) {
|
|||||||
const authorization = event.headers.get('Authorization');
|
const authorization = event.headers.get('Authorization');
|
||||||
|
|
||||||
if (!authorization) {
|
if (!authorization) {
|
||||||
event.context.auth = { logged: false, }
|
|
||||||
|
event.context.auth = { logged: false }
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
const [type, token] = authorization.split(' ');
|
const [type, token] = authorization.split(' ');
|
||||||
|
|
||||||
const valid = readUserJwt(token);
|
const valid = readUserJwt(token);
|
||||||
|
|
||||||
if (!valid) return event.context.auth = { logged: false }
|
if (!valid) return event.context.auth = { logged: false }
|
||||||
|
|
||||||
const user = await UserModel.findOne({ email: valid.email })
|
const user = await UserModel.findOne({ email: valid.email })
|
||||||
|
|
||||||
if (!user) return event.context.auth = { logged: false };
|
if (!user) return event.context.auth = { logged: false };
|
||||||
|
|
||||||
const premium: any = null;//await PremiumModel.findOne({ user_id: user.id });
|
|
||||||
|
|
||||||
const roles: string[] = [];
|
const roles: string[] = [];
|
||||||
|
|
||||||
if (premium && premium.ends_at.getTime() < Date.now()) {
|
|
||||||
// await PremiumModel.deleteOne({ user_id: user.id });
|
|
||||||
} else if (premium) {
|
|
||||||
roles.push('PREMIUM');
|
|
||||||
roles.push('PREMIUM_' + premium.type);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ADMIN_EMAILS.includes(user.email)) {
|
if (ADMIN_EMAILS.includes(user.email)) {
|
||||||
roles.push('ADMIN');
|
roles.push('ADMIN');
|
||||||
}
|
}
|
||||||
@@ -61,6 +53,7 @@ async function authorizationMiddleware(event: H3Event<EventHandlerRequest>) {
|
|||||||
},
|
},
|
||||||
id: user._id.toString()
|
id: user._id.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
event.context.auth = authContext;
|
event.context.auth = authContext;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
28
dashboard/server/middleware/02-logging.ts
Normal file
28
dashboard/server/middleware/02-logging.ts
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import { logger } from "../Logger"
|
||||||
|
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
|
||||||
|
const ip = getRequestAddress(event);
|
||||||
|
const user = getRequestUser(event);
|
||||||
|
|
||||||
|
event.node.res.on('finish', () => {
|
||||||
|
if (!event.context['performance-start']) return;
|
||||||
|
const start = parseInt(event.context['performance-start']);
|
||||||
|
if (isNaN(start)) return;
|
||||||
|
|
||||||
|
const end = Date.now();
|
||||||
|
const duration = (end - start);
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
logger.debug('Request without user', { path: event.path, method: event.method, ip, duration });
|
||||||
|
} else if (!user.logged) {
|
||||||
|
logger.debug('Request as guest', { path: event.path, method: event.method, ip, duration });
|
||||||
|
} else {
|
||||||
|
logger.debug(`(${duration}ms) [${event.method}] ${event.path} { ${user.user.email} }`, { ip });
|
||||||
|
}
|
||||||
|
|
||||||
|
// event.node.res.setHeader('X-Total-Response-Time', `${duration.toFixed(2)} ms`);
|
||||||
|
});
|
||||||
|
|
||||||
|
})
|
||||||
@@ -4,15 +4,14 @@ import { createClient } from 'redis';
|
|||||||
const runtimeConfig = useRuntimeConfig();
|
const runtimeConfig = useRuntimeConfig();
|
||||||
|
|
||||||
export const DATA_EXPIRE_TIME = 30;
|
export const DATA_EXPIRE_TIME = 30;
|
||||||
export const TIMELINE_EXPIRE_TIME = 60 * 5;
|
export const TIMELINE_EXPIRE_TIME = 60;
|
||||||
export const COUNTS_EXPIRE_TIME = 10;
|
export const COUNTS_EXPIRE_TIME = 10;
|
||||||
|
|
||||||
export const COUNTS_OLD_SESSIONS_EXPIRE_TIME = 60 * 5;
|
|
||||||
export const COUNTS_SESSIONS_EXPIRE_TIME = 60 * 3;
|
export const COUNTS_SESSIONS_EXPIRE_TIME = 60 * 3;
|
||||||
|
|
||||||
export const EVENT_NAMES_EXPIRE_TIME = 60;
|
export const EVENT_NAMES_EXPIRE_TIME = 60;
|
||||||
|
|
||||||
export const EVENT_METADATA_FIELDS_EXPIRE_TIME = 120;
|
export const EVENT_METADATA_FIELDS_EXPIRE_TIME = 30;
|
||||||
|
|
||||||
|
|
||||||
export class Redis {
|
export class Redis {
|
||||||
|
|||||||
Reference in New Issue
Block a user