@ngn-net/nestjs-telescope 0.2.11 → 0.2.12

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.12",
4
+ "builtAt": "2026-06-08T12:55:28.493Z"
5
5
  }
@@ -2,7 +2,6 @@ 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;
8
7
  }
@@ -16,107 +16,61 @@ 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;
22
21
  }
23
22
  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);
28
23
  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)
24
+ const originalStdoutWrite = process.stdout.write;
25
+ const originalStderrWrite = process.stderr.write;
26
+ const handleWrite = (levelDefault, chunk, encoding) => {
27
+ // If telescope is currently recording, bypass to avoid infinite recursion loops
28
+ if (self.telescope.isRecording()) {
32
29
  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')) {
30
+ }
31
+ // Decode buffer/string
32
+ let rawMessage = '';
33
+ if (typeof chunk === 'string') {
34
+ rawMessage = chunk;
35
+ }
36
+ else if (Buffer.isBuffer(chunk)) {
37
+ rawMessage = chunk.toString(encoding || 'utf8');
38
+ }
39
+ // Skip telescope internal queries or module logs
40
+ if (rawMessage.includes('telescope_entries') || rawMessage.includes('TelescopeModule')) {
36
41
  return;
37
42
  }
38
- self.isRecording = true;
39
- try {
43
+ // Parse the log message
44
+ const parsed = parseConsoleLog(rawMessage);
45
+ if (parsed) {
46
+ // If NestJS parsed level exists, use it, otherwise use levelDefault (e.g. 'error' for stderr)
47
+ const level = parsed.level === 'log' && levelDefault === 'error' ? 'error' : parsed.level;
40
48
  self.telescope.record({
41
49
  type: entry_type_enum_1.EntryType.LOG,
42
- content: { level, message },
50
+ content: { level, message: parsed.message },
43
51
  }).catch(() => { });
44
52
  }
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
53
  };
58
- console.warn = function (...args) {
59
- recordConsoleLog('warn', args);
60
- return originalWarn(...args);
54
+ // Override process.stdout.write
55
+ process.stdout.write = function (chunk, encoding, callback) {
56
+ try {
57
+ handleWrite('log', chunk, encoding);
58
+ }
59
+ catch {
60
+ // Safe guard
61
+ }
62
+ return originalStdoutWrite.call(process.stdout, chunk, encoding, callback);
61
63
  };
62
- console.debug = function (...args) {
63
- recordConsoleLog('debug', args);
64
- return originalDebug(...args);
64
+ // Override process.stderr.write
65
+ process.stderr.write = function (chunk, encoding, callback) {
66
+ try {
67
+ handleWrite('error', chunk, encoding);
68
+ }
69
+ catch {
70
+ // Safe guard
71
+ }
72
+ return originalStderrWrite.call(process.stderr, chunk, encoding, callback);
65
73
  };
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
74
  }
121
75
  };
122
76
  exports.LogWatcher = LogWatcher;
@@ -124,3 +78,20 @@ exports.LogWatcher = LogWatcher = __decorate([
124
78
  (0, common_1.Injectable)(),
125
79
  __metadata("design:paramtypes", [telescope_service_1.TelescopeService])
126
80
  ], LogWatcher);
81
+ function parseConsoleLog(rawMessage) {
82
+ // Strip ANSI escape codes
83
+ const clean = rawMessage.replace(/\x1B\[[0-9;]*[a-zA-Z]/g, '').trim();
84
+ if (!clean)
85
+ return null;
86
+ // Pattern for NestJS log: [Nest] pid - date LEVEL [Context] Message
87
+ // Example: [Nest] 1069241 - 06/08/2026, 4:23:32 PM LOG [ProviderController] Executing topup...
88
+ 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;
89
+ const match = clean.match(nestRegex);
90
+ if (match) {
91
+ const level = match[2].toLowerCase(); // e.g. 'log', 'debug', etc.
92
+ const message = match[3];
93
+ return { level, message };
94
+ }
95
+ // Fallback for other standard formats, or just log
96
+ return { level: 'log', message: clean };
97
+ }
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.12",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },