@mongosh/logging 3.0.0 → 3.4.0
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.
- package/AUTHORS +1 -0
- package/lib/analytics-helpers.d.ts +1 -1
- package/lib/helpers.d.ts +7 -0
- package/lib/helpers.js +31 -0
- package/lib/helpers.js.map +1 -0
- package/lib/index.d.ts +2 -1
- package/lib/index.js +3 -3
- package/lib/index.js.map +1 -1
- package/lib/logging-and-telemetry.d.ts +2 -0
- package/lib/logging-and-telemetry.js +429 -0
- package/lib/logging-and-telemetry.js.map +1 -0
- package/lib/types.d.ts +33 -0
- package/lib/types.js +3 -0
- package/lib/types.js.map +1 -0
- package/package.json +6 -6
- package/src/analytics-helpers.ts +1 -1
- package/src/helpers.spec.ts +26 -0
- package/src/helpers.ts +49 -0
- package/src/index.ts +2 -1
- package/src/{setup-logger-and-telemetry.spec.ts → logging-and-telemetry.spec.ts} +298 -84
- package/src/logging-and-telemetry.ts +871 -0
- package/src/types.ts +35 -0
- package/lib/setup-logger-and-telemetry.d.ts +0 -5
- package/lib/setup-logger-and-telemetry.js +0 -361
- package/lib/setup-logger-and-telemetry.js.map +0 -1
- package/src/setup-logger-and-telemetry.ts +0 -783
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { expect } from 'chai';
|
|
2
|
+
import { toSnakeCase } from './helpers';
|
|
3
|
+
|
|
4
|
+
describe('logging helpers', function () {
|
|
5
|
+
describe('toSnakeCase', function () {
|
|
6
|
+
const useCases = [
|
|
7
|
+
{ input: 'MongoDB REPL', output: 'mongo_db_repl' },
|
|
8
|
+
{
|
|
9
|
+
input: 'Node.js REPL Instantiation',
|
|
10
|
+
output: 'node_js_repl_instantiation',
|
|
11
|
+
},
|
|
12
|
+
{ input: 'A', output: 'a' },
|
|
13
|
+
{
|
|
14
|
+
input: 'OneLongThingInPascalCase',
|
|
15
|
+
output: 'one_long_thing_in_pascal_case',
|
|
16
|
+
},
|
|
17
|
+
{ input: 'Removes .Dots in Node.js', output: 'removes_dots_in_node_js' },
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
for (const { input, output } of useCases) {
|
|
21
|
+
it(`should convert ${input} to ${output}`, function () {
|
|
22
|
+
expect(toSnakeCase(input)).to.equal(output);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
});
|
package/src/helpers.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A helper class for keeping track of how often specific events occurred.
|
|
3
|
+
*/
|
|
4
|
+
export class MultiSet<T extends Record<string, unknown>> {
|
|
5
|
+
_entries: Map<string, number> = new Map();
|
|
6
|
+
|
|
7
|
+
add(entry: T): void {
|
|
8
|
+
const key = JSON.stringify(Object.entries(entry).sort());
|
|
9
|
+
this._entries.set(key, (this._entries.get(key) ?? 0) + 1);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
clear(): void {
|
|
13
|
+
this._entries.clear();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
*[Symbol.iterator](): Iterator<[T, number]> {
|
|
17
|
+
for (const [key, count] of this._entries) {
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
19
|
+
yield [Object.fromEntries(JSON.parse(key)) as T, count];
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* It transforms a random string into snake case. Snake case is completely
|
|
26
|
+
* lowercase and uses '_' to separate words. For example:
|
|
27
|
+
*
|
|
28
|
+
* This function defines a "word" as a sequence of characters until the next `.` or capital letter.
|
|
29
|
+
*
|
|
30
|
+
* 'Random String' => 'random_string'
|
|
31
|
+
*
|
|
32
|
+
* It will also remove any non alphanumeric characters to ensure the string
|
|
33
|
+
* is compatible with Segment. For example:
|
|
34
|
+
*
|
|
35
|
+
* 'Node.js REPL Instantiation' => 'node_js_repl_instantiation'
|
|
36
|
+
*
|
|
37
|
+
* @param str Any non snake-case formatted string
|
|
38
|
+
* @returns The snake-case formatted string
|
|
39
|
+
*/
|
|
40
|
+
export function toSnakeCase(str: string): string {
|
|
41
|
+
const matches = str.match(
|
|
42
|
+
/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g
|
|
43
|
+
);
|
|
44
|
+
if (!matches) {
|
|
45
|
+
return str;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return matches.map((x) => x.toLowerCase()).join('_');
|
|
49
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export { setupLoggerAndTelemetry } from './setup-logger-and-telemetry';
|
|
2
1
|
export {
|
|
3
2
|
MongoshAnalytics,
|
|
4
3
|
ToggleableAnalytics,
|
|
@@ -6,3 +5,5 @@ export {
|
|
|
6
5
|
NoopAnalytics,
|
|
7
6
|
ThrottledAnalytics,
|
|
8
7
|
} from './analytics-helpers';
|
|
8
|
+
export { MongoshLoggingAndTelemetry } from './types';
|
|
9
|
+
export { setupLoggingAndTelemetry } from './logging-and-telemetry';
|