dl-commons 0.0.2-testing.37 → 0.0.2-testing.39

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.
@@ -0,0 +1,8 @@
1
+ export declare class Logger {
2
+ private _isGraylog;
3
+ private _host;
4
+ private _port;
5
+ private _hostname;
6
+ private _facility;
7
+ constructor(host: string, port: number, hostname: string, facility: string, isGraylog?: boolean);
8
+ }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Logger = void 0;
4
+ const gelf_pro_1 = require("gelf-pro");
5
+ class Logger {
6
+ constructor(host, port, hostname, facility, isGraylog = false) {
7
+ this._isGraylog = isGraylog;
8
+ if (this._isGraylog) {
9
+ gelf_pro_1.default.setConfig({
10
+ adapterName: 'udp',
11
+ adapterOptions: {
12
+ host,
13
+ port,
14
+ },
15
+ fields: { facility, hostname },
16
+ });
17
+ }
18
+ }
19
+ }
20
+ exports.Logger = Logger;
21
+ //# sourceMappingURL=logger.class.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.class.js","sourceRoot":"","sources":["../../src/utils/logger.class.ts"],"names":[],"mappings":";;;AAAA,uCAA4B;AAE5B,MAAa,MAAM;IASf,YAAY,IAAY,EAAE,IAAY,EAAE,QAAgB,EAAE,QAAgB,EAAE,YAAqB,KAAK;QAClG,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;QAE5B,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;YAClB,kBAAI,CAAC,SAAS,CAAC;gBACX,WAAW,EAAE,KAAK;gBAClB,cAAc,EAAE;oBACZ,IAAI;oBACJ,IAAI;iBACP;gBACD,MAAM,EAAE,EAAE,QAAQ,EAAE,QAAQ,EAAE;aACjC,CAAC,CAAC;QACP,CAAC;IACL,CAAC;CACJ;AAvBD,wBAuBC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dl-commons",
3
3
  "type": "commonjs",
4
- "version": "0.0.2-testing.37",
4
+ "version": "0.0.2-testing.39",
5
5
  "description": "distance learning commons",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -21,6 +21,7 @@
21
21
  "class-transformer": "^0.5.1",
22
22
  "class-validator": "^0.14.0",
23
23
  "express": "^4.18.2",
24
+ "gelf-pro": "^1.4.0",
24
25
  "mongoose": "^8.0.4",
25
26
  "reflect-metadata": "^0.2.2"
26
27
  },
@@ -7,6 +7,7 @@ import { Session, SessionSchema } from './models/schemas/session.schema';
7
7
  import { HttpModule } from '@nestjs/axios';
8
8
  import { ISession } from './models/interfaces/session.interface';
9
9
  import { DictionariesService } from './services/dictionaries.service';
10
+ import { AppLoggerService } from './services/app-logger.service';
10
11
 
11
12
  declare global {
12
13
  namespace Express {
@@ -30,7 +31,7 @@ declare global {
30
31
  MongooseModule.forFeature([{ name: Session.name, schema: SessionSchema }], 'sessions-store'),
31
32
  ],
32
33
  controllers: [],
33
- providers: [SessionsService, DictionariesService],
34
- exports: [SessionsService, DictionariesService],
34
+ providers: [SessionsService, DictionariesService, AppLoggerService],
35
+ exports: [SessionsService, DictionariesService, AppLoggerService],
35
36
  })
36
37
  export class DLCommonsModule {}
@@ -27,27 +27,20 @@ import { mongo } from 'mongoose';
27
27
  import { ValidationError } from 'class-validator';
28
28
  import { HandledException } from '../exceptions/custom.exception';
29
29
  import { RESTError } from '../models/dtos/wrappers/rest-wrapper.class';
30
+ import { AppLoggerService } from '../services/app-logger.service';
30
31
 
31
32
  export class BaseExceptionsFilter implements ExceptionFilter {
32
- private _debug: boolean = false;
33
- private _log: boolean = false;
33
+
34
34
 
35
- constructor(debug: boolean, log?: boolean) {
36
- this._debug = debug;
37
- this._log = log;
35
+ constructor(private readonly _appLoggerService: AppLoggerService) {
38
36
  }
39
37
 
40
38
  catch(exception: any, host: ArgumentsHost) {
41
39
  const context = host.switchToHttp();
42
40
  const response = context.getResponse<Response>();
43
41
 
44
- if (this._debug) {
45
- console.log('BaseExceptionsFilter - name:', exception.constructor.name);
46
- }
47
-
48
- if (this._log) {
49
- console.log(exception);
50
- }
42
+ this._appLoggerService.info('BaseExceptionsFilter - name:', exception.constructor.name);
43
+ this._appLoggerService.info('BaseExceptionsFilter - exception:', exception);
51
44
 
52
45
  let handledException: HandledException | any = null;
53
46
 
@@ -0,0 +1,66 @@
1
+ import { Injectable } from '@nestjs/common';
2
+ import { ConfigService } from '@nestjs/config';
3
+ const gelf = require('gelf-pro');
4
+
5
+ @Injectable()
6
+ export class AppLoggerService {
7
+ //#region Private Members
8
+ private readonly _isGraylogEnabled: boolean = this._configService.get<boolean>('GRAYLOG__ENABLED') || false;
9
+ //#endregion
10
+
11
+ //#region Constructor
12
+ constructor(private readonly _configService: ConfigService) {
13
+ if (this._isGraylogEnabled) {
14
+ gelf.setConfig({
15
+ adapterName: 'udp',
16
+ adapterOptions: {
17
+ host: _configService.get('GRAYLOG__HOST') || 'graylog-input.emis.ge',
18
+ port: _configService.get<number>('GRAYLOG__PORT'),
19
+ },
20
+ fields: { facility: _configService.get('GRAYLOG__FACILITY'), hostname: _configService.get('GRAYLOG__HOSTNAME') },
21
+ });
22
+ }
23
+ }
24
+ //#endregion
25
+
26
+ //#region Public API
27
+ public async info(message: string, data?: any) {
28
+ if (this._isGraylogEnabled) {
29
+ this._send(5, message, data);
30
+ } else {
31
+ console.info(message, data);
32
+ }
33
+ }
34
+
35
+ public async error(message: string, data?: any) {
36
+ if (this._isGraylogEnabled) {
37
+ this._send(2, message, data);
38
+ } else {
39
+ console.error(message, data);
40
+ }
41
+ }
42
+
43
+ public async warn(message: string, data?: any) {
44
+ if (this._isGraylogEnabled) {
45
+ this._send(3, message, data);
46
+ } else {
47
+ console.warn(message, data);
48
+ }
49
+ }
50
+
51
+ public async debug(message: string, data?: any) {
52
+ if (this._isGraylogEnabled) {
53
+ this._send(5, message, data);
54
+ } else {
55
+ console.debug(message, data);
56
+ }
57
+ }
58
+ //#endregion
59
+
60
+ //#region Private APIs
61
+ private _send(level: number, message: string, extra: any = {}) {
62
+ // 1 = fatal, 2 = error, 3 = warn, 4 = info, 5 = debug, 6 = trace
63
+ gelf.message('gelf.log', level, extra);
64
+ }
65
+ //#endregion
66
+ }
@@ -0,0 +1,26 @@
1
+ import gelf from 'gelf-pro';
2
+
3
+ export class Logger {
4
+ //#region Private Members
5
+ private _isGraylog: boolean;
6
+ private _host: string;
7
+ private _port: number;
8
+ private _hostname: string;
9
+ private _facility: string;
10
+ //#endregion
11
+
12
+ constructor(host: string, port: number, hostname: string, facility: string, isGraylog: boolean = false) {
13
+ this._isGraylog = isGraylog;
14
+
15
+ if (this._isGraylog) {
16
+ gelf.setConfig({
17
+ adapterName: 'udp',
18
+ adapterOptions: {
19
+ host,
20
+ port,
21
+ },
22
+ fields: { facility, hostname },
23
+ });
24
+ }
25
+ }
26
+ }