jp.ms.common.modules 1.0.8 → 1.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jp.ms.common.modules",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -15,7 +15,7 @@
15
15
  "jp.common.models": "1.0.7",
16
16
  "jp.common.utils": "1.0.2",
17
17
  "jp.db.schemas": "2.0.1",
18
- "jp.ms.common.engine": "2.0.1",
18
+ "jp.ms.common.engine": "2.0.2",
19
19
  "mongoose": "9.1.5",
20
20
  "nestjs-i18n": "10.5.1",
21
21
  "reflect-metadata": "0.2.2",
@@ -1,10 +1,9 @@
1
- import process from 'process';
1
+ import * as process from 'process';
2
2
 
3
3
  export function getCors () {
4
4
  if (process.env.NODE_ENV === 'prod') {
5
5
  return {
6
- origin: [/test.justpoker.online/],
7
- // origin: [new RegExp(process.env.CORS_ADDRESS ?? '')],
6
+ origin: [new RegExp(process.env.CORS_ADDRESS ?? '')],
8
7
  methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
9
8
  credentials: true,
10
9
  }
@@ -8,6 +8,8 @@ import { SimpleRoomsManagerModule } from './simple-rooms-manager/simple-rooms-ma
8
8
  import { SimpleRoomsManagerService } from './simple-rooms-manager/simple-rooms-manager.service';
9
9
  import { RoomsModule } from './rooms/rooms.module';
10
10
  import { SupportModule } from './support/support.module';
11
+ import { JpLoggerModule } from './logger/jp-logger.module';
12
+ import { JpLoggerService } from './logger/jp-logger.service';
11
13
 
12
14
  export {
13
15
  PlayersManagerModule,
@@ -20,4 +22,6 @@ export {
20
22
  SimpleRoomsManagerService,
21
23
  RoomsModule,
22
24
  SupportModule,
25
+ JpLoggerModule,
26
+ JpLoggerService,
23
27
  }
@@ -0,0 +1,10 @@
1
+ import { Global, Module } from '@nestjs/common';
2
+
3
+ import { JpLoggerService } from './jp-logger.service';
4
+
5
+ @Global()
6
+ @Module({
7
+ providers: [JpLoggerService],
8
+ exports: [JpLoggerService],
9
+ })
10
+ export class JpLoggerModule {}
@@ -0,0 +1,32 @@
1
+ import { Injectable, Scope, ConsoleLogger, LogLevel } from '@nestjs/common';
2
+ import * as process from 'process';
3
+
4
+ @Injectable({ scope: Scope.TRANSIENT })
5
+ export class JpLoggerService extends ConsoleLogger {
6
+ constructor () {
7
+ super({ json: true });
8
+ }
9
+
10
+ protected getTimestamp (): string {
11
+ return new Date().toISOString();
12
+ }
13
+
14
+ protected printAsJson (message: unknown, options: {
15
+ context: string;
16
+ logLevel: LogLevel;
17
+ writeStreamType?: "stdout" | "stderr";
18
+ errorStack?: unknown
19
+ }) {
20
+ const logObject = {
21
+ level: options.logLevel,
22
+ pid: process.pid,
23
+ timestamp: this.getTimestamp(),
24
+ message,
25
+ context: options.context ? options.context : undefined,
26
+ };
27
+
28
+ const formattedMessage = JSON.stringify(logObject, this.stringifyReplacer);
29
+
30
+ process[options.writeStreamType ?? 'stdout'].write(`${formattedMessage}\n`);
31
+ }
32
+ }
@@ -1,5 +1,5 @@
1
- import { ConsoleLogger, DynamicModule, Global, Inject, Module, Provider } from '@nestjs/common';
2
- import { MongooseModule, InjectConnection } from '@nestjs/mongoose';
1
+ import { DynamicModule, Global, Inject, Module, Provider } from '@nestjs/common';
2
+ import { MongooseModule, InjectConnection } from '@nestjs/mongoose';
3
3
  import {
4
4
  Room,
5
5
  RoomSchema,
@@ -11,14 +11,11 @@ import {
11
11
  GameResultSchema,
12
12
  GameResultRepository,
13
13
  } from 'jp.db.schemas';
14
- import {
15
- BaseGameEvent,
16
- GameMaster,
17
- GameRoomEvent,
18
- MinPlayersStartStrategy,
19
- } from 'jp.ms.common.engine';
14
+ import { BaseGameEvent, GameMaster, MinPlayersStartStrategy } from 'jp.ms.common.engine';
20
15
  import { Connection } from 'mongoose';
21
16
 
17
+ import { JpLoggerService } from '../logger/jp-logger.service';
18
+
22
19
  import { SimpleRoomsManagerService } from './simple-rooms-manager.service';
23
20
  import { SimpleRoomsFactoryOptions, SimpleRoomsManagerOptions } from './simple-rooms-manager.interfaces';
24
21
  import { SIMPLE_ROOMS_MANAGER_OPTIONS } from './simple-rooms-manager.constants';
@@ -39,8 +36,6 @@ import { SIMPLE_ROOMS_MANAGER_OPTIONS } from './simple-rooms-manager.constants';
39
36
  exports: [SimpleRoomsManagerService],
40
37
  })
41
38
  export class SimpleRoomsManagerModule {
42
- private readonly logger = new ConsoleLogger(SimpleRoomsManagerModule.name);
43
-
44
39
  constructor (
45
40
  private readonly roomsRepository: RoomsRepository,
46
41
  private readonly usersRepository: UsersRepository,
@@ -48,6 +43,7 @@ export class SimpleRoomsManagerModule {
48
43
  private readonly simpleRoomsManagerService: SimpleRoomsManagerService,
49
44
  @Inject(SIMPLE_ROOMS_MANAGER_OPTIONS) private readonly options: SimpleRoomsFactoryOptions,
50
45
  @InjectConnection() private readonly connection: Connection,
46
+ @Inject() private readonly logger: JpLoggerService,
51
47
  ) {
52
48
  }
53
49
 
@@ -99,11 +95,7 @@ export class SimpleRoomsManagerModule {
99
95
  }
100
96
 
101
97
  private handleRoomEvent = (event: BaseGameEvent) => {
102
- if (event instanceof GameRoomEvent) {
103
- this.logger.log(event.constructor.name, event.roomId);
104
- } else {
105
- this.logger.log(event.constructor.name);
106
- }
98
+ this.logger.log(event.toLog());
107
99
  };
108
100
 
109
101
  private handleRoomTableGameFinished = async (master: GameMaster) => {