Skip to content

Commit

Permalink
feat(version): v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianjnuwu committed Jan 16, 2025
1 parent 77b237d commit 5788233
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
15 changes: 6 additions & 9 deletions server/src/functions/logger.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import { createLogger as createWinstonLogger, format, transports, Logger } from "winston";
import { createLogger as createWinstonLogger, format, transports, Logger as WinstonLogger } from "winston";
import colors from "colors";

/**
* Creates a logger with colored output for console.
*
* @param {string | number} [context="Socket"] - The context identifier for the logger.
* @returns {Logger} - A configured Winston logger instance.
*/
export function Logger(context: string): Logger {
return createWinstonLogger({
const logger = createWinstonLogger({
level: "info",
format: format.combine(
format.timestamp({
format: "YYYY-MM-DD HH:mm:ss", // Example: 2025-01-15 14:30:45
}),
format.printf((info) => {
const timestamp = colors.green(info.timestamp); // Green for timestamp
const prefix = `[${colors.cyan(context)}]`; // Cyan for context
const timestamp = colors.green(info.timestamp as string); // Green for timestamp
const prefix = `[${colors.cyan('server')}]`; // Cyan for context
const level = {
error: colors.red(info.level.toUpperCase()), // Red for errors
warn: colors.yellow(info.level.toUpperCase()), // Yellow for warnings
Expand All @@ -37,4 +33,5 @@ export function Logger(context: string): Logger {
new transports.Console(),
],
});
}

export { logger };
9 changes: 4 additions & 5 deletions server/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Logger } from "./functions/logger";
import { logger } from "./functions/logger";
import express from "express";
import { createServer } from "node:http";
import { Server, type Socket } from "socket.io";
Expand All @@ -12,7 +12,6 @@ import type {
} from "./types/rooms";
import colors from "colors";
import "dotenv/config"
const logger = Logger("Server");

/**
* Initializes the Express application.
Expand Down Expand Up @@ -123,7 +122,7 @@ io.on("connection", (socket: Socket) => {

io.to(room_code).emit("update_room", { room_player, room });

logger.info(`Player "${colors.bold.brightGreen.underline(room_player)}" joined room "${colors.bold.brightGreen.underline(room_code)}".`);
logger.info(`Player "${colors.bold.green.underline(room_player)}" joined room "${colors.bold.green.underline(room_code)}".`);
});

/**
Expand Down Expand Up @@ -197,7 +196,7 @@ io.on("connection", (socket: Socket) => {

io.to(randomRoom.code).emit("update_room", { room_player, room: randomRoom });

logger.info(`Player "${colors.bold.brightGreen.underline(room_player)}" joined random room "${randomRoom.code}".`);
logger.info(`Player "${colors.bold.green.underline(room_player)}" joined random room "${randomRoom.code}".`);
});

/**
Expand Down Expand Up @@ -317,5 +316,5 @@ io.on("connection", (socket: Socket) => {


HTTP.listen(process.env.PORT, () => {
logger.info(`Socket running: ` + colors.bold.brightGreen.underline(`http://0.0.0.0:${process.env.PORT}`));
logger.info(`Socket running: ` + colors.bold.green.underline(`http://0.0.0.0:${process.env.PORT}`));
});

0 comments on commit 5788233

Please sign in to comment.