@xrystal/core 3.17.2 → 3.17.4

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/README.md CHANGED
@@ -1,14 +1,21 @@
1
1
  ## **DOCUMENT**
2
2
 
3
3
  # LOG Schema
4
+ JS
4
5
  '
5
- service: this.serviceName,
6
+ level
7
+ message
8
+ payload?
9
+ code?
10
+ '
11
+ KAFKA
12
+ '
13
+ service,
6
14
  level,
7
-
8
15
  message,
9
- ...rest,
10
- timestamp: new Date().toISOString(),
11
-
12
- id: id || null,
13
- env: this.environment
16
+ payload,
17
+ code,
18
+ timestamp: ISO String,
19
+ id,
20
+ env
14
21
  '
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "author": "Yusuf Yasir KAYGUSUZ",
3
3
  "name": "@xrystal/core",
4
- "version": "3.17.2",
4
+ "version": "3.17.4",
5
5
  "description": "Project core for xrystal",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -16,6 +16,8 @@ export default class ConfigsService {
16
16
  debug: process.env.SYSTEM_LOGGER_LAYER,
17
17
  https: process.env.HTTPS,
18
18
  httpsfileEncoding: process.env.ENCODING || 'utf8',
19
+ httpsCertfile: process.env.HTTPS_CERT_FILE,
20
+ httpsKeyfile: process.env.HTTPS_KEY_FILE,
19
21
  rootFolderPath: rawConfigs.rootFolderPath || 'x',
20
22
  projectName: tmp.project.name,
21
23
  serviceName: rawConfigs.service,
@@ -2,11 +2,17 @@ import winston from "winston";
2
2
  import "winston-daily-rotate-file";
3
3
  import { AsyncLocalStorage } from "node:async_hooks";
4
4
  import ConfigsService from "../configs";
5
- import { IService } from "../../utils";
5
+ import { LoggerLayerEnum, IService } from "../../utils";
6
6
  interface CustomLogger extends winston.Logger {
7
7
  critical: winston.LeveledLogMethod;
8
8
  http: winston.LeveledLogMethod;
9
9
  }
10
+ interface LogOptions {
11
+ level: LoggerLayerEnum;
12
+ message: any;
13
+ payload?: any;
14
+ code?: string | number;
15
+ }
10
16
  export default class LoggerService implements IService {
11
17
  #private;
12
18
  static readonly storage: AsyncLocalStorage<Map<string, string>>;
@@ -18,15 +24,17 @@ export default class LoggerService implements IService {
18
24
  configsService: ConfigsService;
19
25
  });
20
26
  load: (config: Record<string, any>) => Promise<void>;
21
- private safeReplacer;
22
- private getTracingFormat;
23
- private getConsoleFormat;
24
- winston: CustomLogger;
25
- runWithId: <T>(id: string, callback: () => T) => T;
26
27
  winstonLoader: ({ loadPath, loggerLevel }: {
27
28
  loadPath: string;
28
29
  loggerLevel: string;
29
30
  }) => CustomLogger;
31
+ winston: CustomLogger;
32
+ private safeReplacer;
33
+ private getTracingFormat;
34
+ private getConsoleFormat;
35
+ runWithId: <T>(id: string, callback: () => T) => T;
30
36
  private logToKafka;
37
+ log(level: LoggerLayerEnum, message: any, payload?: any, code?: string | number): void;
38
+ log(options: LogOptions): void;
31
39
  }
32
40
  export {};
@@ -74,45 +74,6 @@ export default class LoggerService {
74
74
  loggerLevel: config?.loggerLevel
75
75
  });
76
76
  };
77
- safeReplacer = (key, value) => {
78
- if (value instanceof Headers)
79
- return Object.fromEntries(value.entries());
80
- if (value instanceof Error)
81
- return { message: value.message, stack: value.stack };
82
- return value;
83
- };
84
- getTracingFormat = format((info) => {
85
- const store = LoggerService.storage.getStore();
86
- if (store && store.has("correlationId")) {
87
- info.id = store.get("correlationId");
88
- }
89
- Object.keys(info).forEach(key => {
90
- if (info[key] instanceof Headers) {
91
- info[key] = Object.fromEntries(info[key].entries());
92
- }
93
- });
94
- return info;
95
- });
96
- getConsoleFormat() {
97
- return format.combine(this.getTracingFormat(), format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), format.colorize({ all: true }), format.printf((info) => {
98
- const msg = typeof info.message === "object"
99
- ? JSON.stringify(info.message, this.safeReplacer)
100
- : info.message;
101
- const idPart = info.id ? ` : id: ${info.id}` : "";
102
- return `${info.timestamp} [${this.serviceName}] ${info.level}: ${msg}${idPart}`;
103
- }));
104
- }
105
- winston = winston.createLogger({
106
- level: "debug",
107
- levels: customLevels,
108
- format: this.getConsoleFormat(),
109
- transports: [new winston.transports.Console()]
110
- });
111
- runWithId = (id, callback) => {
112
- const store = new Map();
113
- store.set("correlationId", id);
114
- return LoggerService.storage.run(store, callback);
115
- };
116
77
  winstonLoader = ({ loadPath, loggerLevel }) => {
117
78
  const { combine, timestamp, json, errors } = format;
118
79
  const tracing = this.getTracingFormat();
@@ -147,11 +108,52 @@ export default class LoggerService {
147
108
  });
148
109
  return this.winston;
149
110
  };
111
+ winston = winston.createLogger({
112
+ level: this.#configsService.all.systemLoggerLayer,
113
+ levels: customLevels,
114
+ format: this.getConsoleFormat(),
115
+ transports: [new winston.transports.Console()]
116
+ });
117
+ safeReplacer = (key, value) => {
118
+ if (value instanceof Headers)
119
+ return Object.fromEntries(value.entries());
120
+ if (value instanceof Error)
121
+ return { message: value.message, stack: value.stack };
122
+ return value;
123
+ };
124
+ getTracingFormat = format((info) => {
125
+ const store = LoggerService.storage.getStore();
126
+ if (store && store.has("correlationId")) {
127
+ info.id = store.get("correlationId");
128
+ }
129
+ Object.keys(info).forEach(key => {
130
+ if (info[key] instanceof Headers) {
131
+ info[key] = Object.fromEntries(info[key].entries());
132
+ }
133
+ });
134
+ return info;
135
+ });
136
+ getConsoleFormat() {
137
+ return format.combine(this.getTracingFormat(), format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), format.colorize({ all: true }), format.printf((info) => {
138
+ const msg = typeof info.message === "object"
139
+ ? JSON.stringify(info.message, this.safeReplacer)
140
+ : info.message;
141
+ const idPart = info.id ? ` : id: ${info.id}` : "";
142
+ const payloadPart = info.payload ? `\nPayload: ${JSON.stringify(info.payload, this.safeReplacer)}` : "";
143
+ const codePart = info.code ? ` [Code: ${info.code}]` : "";
144
+ return `${info.timestamp} [${this.serviceName}] ${info.level}${codePart}: ${msg}${idPart}${payloadPart}`;
145
+ }));
146
+ }
147
+ runWithId = (id, callback) => {
148
+ const store = new Map();
149
+ store.set("correlationId", id);
150
+ return LoggerService.storage.run(store, callback);
151
+ };
150
152
  async logToKafka(info) {
151
153
  if (!this.kafkaProducer || !this.isKafkaReady)
152
154
  return;
153
155
  try {
154
- const { id, level, message, ...rest } = info;
156
+ const { id, level, message, payload, code } = info;
155
157
  await this.kafkaProducer.send({
156
158
  topic: this.kafkaTopic,
157
159
  messages: [{
@@ -159,7 +161,8 @@ export default class LoggerService {
159
161
  service: this.serviceName,
160
162
  level,
161
163
  message,
162
- ...rest,
164
+ payload,
165
+ code,
163
166
  timestamp: new Date().toISOString(),
164
167
  id: id || null,
165
168
  env: this.#configsService.all.env
@@ -171,4 +174,24 @@ export default class LoggerService {
171
174
  this.isKafkaReady = false;
172
175
  }
173
176
  }
177
+ log(arg1, arg2, arg3, arg4) {
178
+ let level;
179
+ let message;
180
+ let payload;
181
+ let code;
182
+ if (typeof arg1 === 'object' && arg1 !== null && 'level' in arg1) {
183
+ level = arg1.level;
184
+ message = arg1.message;
185
+ payload = arg1.payload;
186
+ code = arg1.code;
187
+ }
188
+ else {
189
+ level = arg1;
190
+ message = arg2;
191
+ payload = arg3;
192
+ code = arg4;
193
+ }
194
+ const levelName = LoggerLayerEnum[level].toLowerCase();
195
+ this.winston.log(levelName, message, { payload, code });
196
+ }
174
197
  }