Refactor logging functions in broadcast.js to create logs directory if it doesn't exist. Update setupBroadcast function to pass __dirname for correct path resolution. This enhances log management and ensures logs are stored in the appropriate directory.

This commit is contained in:
Torsten Schulz (local)
2025-12-05 10:51:34 +01:00
parent 351129dace
commit 152d13b1f6
2 changed files with 19 additions and 9 deletions

View File

@@ -1,5 +1,5 @@
import crypto from 'crypto';
import { readFileSync, writeFileSync, appendFileSync } from 'fs';
import { readFileSync, writeFileSync, appendFileSync, existsSync, mkdirSync } from 'fs';
import { join } from 'path';
import axios from 'axios';
@@ -125,9 +125,14 @@ function getConversationKey(user1, user2) {
return [user1, user2].sort().join(':');
}
function logClientLogin(client) {
function logClientLogin(client, __dirname) {
try {
const logPath = join(process.cwd(), 'logs', 'logins.log');
const logsDir = join(__dirname, '../logs');
// Erstelle logs-Verzeichnis falls es nicht existiert
if (!existsSync(logsDir)) {
mkdirSync(logsDir, { recursive: true });
}
const logPath = join(logsDir, 'logins.log');
const logEntry = `${new Date().toISOString()},${client.userName},${client.country},${client.age},${client.gender}\n`;
appendFileSync(logPath, logEntry, 'utf-8');
} catch (error) {
@@ -135,9 +140,14 @@ function logClientLogin(client) {
}
}
function checkAndLogStart() {
function checkAndLogStart(__dirname) {
try {
const logPath = join(process.cwd(), 'logs', 'starts.log');
const logsDir = join(__dirname, '../logs');
// Erstelle logs-Verzeichnis falls es nicht existiert
if (!existsSync(logsDir)) {
mkdirSync(logsDir, { recursive: true });
}
const logPath = join(logsDir, 'starts.log');
const logEntry = `${new Date().toISOString()}\n`;
appendFileSync(logPath, logEntry, 'utf-8');
} catch (error) {
@@ -145,7 +155,7 @@ function checkAndLogStart() {
}
}
export function setupBroadcast(io) {
export function setupBroadcast(io, __dirname) {
// Länderliste beim Start laden
let countriesMap = {};
@@ -553,8 +563,8 @@ export function setupBroadcast(io) {
socketConnected: client.socket ? client.socket.connected : false
});
logClientLogin(client);
checkAndLogStart();
logClientLogin(client, __dirname);
checkAndLogStart(__dirname);
// Benutzerliste an alle senden
broadcastUserList();

View File

@@ -96,7 +96,7 @@ setupSEORoutes(app, __dirname);
setupRoutes(app, __dirname);
// Socket.IO-Handling
setupBroadcast(io);
setupBroadcast(io, __dirname);
// In Production: Serviere auch die gebauten Client-Dateien
// SPA-Fallback muss nach allen anderen Routen stehen