@xrystal/core 3.5.4 → 3.5.6

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,7 +1,7 @@
1
1
  {
2
2
  "author": "Yusuf Yasir KAYGUSUZ",
3
3
  "name": "@xrystal/core",
4
- "version": "3.5.4",
4
+ "version": "3.5.6",
5
5
  "description": "Project core for xrystal",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -12,6 +12,7 @@ export default class LoggerService {
12
12
  private kafkaProducer;
13
13
  private kafkaTopic;
14
14
  private isKafkaReady;
15
+ private safeReplacer;
15
16
  private getTracingFormat;
16
17
  private getConsoleFormat;
17
18
  winston: CustomLogger;
@@ -26,18 +26,32 @@ export default class LoggerService {
26
26
  kafkaProducer = null;
27
27
  kafkaTopic = "";
28
28
  isKafkaReady = false;
29
+ safeReplacer = (key, value) => {
30
+ if (value instanceof Headers)
31
+ return Object.fromEntries(value.entries());
32
+ if (value instanceof Error)
33
+ return { message: value.message, stack: value.stack };
34
+ return value;
35
+ };
29
36
  getTracingFormat = format((info) => {
30
37
  const store = LoggerService.storage.getStore();
31
38
  if (store) {
32
- info.correlationId = store.get("correlationId");
39
+ info.id = store.get("correlationId");
33
40
  }
41
+ Object.keys(info).forEach(key => {
42
+ if (info[key] instanceof Headers) {
43
+ info[key] = Object.fromEntries(info[key].entries());
44
+ }
45
+ });
34
46
  return info;
35
47
  });
36
48
  getConsoleFormat() {
37
49
  return format.combine(this.getTracingFormat(), format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), format.colorize({ all: true }), format.printf((info) => {
38
- const msg = typeof info.message === "object" ? JSON.stringify(info.message) : info.message;
39
- const id = info.correlationId ? ` [ID: ${info.correlationId}]` : "";
40
- return `${info.timestamp} [${this.serviceName}]${info.level}: ${msg} : ${id}`;
50
+ const msg = typeof info.message === "object"
51
+ ? JSON.stringify(info.message, this.safeReplacer)
52
+ : info.message;
53
+ const idPart = info.id ? ` : id: ${info.id}` : "";
54
+ return `${info.timestamp} [${this.serviceName}] ${info.level}: ${msg}${idPart}`;
41
55
  }));
42
56
  }
43
57
  winston = winston.createLogger({
@@ -55,7 +69,7 @@ export default class LoggerService {
55
69
  runWithId = (id, callback) => {
56
70
  const store = new Map();
57
71
  store.set("correlationId", id);
58
- LoggerService.storage.run(store, callback);
72
+ return LoggerService.storage.run(store, callback);
59
73
  };
60
74
  load = async (config) => {
61
75
  this.serviceName = config?.serviceName || "service";
@@ -95,7 +109,7 @@ export default class LoggerService {
95
109
  };
96
110
  winstonLoader = ({ loadPath, loggerLevel }) => {
97
111
  const { combine, timestamp, json, errors } = format;
98
- const jsonFileFormat = combine(this.getTracingFormat(), timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), errors({ stack: true }), json());
112
+ const jsonFileFormat = combine(this.getTracingFormat(), timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), errors({ stack: true }), json({ replacer: this.safeReplacer }));
99
113
  const winstonLogger = winston.createLogger({
100
114
  level: loggerLevel,
101
115
  levels: customLevels,
@@ -131,15 +145,19 @@ export default class LoggerService {
131
145
  if (!this.kafkaProducer || !this.isKafkaReady)
132
146
  return;
133
147
  try {
148
+ const { id, level, message, ...rest } = info;
134
149
  await this.kafkaProducer.send({
135
150
  topic: this.kafkaTopic,
136
151
  messages: [{
137
152
  value: JSON.stringify({
138
- ...info,
153
+ timestamp: new Date().toISOString(),
139
154
  service: this.serviceName,
155
+ level,
156
+ message,
157
+ ...rest,
158
+ id: id || null,
140
159
  env: this.environment,
141
- timestamp: new Date().toISOString()
142
- })
160
+ }, this.safeReplacer)
143
161
  }]
144
162
  });
145
163
  }
@@ -12,6 +12,7 @@ export default class LoggerService {
12
12
  private kafkaProducer;
13
13
  private kafkaTopic;
14
14
  private isKafkaReady;
15
+ private safeReplacer;
15
16
  private getTracingFormat;
16
17
  private getConsoleFormat;
17
18
  winston: CustomLogger;
@@ -26,18 +26,32 @@ export default class LoggerService {
26
26
  kafkaProducer = null;
27
27
  kafkaTopic = "";
28
28
  isKafkaReady = false;
29
+ safeReplacer = (key, value) => {
30
+ if (value instanceof Headers)
31
+ return Object.fromEntries(value.entries());
32
+ if (value instanceof Error)
33
+ return { message: value.message, stack: value.stack };
34
+ return value;
35
+ };
29
36
  getTracingFormat = format((info) => {
30
37
  const store = LoggerService.storage.getStore();
31
38
  if (store) {
32
- info.correlationId = store.get("correlationId");
39
+ info.id = store.get("correlationId");
33
40
  }
41
+ Object.keys(info).forEach(key => {
42
+ if (info[key] instanceof Headers) {
43
+ info[key] = Object.fromEntries(info[key].entries());
44
+ }
45
+ });
34
46
  return info;
35
47
  });
36
48
  getConsoleFormat() {
37
49
  return format.combine(this.getTracingFormat(), format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), format.colorize({ all: true }), format.printf((info) => {
38
- const msg = typeof info.message === "object" ? JSON.stringify(info.message) : info.message;
39
- const id = info.correlationId ? ` [ID: ${info.correlationId}]` : "";
40
- return `${info.timestamp} [${this.serviceName}]${info.level}: ${msg} : ${id}`;
50
+ const msg = typeof info.message === "object"
51
+ ? JSON.stringify(info.message, this.safeReplacer)
52
+ : info.message;
53
+ const idPart = info.id ? ` : id: ${info.id}` : "";
54
+ return `${info.timestamp} [${this.serviceName}] ${info.level}: ${msg}${idPart}`;
41
55
  }));
42
56
  }
43
57
  winston = winston.createLogger({
@@ -55,7 +69,7 @@ export default class LoggerService {
55
69
  runWithId = (id, callback) => {
56
70
  const store = new Map();
57
71
  store.set("correlationId", id);
58
- LoggerService.storage.run(store, callback);
72
+ return LoggerService.storage.run(store, callback);
59
73
  };
60
74
  load = async (config) => {
61
75
  this.serviceName = config?.serviceName || "service";
@@ -95,7 +109,7 @@ export default class LoggerService {
95
109
  };
96
110
  winstonLoader = ({ loadPath, loggerLevel }) => {
97
111
  const { combine, timestamp, json, errors } = format;
98
- const jsonFileFormat = combine(this.getTracingFormat(), timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), errors({ stack: true }), json());
112
+ const jsonFileFormat = combine(this.getTracingFormat(), timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), errors({ stack: true }), json({ replacer: this.safeReplacer }));
99
113
  const winstonLogger = winston.createLogger({
100
114
  level: loggerLevel,
101
115
  levels: customLevels,
@@ -131,15 +145,19 @@ export default class LoggerService {
131
145
  if (!this.kafkaProducer || !this.isKafkaReady)
132
146
  return;
133
147
  try {
148
+ const { id, level, message, ...rest } = info;
134
149
  await this.kafkaProducer.send({
135
150
  topic: this.kafkaTopic,
136
151
  messages: [{
137
152
  value: JSON.stringify({
138
- ...info,
153
+ timestamp: new Date().toISOString(),
139
154
  service: this.serviceName,
155
+ level,
156
+ message,
157
+ ...rest,
158
+ id: id || null,
140
159
  env: this.environment,
141
- timestamp: new Date().toISOString()
142
- })
160
+ }, this.safeReplacer)
143
161
  }]
144
162
  });
145
163
  }