@ngn-net/nestjs-telescope 0.2.11 → 0.2.13

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.
@@ -36,4 +36,9 @@ export declare class TelescopeService implements OnModuleInit {
36
36
  record(entry: Partial<TelescopeEntry> & {
37
37
  type: EntryType;
38
38
  }): Promise<void>;
39
+ /**
40
+ * Check if Telescope is currently recording an entry in the active context.
41
+ * This is useful for watchers (like LogWatcher) to prevent infinite loops.
42
+ */
43
+ isRecording(): boolean;
39
44
  }
@@ -136,6 +136,13 @@ let TelescopeService = class TelescopeService {
136
136
  }
137
137
  });
138
138
  }
139
+ /**
140
+ * Check if Telescope is currently recording an entry in the active context.
141
+ * This is useful for watchers (like LogWatcher) to prevent infinite loops.
142
+ */
143
+ isRecording() {
144
+ return !!this.asyncLocalStorage.getStore();
145
+ }
139
146
  };
140
147
  exports.TelescopeService = TelescopeService;
141
148
  exports.TelescopeService = TelescopeService = __decorate([
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@ngn-net/nestjs-telescope",
3
- "version": "0.2.11",
4
- "builtAt": "2026-06-08T12:52:50.170Z"
3
+ "version": "0.2.13",
4
+ "builtAt": "2026-06-08T12:58:05.511Z"
5
5
  }
@@ -2,7 +2,7 @@ import { OnApplicationBootstrap } from '@nestjs/common';
2
2
  import { TelescopeService } from '../telescope.service';
3
3
  export declare class LogWatcher implements OnApplicationBootstrap {
4
4
  private readonly telescope;
5
- private isRecording;
6
5
  constructor(telescope: TelescopeService);
7
6
  onApplicationBootstrap(): void;
7
+ private init;
8
8
  }
@@ -16,107 +16,65 @@ const telescope_service_1 = require("../telescope.service");
16
16
  const entry_type_enum_1 = require("../enums/entry-type.enum");
17
17
  let LogWatcher = class LogWatcher {
18
18
  telescope;
19
- isRecording = false; // Guard against infinite recursion
20
19
  constructor(telescope) {
21
20
  this.telescope = telescope;
21
+ this.init();
22
22
  }
23
23
  onApplicationBootstrap() {
24
- const originalLog = console.log.bind(console);
25
- const originalError = console.error.bind(console);
26
- const originalWarn = console.warn.bind(console);
27
- const originalDebug = console.debug.bind(console);
24
+ // Keep for framework interface compatibility
25
+ }
26
+ init() {
28
27
  const self = this;
29
- const formatArgs = (args) => args.map((a) => (typeof a === 'object' ? JSON.stringify(a) : String(a))).join(' ');
30
- const recordConsoleLog = (level, args) => {
31
- if (self.isRecording)
28
+ const originalStdoutWrite = process.stdout.write;
29
+ const originalStderrWrite = process.stderr.write;
30
+ const handleWrite = (levelDefault, chunk, encoding) => {
31
+ // If telescope is currently recording, bypass to avoid infinite recursion loops
32
+ if (self.telescope.isRecording()) {
32
33
  return;
33
- const message = formatArgs(args);
34
- // Skip telescope queries, logs, or dashboard routes to prevent recursion
35
- if (message.includes('telescope_entries') || message.includes('TelescopeModule')) {
34
+ }
35
+ // Decode buffer/string
36
+ let rawMessage = '';
37
+ if (typeof chunk === 'string') {
38
+ rawMessage = chunk;
39
+ }
40
+ else if (Buffer.isBuffer(chunk)) {
41
+ rawMessage = chunk.toString(encoding || 'utf8');
42
+ }
43
+ // Skip telescope internal queries or module logs
44
+ if (rawMessage.includes('telescope_entries') || rawMessage.includes('TelescopeModule')) {
36
45
  return;
37
46
  }
38
- self.isRecording = true;
39
- try {
47
+ // Parse the log message
48
+ const parsed = parseConsoleLog(rawMessage);
49
+ if (parsed) {
50
+ // If NestJS parsed level exists, use it, otherwise use levelDefault (e.g. 'error' for stderr)
51
+ const level = parsed.level === 'log' && levelDefault === 'error' ? 'error' : parsed.level;
40
52
  self.telescope.record({
41
53
  type: entry_type_enum_1.EntryType.LOG,
42
- content: { level, message },
54
+ content: { level, message: parsed.message },
43
55
  }).catch(() => { });
44
56
  }
45
- finally {
46
- self.isRecording = false;
47
- }
48
- };
49
- // 1. Intercept raw console logs
50
- console.log = function (...args) {
51
- recordConsoleLog('log', args);
52
- return originalLog(...args);
53
- };
54
- console.error = function (...args) {
55
- recordConsoleLog('error', args);
56
- return originalError(...args);
57
57
  };
58
- console.warn = function (...args) {
59
- recordConsoleLog('warn', args);
60
- return originalWarn(...args);
58
+ // Override process.stdout.write
59
+ process.stdout.write = function (chunk, encoding, callback) {
60
+ try {
61
+ handleWrite('log', chunk, encoding);
62
+ }
63
+ catch {
64
+ // Safe guard
65
+ }
66
+ return originalStdoutWrite.call(process.stdout, chunk, encoding, callback);
61
67
  };
62
- console.debug = function (...args) {
63
- recordConsoleLog('debug', args);
64
- return originalDebug(...args);
68
+ // Override process.stderr.write
69
+ process.stderr.write = function (chunk, encoding, callback) {
70
+ try {
71
+ handleWrite('error', chunk, encoding);
72
+ }
73
+ catch {
74
+ // Safe guard
75
+ }
76
+ return originalStderrWrite.call(process.stderr, chunk, encoding, callback);
65
77
  };
66
- // 2. Intercept NestJS static Logger methods directly
67
- try {
68
- const originalStaticLog = common_1.Logger.log;
69
- const originalStaticError = common_1.Logger.error;
70
- const originalStaticWarn = common_1.Logger.warn;
71
- const originalStaticDebug = common_1.Logger.debug;
72
- const originalStaticVerbose = common_1.Logger.verbose;
73
- const recordNestLog = (level, message, optionalParams) => {
74
- if (self.isRecording)
75
- return;
76
- let formattedMessage = typeof message === 'object' ? JSON.stringify(message) : String(message);
77
- if (optionalParams.length > 0) {
78
- const context = optionalParams[0];
79
- const contextStr = typeof context === 'string' ? `[${context}] ` : '';
80
- formattedMessage = contextStr + formattedMessage;
81
- }
82
- if (formattedMessage.includes('telescope_entries') || formattedMessage.includes('TelescopeModule')) {
83
- return;
84
- }
85
- self.isRecording = true;
86
- try {
87
- self.telescope.record({
88
- type: entry_type_enum_1.EntryType.LOG,
89
- content: { level, message: formattedMessage },
90
- }).catch(() => { });
91
- }
92
- finally {
93
- self.isRecording = false;
94
- }
95
- };
96
- common_1.Logger.log = function (message, ...optionalParams) {
97
- recordNestLog('log', message, optionalParams);
98
- return originalStaticLog.call(common_1.Logger, message, ...optionalParams);
99
- };
100
- common_1.Logger.error = function (message, ...optionalParams) {
101
- recordNestLog('error', message, optionalParams);
102
- return originalStaticError.call(common_1.Logger, message, ...optionalParams);
103
- };
104
- common_1.Logger.warn = function (message, ...optionalParams) {
105
- recordNestLog('warn', message, optionalParams);
106
- return originalStaticWarn.call(common_1.Logger, message, ...optionalParams);
107
- };
108
- common_1.Logger.debug = function (message, ...optionalParams) {
109
- recordNestLog('debug', message, optionalParams);
110
- return originalStaticDebug.call(common_1.Logger, message, ...optionalParams);
111
- };
112
- common_1.Logger.verbose = function (message, ...optionalParams) {
113
- recordNestLog('verbose', message, optionalParams);
114
- return originalStaticVerbose.call(common_1.Logger, message, ...optionalParams);
115
- };
116
- }
117
- catch {
118
- // Ignore patching errors to protect app stability
119
- }
120
78
  }
121
79
  };
122
80
  exports.LogWatcher = LogWatcher;
@@ -124,3 +82,20 @@ exports.LogWatcher = LogWatcher = __decorate([
124
82
  (0, common_1.Injectable)(),
125
83
  __metadata("design:paramtypes", [telescope_service_1.TelescopeService])
126
84
  ], LogWatcher);
85
+ function parseConsoleLog(rawMessage) {
86
+ // Strip ANSI escape codes
87
+ const clean = rawMessage.replace(/\x1B\[[0-9;]*[a-zA-Z]/g, '').trim();
88
+ if (!clean)
89
+ return null;
90
+ // Pattern for NestJS log: [Nest] pid - date LEVEL [Context] Message
91
+ // Example: [Nest] 1069241 - 06/08/2026, 4:23:32 PM LOG [ProviderController] Executing topup...
92
+ const nestRegex = /^\[Nest\]\s+\d+\s+-\s+\d{1,2}\/\d{1,2}\/\d{4},\s+\d{1,2}:\d{2}:\d{2}\s+(AM|PM)\s+([A-Z]+)\s+(.*)/i;
93
+ const match = clean.match(nestRegex);
94
+ if (match) {
95
+ const level = match[2].toLowerCase(); // e.g. 'log', 'debug', etc.
96
+ const message = match[3];
97
+ return { level, message };
98
+ }
99
+ // Fallback for other standard formats, or just log
100
+ return { level: 'log', message: clean };
101
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ngn-net/nestjs-telescope",
3
- "version": "0.2.11",
3
+ "version": "0.2.13",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },