add logger

This commit is contained in:
Emily
2024-09-17 13:38:02 +02:00
parent fd5eca29cc
commit 86011c38ce
6 changed files with 50 additions and 17 deletions

View File

@@ -9,6 +9,7 @@ export async function getUserProjectFromId(project_id: string, user: AuthContext
return project;
} else {
if (!user?.logged) return;
if (!project_id) return;
const project = await ProjectModel.findById(project_id);
if (!project) return;
const [hasAccess, role] = await hasAccessToProject(user.id, project_id, project);

View File

@@ -23,6 +23,7 @@ export const logger = winston.createLogger({
],
transports: [
new winston.transports.Console({
level: 'debug',
format: combine(
winston.format.colorize({ all: 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'
})
]
});

View File

@@ -0,0 +1,7 @@
import { logger } from "../Logger"
export default defineEventHandler(async (event) => {
const start = Date.now();
event.context['performance-start'] = start.toString();
});

View File

@@ -24,29 +24,21 @@ async function authorizationMiddleware(event: H3Event<EventHandlerRequest>) {
const authorization = event.headers.get('Authorization');
if (!authorization) {
event.context.auth = { logged: false, }
event.context.auth = { logged: false }
} else {
const [type, token] = authorization.split(' ');
const valid = readUserJwt(token);
if (!valid) return event.context.auth = { logged: false }
const user = await UserModel.findOne({ email: valid.email })
if (!user) return event.context.auth = { logged: false };
const premium: any = null;//await PremiumModel.findOne({ user_id: user.id });
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)) {
roles.push('ADMIN');
}
@@ -61,6 +53,7 @@ async function authorizationMiddleware(event: H3Event<EventHandlerRequest>) {
},
id: user._id.toString()
}
event.context.auth = authContext;
}

View 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`);
});
})

View File

@@ -4,15 +4,14 @@ import { createClient } from 'redis';
const runtimeConfig = useRuntimeConfig();
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_OLD_SESSIONS_EXPIRE_TIME = 60 * 5;
export const COUNTS_SESSIONS_EXPIRE_TIME = 60 * 3;
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 {