-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
mongo_logger.ts
130 lines (105 loc) · 3.81 KB
/
mongo_logger.ts
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import * as fs from 'fs';
import type { Writable } from 'stream';
import { getUint } from './connection_string';
/** @public */
export const SeverityLevel = Object.freeze({
EMERGENCY: 'emergency',
ALERT: 'alert',
CRITICAL: 'critical',
ERROR: 'error',
WARNING: 'warn',
NOTICE: 'notice',
INFORMATIONAL: 'info',
DEBUG: 'debug',
TRACE: 'trace',
OFF: 'off'
} as const);
/** @public */
export type SeverityLevel = typeof SeverityLevel[keyof typeof SeverityLevel];
/** @returns one of SeverityLevel or null if it is not a valid SeverityLevel */
function toValidSeverity(severity?: string): SeverityLevel | null {
const validSeverities: string[] = Object.values(SeverityLevel);
const lowerSeverity = severity?.toLowerCase();
if (lowerSeverity != null && validSeverities.includes(lowerSeverity)) {
return lowerSeverity as SeverityLevel;
}
return null;
}
/** @internal */
export const LoggableComponent = Object.freeze({
COMMAND: 'command',
TOPOLOGY: 'topology',
SERVER_SELECTION: 'serverSelection',
CONNECTION: 'connection'
} as const);
/** @internal */
type LoggableComponent = typeof LoggableComponent[keyof typeof LoggableComponent];
/** @internal */
export interface LoggerMongoClientOptions {
mongodbLogPath?: string | Writable;
}
/** @public */
export interface LoggerOptions {
command: SeverityLevel;
topology: SeverityLevel;
serverSelection: SeverityLevel;
connection: SeverityLevel;
defaultSeverity: SeverityLevel;
maxDocumentLength: number;
logPath: string | Writable;
}
/**
* @internal
* TODO(andymina): add docs
*/
export class Logger {
/** @internal */
componentSeverities: Record<LoggableComponent, SeverityLevel>;
maxDocumentLength: number;
logPath: Writable;
constructor(options: LoggerOptions) {
// validate log path
if (typeof options.logPath === 'string') {
this.logPath =
options.logPath === 'stderr' || options.logPath === 'stdout'
? process[options.logPath]
: fs.createWriteStream(options.logPath, { flags: 'a+' });
} else {
this.logPath = options.logPath;
}
// extract comp severities
this.componentSeverities = options;
// fill max doc length
this.maxDocumentLength = options.maxDocumentLength;
}
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-empty-function */
emergency(component: any, message: any): void {}
alert(component: any, message: any): void {}
critical(component: any, message: any): void {}
error(component: any, message: any): void {}
warn(component: any, message: any): void {}
notice(component: any, message: any): void {}
info(component: any, message: any): void {}
debug(component: any, message: any): void {}
trace(component: any, message: any): void {}
static resolveOptions(clientOptions?: LoggerMongoClientOptions): LoggerOptions {
const defaultSeverity = toValidSeverity(process.env.MONGODB_LOG_ALL) ?? SeverityLevel.OFF;
return {
command: toValidSeverity(process.env.MONGODB_LOG_COMMAND) ?? defaultSeverity,
topology: toValidSeverity(process.env.MONGODB_LOG_TOPOLOGY) ?? defaultSeverity,
serverSelection: toValidSeverity(process.env.MONGODB_LOG_SERVER_SELECTION) ?? defaultSeverity,
connection: toValidSeverity(process.env.MONGODB_LOG_CONNECTION) ?? defaultSeverity,
defaultSeverity,
maxDocumentLength:
typeof process.env.MONGODB_LOG_MAX_DOCUMENT_LENGTH === 'string' &&
process.env.MONGODB_LOG_MAX_DOCUMENT_LENGTH !== ''
? getUint('MONGODB_LOG_MAX_DOCUMENT_LENGTH', process.env.MONGODB_LOG_MAX_DOCUMENT_LENGTH)
: 1000,
logPath:
typeof process.env.MONGODB_LOG_PATH === 'string' && process.env.MONGODB_LOG_PATH !== ''
? process.env.MONGODB_LOG_PATH
: clientOptions?.mongodbLogPath ?? 'stderr'
};
}
}