Skip to content
This repository has been archived by the owner on Apr 23, 2023. It is now read-only.

Commit

Permalink
Merge pull request #132 from DynamicaBot:sebasptsch/issue130_1
Browse files Browse the repository at this point in the history
Bot crashes on start
  • Loading branch information
sebasptsch committed Nov 7, 2022
2 parents 7f7ed54 + 12ba934 commit e719b33
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/services/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@ import { Signales } from '@dynamicabot/signales';
import { Service, Token } from 'typedi';
import fs from 'node:fs';
import fsP from 'node:fs/promises';
import path from 'node:path';

export const LoggerToken = new Token<Logger>('Logger');

// DATABASE URL IS RELATIVE TO PRISMA CONFIGURATION
const logDir = path.join(
path.dirname(new URL(process.env.DATABASE_URL, import.meta.url).pathname),
'logs'
);

console.log('Log directory:', logDir);

// generate file name depending on the date (e.g. 2021-09-01-1.log, 2021-09-01-2.log, etc.) with a suffix parameter
const createLogfileWriter = async (suffix: string = 'log') => {
await fsP.mkdir('logs', { recursive: true });
await fsP.mkdir(logDir, { recursive: true });
const date = new Date();
const filename = `logs/${date.getFullYear()}-${
date.getMonth() + 1
}-${date.getDate()}-${suffix}.log`;
const filename = path.join(
logDir,
`${date.getFullYear()}-${
date.getMonth() + 1
}-${date.getDate()}-${suffix}.log`
);
const writer = fs.createWriteStream(filename);

return writer;
Expand Down

0 comments on commit e719b33

Please sign in to comment.