Skip to content

Commit

Permalink
feat: Add logger module
Browse files Browse the repository at this point in the history
  • Loading branch information
SevenWaysDP committed Aug 20, 2024
1 parent 2c45288 commit f9b59a8
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 18 deletions.
9 changes: 7 additions & 2 deletions src/apps/tldraw-server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NestFactory } from '@nestjs/core';
import { App } from 'uws';
import { Logger } from '../infra/logging/logger.mjs';
import { ServerModule } from '../modules/server/server.module.mjs';

async function bootstrap() {
Expand All @@ -11,14 +12,18 @@ async function bootstrap() {
nestApp.enableCors();

await nestApp.init();

const logger = await nestApp.resolve(Logger);
logger.setContext('TLDRAW');

webSocketServer.listen(wsPort, (t) => {
if (t) {
console.log(`TLDRAW Websocket Server is running on port ${wsPort}`);
logger.log(`Websocket Server is running on port ${wsPort}`);
}
});

await nestApp.listen(httpPort, () => {
console.log(`TLDRAW Server is running on port ${httpPort}`);
logger.log(`Server is running on port ${httpPort}`);
});
}
bootstrap();
3 changes: 3 additions & 0 deletions src/infra/logging/index.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './logger.mjs';
export * from './logger.module.mjs';

9 changes: 9 additions & 0 deletions src/infra/logging/logger.module.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

import { Module } from '@nestjs/common';
import { Logger } from './logger.mjs';

@Module({
providers: [Logger],
exports: [Logger]
})
export class LoggerModule {}
4 changes: 4 additions & 0 deletions src/infra/logging/logger.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { ConsoleLogger, Injectable, Scope } from '@nestjs/common';

@Injectable({ scope: Scope.TRANSIENT })
export class Logger extends ConsoleLogger {}
2 changes: 2 additions & 0 deletions src/infra/redis/redis.module.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Module } from '@nestjs/common';
import { LoggerModule } from '../logging/logger.module.mjs';
import { RedisService } from './redis.service.mjs';

@Module({
imports: [LoggerModule],
providers: [RedisService],
exports: [RedisService],
})
Expand Down
9 changes: 6 additions & 3 deletions src/infra/redis/redis.service.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import { ConfigService } from '@nestjs/config';
import * as dns from 'dns';
import { Redis } from 'ioredis';
import * as util from 'util';
import { Logger } from '../logging/logger.mjs';

@Injectable()
export class RedisService {
private sentinelServiceName: string;

constructor(private configService: ConfigService) {
constructor(private configService: ConfigService, private logger: Logger) {
this.sentinelServiceName = this.configService.get<string>(
'REDIS_SENTINEL_SERVICE_NAME') || '';

this.logger.setContext(RedisService.name);
}

async getRedisInstance() {
Expand All @@ -35,7 +38,7 @@ export class RedisService {
const sentinelName = this.configService.get<string>('REDIS_SENTINEL_NAME') || 'mymaster';
const sentinelPassword = this.configService.getOrThrow('REDIS_SENTINEL_PASSWORD');
const sentinels = await this.discoverSentinelHosts();
console.log('Discovered sentinels:', sentinels);
this.logger.log('Discovered sentinels:', sentinels);

const redisInstance = new Redis({
sentinels,
Expand All @@ -59,7 +62,7 @@ export class RedisService {

return hosts;
} catch (err) {
console.error('Error during service discovery:', err);
this.logger.error('Error during service discovery:', err);
throw err;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/infra/storage/storage.module.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Module } from '@nestjs/common';
import { LoggerModule } from '../logging/logger.module.mjs';
import { StorageService } from './storage.service.mjs';

@Module({
imports: [LoggerModule],
providers: [StorageService],
exports: [StorageService],
})
Expand Down
9 changes: 6 additions & 3 deletions src/infra/storage/storage.service.mts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Logger } from '../logging/logger.mjs';

@Injectable()
export class StorageService {
constructor(private configService: ConfigService) {}
constructor(private configService: ConfigService, private logger: Logger) {
this.logger.setContext(StorageService.name);
}

async get() {
const s3Endpoint = this.configService.get<string>('S3_ENDPOINT');
Expand All @@ -12,7 +15,7 @@ export class StorageService {
let store;

if (s3Endpoint) {
console.log('using s3 store');
this.logger.log('using s3 store');
const { createS3Storage } = await import('@y/redis/storage/s3');

store = createS3Storage(bucketName);
Expand All @@ -21,7 +24,7 @@ export class StorageService {
await store.client.makeBucket(bucketName);
} catch (e) {}
} else {
console.log('ATTENTION! using in-memory store');
this.logger.log('ATTENTION! using in-memory store');
const { createMemoryStorage } = await import('@y/redis/storage/memory');
store = createMemoryStorage();
}
Expand Down
7 changes: 2 additions & 5 deletions src/modules/worker/domain/worker.service.mts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
import { Injectable, OnModuleInit } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { createWorker } from '@y/redis';
import { RedisService } from '../../../infra/redis/redis.service.mjs';
import { StorageService } from '../../../infra/storage/storage.service.mjs';

@Injectable()
export class WorkerService implements OnModuleInit, OnModuleDestroy {
export class WorkerService implements OnModuleInit {
constructor(
private storage: StorageService,
private redisService: RedisService,
Expand All @@ -21,7 +21,4 @@ export class WorkerService implements OnModuleInit, OnModuleDestroy {
await this.redisService.getRedisInstance(),
);
}
onModuleDestroy() {
throw new Error('Method not implemented.');
}
}
8 changes: 3 additions & 5 deletions src/modules/worker/worker.module.mts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { RedisService } from '../../infra/redis/redis.service.mjs';
import { StorageService } from '../../infra/storage/storage.service.mjs';
import { RedisModule } from '../../infra/redis/redis.module.mjs';
import { StorageModule } from '../../infra/storage/storage.module.mjs';
import { WorkerService } from './domain/worker.service.mjs';

@Module({
imports: [ConfigModule.forRoot({isGlobal: true})],
imports: [ConfigModule.forRoot({isGlobal: true}), RedisModule, StorageModule],
providers: [
RedisService,
StorageService,
WorkerService
],
})
Expand Down

0 comments on commit f9b59a8

Please sign in to comment.