-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathlogger.ts
More file actions
33 lines (27 loc) · 876 Bytes
/
logger.ts
File metadata and controls
33 lines (27 loc) · 876 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import fs from 'node:fs';
import {debug} from './third_party/index.js';
const mcpDebugNamespace = 'mcp:log';
const namespacesToEnable = [
mcpDebugNamespace,
...(process.env['DEBUG'] ? [process.env['DEBUG']] : []),
];
export function saveLogsToFile(fileName: string): fs.WriteStream {
// Enable overrides everything so we need to add them
debug.enable(namespacesToEnable.join(','));
const logFile = fs.createWriteStream(fileName, {flags: 'a+'});
debug.log = function (...chunks: any[]) {
logFile.write(`${chunks.join(' ')}\n`);
};
logFile.on('error', function (error) {
console.error(`Error when opening/writing to log file: ${error.message}`);
logFile.end();
process.exit(1);
});
return logFile;
}
export const logger = debug(mcpDebugNamespace);