Skip to content

Commit

Permalink
#24 Created logging module for core service
Browse files Browse the repository at this point in the history
  • Loading branch information
nickheyer committed Feb 19, 2024
1 parent bd0145e commit ee82f9b
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 37 deletions.
36 changes: 36 additions & 0 deletions logging.js
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 4 additions & 9 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 3 additions & 16 deletions src/core/CoreService.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { PrismaClient } = require('@prisma/client');
const http = require('http');
const WebSocket = require('ws');


class CoreService {
static _instance;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/methods/discord/methods.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
5 changes: 3 additions & 2 deletions src/core/methods/websocket/connections.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const uuid = require('uuid');
const _ = require('lodash');

module.exports = {
addClientWS(ws) {
Expand All @@ -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);
}
};

0 comments on commit ee82f9b

Please sign in to comment.