From ee82f9bb445c7e9fde8cda3068d104dd636536f0 Mon Sep 17 00:00:00 2001 From: nickheyer Date: Sun, 18 Feb 2024 19:11:01 -0800 Subject: [PATCH] #24 Created logging module for core service --- logging.js | 36 +++++++++++++++++++ .../migration.sql | 12 ++----- prisma/schema.prisma | 13 +++---- src/core/CoreService.js | 19 ++-------- src/core/methods/discord/methods.js | 2 +- src/core/methods/websocket/connections.js | 5 +-- 6 files changed, 50 insertions(+), 37 deletions(-) create mode 100644 logging.js rename prisma/migrations/{20240218223750_init => 20240219022103_init}/migration.sql (95%) diff --git a/logging.js b/logging.js new file mode 100644 index 0000000..f407901 --- /dev/null +++ b/logging.js @@ -0,0 +1,36 @@ +function getLogger(opts = {}) { + const { createLogger, transports } = require('winston'); + const logWrapper = require('@epegzz/winston-dev-console').default; + + const winLogger = createLogger({ + level: 'silly', + transports: [ + new (transports.File)({ + name: 'info-file', + filename: `${__dirname}/logs/filelog-info.log`, + level: 'silly' + }), + + new (transports.File)({ + name: 'error-file', + filename: `${__dirname}/logs/filelog-error.log`, + level: 'error' + }) + ] + }); + const logger = logWrapper.init(winLogger); + logger.add( + logWrapper.transport({ + showTimestamps: false, + addLineSeparation: true, + }) + ); + // logger.add( + // new PrismaWinstonTransporter(opts) + // ) + + global.logger = logger; + return logger; +} + +module.exports = getLogger; diff --git a/prisma/migrations/20240218223750_init/migration.sql b/prisma/migrations/20240219022103_init/migration.sql similarity index 95% rename from prisma/migrations/20240218223750_init/migration.sql rename to prisma/migrations/20240219022103_init/migration.sql index ecb470b..f1bb1ec 100644 --- a/prisma/migrations/20240218223750_init/migration.sql +++ b/prisma/migrations/20240219022103_init/migration.sql @@ -27,18 +27,12 @@ CREATE TABLE "State" ( CONSTRAINT "State_active_server_id_fkey" FOREIGN KEY ("active_server_id") REFERENCES "DiscordServer" ("server_id") ON DELETE SET NULL ON UPDATE CASCADE ); --- CreateTable -CREATE TABLE "ErrLog" ( - "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, - "created" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, - "entry" TEXT NOT NULL DEFAULT 'Error Occured' -); - -- CreateTable CREATE TABLE "EventLog" ( "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, - "created" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, - "entry" TEXT NOT NULL DEFAULT 'Event Occured' + "timestamp" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "level" TEXT NOT NULL, + "message" TEXT NOT NULL ); -- CreateTable diff --git a/prisma/schema.prisma b/prisma/schema.prisma index cf87061..93a5aa6 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -40,16 +40,11 @@ model State { activeServer DiscordServer? @relation(fields: [active_server_id], references: [server_id]) } -model ErrLog { - id Int @id @default(autoincrement()) - created DateTime @default(now()) - entry String @default("Error Occured") -} - model EventLog { - id Int @id @default(autoincrement()) - created DateTime @default(now()) - entry String @default("Event Occured") + id Int @id @default(autoincrement()) + timestamp DateTime @default(now()) + level String + message String } model DiscordBot { diff --git a/src/core/CoreService.js b/src/core/CoreService.js index 2a7f117..180c540 100644 --- a/src/core/CoreService.js +++ b/src/core/CoreService.js @@ -4,6 +4,7 @@ const { PrismaClient } = require('@prisma/client'); const http = require('http'); const WebSocket = require('ws'); + class CoreService { static _instance; @@ -63,7 +64,6 @@ class CoreService { get prisma() { if (!this._prisma) { - this.logger.info('Generating Prisma Cli Instance'); this._prisma = new PrismaClient(); } return this._prisma; @@ -106,21 +106,8 @@ class CoreService { } _bindLogging() { - const { createLogger } = require('winston'); - const logWrapper = require('@epegzz/winston-dev-console').default; - - const winLogger = createLogger({ - level: 'silly', - }); - this.logger = logWrapper.init(winLogger); - this.logger.add( - logWrapper.transport({ - showTimestamps: false, - addLineSeparation: true, - }) - ); - - global.logger = this.logger; + console.log('Initializing logger'); + this.logger = require('../../logging')({ prisma: this.prisma }); this.logger.silly('Logging Initialized'); } diff --git a/src/core/methods/discord/methods.js b/src/core/methods/discord/methods.js index 3506245..313a04c 100644 --- a/src/core/methods/discord/methods.js +++ b/src/core/methods/discord/methods.js @@ -1,7 +1,7 @@ module.exports = { async updatePowerState(powerOn, discordBotInst = null) { - this.logger.debug('Changing Discord Bot Power State: ', discordBot); const discordBot = discordBotInst || await this.discordBot.get(); + this.logger.debug('Changing Discord Bot Power State: ', discordBot); await this.emitCompiled([ 'nav/user/buttons/botPowerButton.pug', 'nav/user/userBoxInfo.pug', diff --git a/src/core/methods/websocket/connections.js b/src/core/methods/websocket/connections.js index 98b6017..fe68145 100644 --- a/src/core/methods/websocket/connections.js +++ b/src/core/methods/websocket/connections.js @@ -1,4 +1,5 @@ const uuid = require('uuid'); +const _ = require('lodash'); module.exports = { addClientWS(ws) { @@ -11,14 +12,14 @@ module.exports = { async emit(data) { await Promise.all([...this.connections.keys()].map(async (client) => { - this.logger.debug('Sent data to ws client: ', data); + this.logger.debug(`Sent data to ws client`); await client.send(data); })); }, async emitCompiled(templateFiles = [], templateValues = {}) { const compiledTemplate = await this.compile(templateFiles, templateValues); - this.logger.debug(compiledTemplate); + this.logger.debug(`Sending:\nTemplates:\n${JSON.stringify(templateFiles, 4, 2)}\nValues:\n${JSON.stringify(_.keys(templateValues), 4, 2)}`); await this.emit(compiledTemplate); } };