@ngn-net/nestjs-telescope 0.2.10 → 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.10",
4
- "builtAt": "2026-06-08T12:50:24.624Z"
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
  }
@@ -14,127 +14,63 @@ exports.LogWatcher = void 0;
14
14
  const common_1 = require("@nestjs/common");
15
15
  const telescope_service_1 = require("../telescope.service");
16
16
  const entry_type_enum_1 = require("../enums/entry-type.enum");
17
- class TelescopeLoggerWrapper {
18
- original;
19
- recordLog;
20
- constructor(original, recordLog) {
21
- this.original = original;
22
- this.recordLog = recordLog;
23
- }
24
- log(message, ...optionalParams) {
25
- this.recordLog('log', message, ...optionalParams);
26
- this.original.log(message, ...optionalParams);
27
- }
28
- error(message, ...optionalParams) {
29
- this.recordLog('error', message, ...optionalParams);
30
- this.original.error(message, ...optionalParams);
31
- }
32
- warn(message, ...optionalParams) {
33
- this.recordLog('warn', message, ...optionalParams);
34
- this.original.warn(message, ...optionalParams);
35
- }
36
- debug(message, ...optionalParams) {
37
- this.recordLog('debug', message, ...optionalParams);
38
- if (this.original.debug) {
39
- this.original.debug(message, ...optionalParams);
40
- }
41
- }
42
- verbose(message, ...optionalParams) {
43
- this.recordLog('verbose', message, ...optionalParams);
44
- if (this.original.verbose) {
45
- this.original.verbose(message, ...optionalParams);
46
- }
47
- }
48
- fatal(message, ...optionalParams) {
49
- this.recordLog('fatal', message, ...optionalParams);
50
- if (this.original.fatal) {
51
- this.original.fatal(message, ...optionalParams);
52
- }
53
- }
54
- }
55
17
  let LogWatcher = class LogWatcher {
56
18
  telescope;
57
- isRecording = false; // Guard against infinite recursion
58
19
  constructor(telescope) {
59
20
  this.telescope = telescope;
60
21
  }
61
22
  onApplicationBootstrap() {
62
- const originalLog = console.log.bind(console);
63
- const originalError = console.error.bind(console);
64
- const originalWarn = console.warn.bind(console);
65
- const originalDebug = console.debug.bind(console);
66
23
  const self = this;
67
- const formatArgs = (args) => args.map((a) => (typeof a === 'object' ? JSON.stringify(a) : String(a))).join(' ');
68
- const recordConsoleLog = (level, args) => {
69
- 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()) {
70
29
  return;
71
- const message = formatArgs(args);
72
- // Skip telescope queries, logs, or dashboard routes to prevent recursion
73
- 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')) {
74
41
  return;
75
42
  }
76
- self.isRecording = true;
77
- 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;
78
48
  self.telescope.record({
79
49
  type: entry_type_enum_1.EntryType.LOG,
80
- content: { level, message },
50
+ content: { level, message: parsed.message },
81
51
  }).catch(() => { });
82
52
  }
83
- finally {
84
- self.isRecording = false;
85
- }
86
- };
87
- // 1. Intercept raw console logs
88
- console.log = function (...args) {
89
- recordConsoleLog('log', args);
90
- return originalLog(...args);
91
- };
92
- console.error = function (...args) {
93
- recordConsoleLog('error', args);
94
- return originalError(...args);
95
53
  };
96
- console.warn = function (...args) {
97
- recordConsoleLog('warn', args);
98
- return originalWarn(...args);
99
- };
100
- console.debug = function (...args) {
101
- recordConsoleLog('debug', args);
102
- return originalDebug(...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);
103
63
  };
104
- // 2. Intercept NestJS Logger Service logs
105
- try {
106
- const activeLogger = common_1.Logger.logger;
107
- if (activeLogger && !(activeLogger instanceof TelescopeLoggerWrapper)) {
108
- const wrapper = new TelescopeLoggerWrapper(activeLogger, (level, message, ...optionalParams) => {
109
- if (self.isRecording)
110
- return;
111
- let formattedMessage = typeof message === 'object' ? JSON.stringify(message) : String(message);
112
- if (optionalParams.length > 0) {
113
- // Get context (usually the last parameter, e.g. [PurchaseService])
114
- const context = optionalParams[optionalParams.length - 1];
115
- const contextStr = typeof context === 'string' ? `[${context}] ` : '';
116
- formattedMessage = contextStr + formattedMessage;
117
- }
118
- if (formattedMessage.includes('telescope_entries') || formattedMessage.includes('TelescopeModule')) {
119
- return;
120
- }
121
- self.isRecording = true;
122
- try {
123
- self.telescope.record({
124
- type: entry_type_enum_1.EntryType.LOG,
125
- content: { level, message: formattedMessage },
126
- }).catch(() => { });
127
- }
128
- finally {
129
- self.isRecording = false;
130
- }
131
- });
132
- common_1.Logger.overrideLogger(wrapper);
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
133
71
  }
134
- }
135
- catch {
136
- // Ignore errors if overrideLogger doesn't exist or fails
137
- }
72
+ return originalStderrWrite.call(process.stderr, chunk, encoding, callback);
73
+ };
138
74
  }
139
75
  };
140
76
  exports.LogWatcher = LogWatcher;
@@ -142,3 +78,20 @@ exports.LogWatcher = LogWatcher = __decorate([
142
78
  (0, common_1.Injectable)(),
143
79
  __metadata("design:paramtypes", [telescope_service_1.TelescopeService])
144
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.10",
3
+ "version": "0.2.12",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },