From 365a28cc555e1eb8b16fe2c2a185c0df413966d0 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 16 Sep 2021 17:50:33 +0200 Subject: [PATCH] chore: split setup-logger-and-telemetry into separate package COMPASS-5035 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is done so that the Compass shell can also use the same code to hook its own logging to the shell’s message bus. --- packages/cli-repl/package-lock.json | 11 +- packages/cli-repl/package.json | 2 +- packages/cli-repl/src/cli-repl.ts | 20 +- packages/logging/.eslintignore | 3 + packages/logging/.eslintrc.js | 1 + packages/logging/.gitignore | 1 + packages/logging/AUTHORS | 1 + packages/logging/LICENSE | 202 ++++++++++++++++++ packages/logging/README.md | 11 + packages/logging/package.json | 37 ++++ packages/logging/src/index.ts | 1 + .../src/setup-logger-and-telemetry.spec.ts | 48 ++--- .../src/setup-logger-and-telemetry.ts | 29 +-- packages/logging/tsconfig.json | 13 ++ packages/logging/tsconfig.lint.json | 8 + 15 files changed, 328 insertions(+), 60 deletions(-) create mode 100644 packages/logging/.eslintignore create mode 100644 packages/logging/.eslintrc.js create mode 100644 packages/logging/.gitignore create mode 100644 packages/logging/AUTHORS create mode 100644 packages/logging/LICENSE create mode 100644 packages/logging/README.md create mode 100644 packages/logging/package.json create mode 100644 packages/logging/src/index.ts rename packages/{cli-repl => logging}/src/setup-logger-and-telemetry.spec.ts (93%) rename packages/{cli-repl => logging}/src/setup-logger-and-telemetry.ts (95%) create mode 100644 packages/logging/tsconfig.json create mode 100644 packages/logging/tsconfig.lint.json diff --git a/packages/cli-repl/package-lock.json b/packages/cli-repl/package-lock.json index 32c0ba2b94..b26290fdb4 100644 --- a/packages/cli-repl/package-lock.json +++ b/packages/cli-repl/package-lock.json @@ -828,7 +828,8 @@ "lodash": { "version": "4.17.21", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", - "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==" + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", + "dev": true }, "lodash.isstring": { "version": "4.0.1", @@ -912,14 +913,6 @@ "bson": "^4.5.1" } }, - "mongodb-redact": { - "version": "0.2.2", - "resolved": "https://registry.npmjs.org/mongodb-redact/-/mongodb-redact-0.2.2.tgz", - "integrity": "sha512-tmgDpSBymFtKggsLzpa0vDYaqh2wEXOswBZtJkXvbPKP0ThfPwoFYXtOukactU6WZsC4RYmpSPM4P6582FR/Xw==", - "requires": { - "lodash": "^4.17.15" - } - }, "ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", diff --git a/packages/cli-repl/package.json b/packages/cli-repl/package.json index b0db433e5d..b53fad6288 100644 --- a/packages/cli-repl/package.json +++ b/packages/cli-repl/package.json @@ -44,6 +44,7 @@ "@mongosh/errors": "0.0.0-dev.0", "@mongosh/history": "0.0.0-dev.0", "@mongosh/i18n": "0.0.0-dev.0", + "@mongosh/logging": "0.0.0-dev.0", "@mongosh/service-provider-core": "0.0.0-dev.0", "@mongosh/service-provider-server": "0.0.0-dev.0", "@mongosh/shell-api": "0.0.0-dev.0", @@ -57,7 +58,6 @@ "is-recoverable-error": "^1.0.2", "lodash.set": "^4.3.2", "mongodb-connection-string-url": "^2.0.0", - "mongodb-redact": "^0.2.2", "mongodb-log-writer": "^1.0.3", "nanobus": "^4.4.0", "pretty-bytes": "^5.3.0", diff --git a/packages/cli-repl/src/cli-repl.ts b/packages/cli-repl/src/cli-repl.ts index 7a36b98b2d..a12128be72 100644 --- a/packages/cli-repl/src/cli-repl.ts +++ b/packages/cli-repl/src/cli-repl.ts @@ -10,13 +10,14 @@ import ConnectionString from 'mongodb-connection-string-url'; import Nanobus from 'nanobus'; import semver from 'semver'; import { Readable, Writable } from 'stream'; +import { buildInfo } from './build-info'; import type { StyleDefinition } from './clr'; import { ConfigManager, ShellHomeDirectory, ShellHomePaths } from './config-directory'; import { CliReplErrors } from './error-codes'; -import { MongoLogManager, MongoLogWriter } from 'mongodb-log-writer'; +import { MongoLogManager, MongoLogWriter, mongoLogId } from 'mongodb-log-writer'; import { MongocryptdManager } from './mongocryptd-manager'; import MongoshNodeRepl, { MongoshNodeReplOptions } from './mongosh-repl'; -import setupLoggerAndTelemetry from './setup-logger-and-telemetry'; +import { setupLoggerAndTelemetry } from '@mongosh/logging'; import { MongoshBus, CliUserConfig } from '@mongosh/types'; import { promises as fs } from 'fs'; import path from 'path'; @@ -177,10 +178,14 @@ class CliRepl { } this.logWriter = logger; + logger.info('MONGOSH', mongoLogId(1_000_000_000), 'log', 'Starting log', { + execPath: process.execPath, + ...buildInfo() + }); + setupLoggerAndTelemetry( - logger.logId, this.bus, - () => logger, + logger, () => { if (process.env.IS_MONGOSH_EVERGREEN_CI && !this.analyticsOptions?.alwaysEnable) { // This error will be in the log file, but otherwise not visible to users @@ -191,7 +196,12 @@ class CliRepl { this.analyticsOptions?.apiKey ?? require('./build-info.json').segmentApiKey, this.analyticsOptions); return this.analytics; - }); + }, + { + platform: process.platform, + arch: process.arch + }, + require('../package.json').version); try { this.config = await this.configDirectory.generateOrReadConfig(this.config); diff --git a/packages/logging/.eslintignore b/packages/logging/.eslintignore new file mode 100644 index 0000000000..56cd90e6cd --- /dev/null +++ b/packages/logging/.eslintignore @@ -0,0 +1,3 @@ +node_modules +local-test.js +lib/ diff --git a/packages/logging/.eslintrc.js b/packages/logging/.eslintrc.js new file mode 100644 index 0000000000..4a958b44bd --- /dev/null +++ b/packages/logging/.eslintrc.js @@ -0,0 +1 @@ +module.exports = require('../../config/eslintrc.base'); diff --git a/packages/logging/.gitignore b/packages/logging/.gitignore new file mode 100644 index 0000000000..c3af857904 --- /dev/null +++ b/packages/logging/.gitignore @@ -0,0 +1 @@ +lib/ diff --git a/packages/logging/AUTHORS b/packages/logging/AUTHORS new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/packages/logging/AUTHORS @@ -0,0 +1 @@ + diff --git a/packages/logging/LICENSE b/packages/logging/LICENSE new file mode 100644 index 0000000000..d645695673 --- /dev/null +++ b/packages/logging/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/packages/logging/README.md b/packages/logging/README.md new file mode 100644 index 0000000000..438941ffa6 --- /dev/null +++ b/packages/logging/README.md @@ -0,0 +1,11 @@ +# `@mongosh/logging` + +Package for [MongoDB Shell](mongosh). + +### Installation +```shell +npm install -S @mongosh/logging +``` + +[mongosh]: https://github.com/mongodb-js/mongosh + diff --git a/packages/logging/package.json b/packages/logging/package.json new file mode 100644 index 0000000000..931f0de5fd --- /dev/null +++ b/packages/logging/package.json @@ -0,0 +1,37 @@ +{ + "name": "@mongosh/logging", + "version": "0.0.0-dev.0", + "description": "MongoDB Shell Logging Utilities Package", + "homepage": "https://github.com/mongodb-js/mongosh", + "license": "Apache-2.0", + "main": "lib/index.js", + "types": "lib/index.d.ts", + "publishConfig": { + "access": "public" + }, + "repository": { + "type": "git", + "url": "https://github.com/mongodb-js/mongosh" + }, + "engines": { + "node": ">=12.4.0" + }, + "dependencies": { + "@mongosh/errors": "0.0.0-dev.0", + "@mongosh/history": "0.0.0-dev.0", + "@mongosh/types": "0.0.0-dev.0", + "mongodb-log-writer": "^1.0.3", + "mongodb-redact": "^0.2.2" + }, + "scripts": { + "test": "mocha -r \"../../scripts/import-expansions.js\" --timeout 15000 -r ts-node/register \"./src/**/*.spec.ts\"", + "test-ci": "node ../../scripts/run-if-package-requested.js npm test", + "lint": "eslint --report-unused-disable-directives \"./{src,test}/**/*.{js,ts,tsx}\"", + "check": "npm run lint", + "compile-ts": "tsc -p tsconfig.json", + "prepublish": "npm run compile-ts" + }, + "mongosh": { + "unitTestsOnly": true + } +} diff --git a/packages/logging/src/index.ts b/packages/logging/src/index.ts new file mode 100644 index 0000000000..06af3ac597 --- /dev/null +++ b/packages/logging/src/index.ts @@ -0,0 +1 @@ +export { MongoshAnalytics, setupLoggerAndTelemetry } from './setup-logger-and-telemetry'; diff --git a/packages/cli-repl/src/setup-logger-and-telemetry.spec.ts b/packages/logging/src/setup-logger-and-telemetry.spec.ts similarity index 93% rename from packages/cli-repl/src/setup-logger-and-telemetry.spec.ts rename to packages/logging/src/setup-logger-and-telemetry.spec.ts index a53d845a85..44e56902ce 100644 --- a/packages/cli-repl/src/setup-logger-and-telemetry.spec.ts +++ b/packages/logging/src/setup-logger-and-telemetry.spec.ts @@ -1,7 +1,6 @@ -/* eslint-disable camelcase */ import { expect } from 'chai'; import { MongoLogWriter } from 'mongodb-log-writer'; -import setupLoggerAndTelemetry from './setup-logger-and-telemetry'; +import { setupLoggerAndTelemetry } from './'; import { EventEmitter } from 'events'; import { MongoshInvalidInputError } from '@mongosh/errors'; import { MongoshBus } from '@mongosh/types'; @@ -30,8 +29,11 @@ describe('setupLoggerAndTelemetry', () => { }); it('works', () => { - setupLoggerAndTelemetry(logId, bus, () => logger, () => analytics); - expect(logOutput).to.have.lengthOf(1); + setupLoggerAndTelemetry(bus, logger, () => analytics, { + platform: process.platform, + arch: process.arch + }, '1.0.0'); + expect(logOutput).to.have.lengthOf(0); expect(analyticsOutput).to.be.empty; bus.emit('mongosh:new-user', userId, false); @@ -95,8 +97,6 @@ describe('setupLoggerAndTelemetry', () => { bus.emit('mongosh-sp:missing-optional-dependency', { name: 'kerberos', error: new Error('no kerberos') }); let i = 0; - expect(logOutput[i].msg).to.equal('Starting log'); - expect(logOutput[i++].attr).to.include.keys('execPath', 'version', 'distributionKind'); expect(logOutput[i].msg).to.equal('User updated'); expect(logOutput[i++].attr).to.deep.equal({ enableTelemetry: false }); expect(logOutput[i].msg).to.equal('Connecting to server'); @@ -184,7 +184,6 @@ describe('setupLoggerAndTelemetry', () => { expect(logOutput[i++].attr).to.deep.equal({ name: 'kerberos', error: 'no kerberos' }); expect(i).to.equal(logOutput.length); - const mongosh_version = require('../package.json').version; expect(analyticsOutput).to.deep.equal([ [ 'identify', @@ -212,7 +211,7 @@ describe('setupLoggerAndTelemetry', () => { userId: '53defe995fa47e6c13102d9d', event: 'New Connection', properties: { - mongosh_version, + mongosh_version: '1.0.0', session_id: '5fb3c20ee1507e894e5340f3', is_localhost: true, is_atlas: false, @@ -226,7 +225,7 @@ describe('setupLoggerAndTelemetry', () => { userId: '53defe995fa47e6c13102d9d', event: 'Error', properties: { - mongosh_version, + mongosh_version: '1.0.0', name: 'MongoshInvalidInputError', code: 'CLIREPL-1005', scope: 'CLIREPL', @@ -239,7 +238,7 @@ describe('setupLoggerAndTelemetry', () => { { userId: '53defe995fa47e6c13102d9d', event: 'Use', - properties: { mongosh_version } + properties: { mongosh_version: '1.0.0' } } ], [ @@ -248,7 +247,7 @@ describe('setupLoggerAndTelemetry', () => { userId: '53defe995fa47e6c13102d9d', event: 'Show', properties: { - mongosh_version, + mongosh_version: '1.0.0', method: 'dbs' } } @@ -258,7 +257,7 @@ describe('setupLoggerAndTelemetry', () => { { event: 'Script Loaded CLI', properties: { - mongosh_version, + mongosh_version: '1.0.0', nested: true, shell: true }, @@ -270,7 +269,7 @@ describe('setupLoggerAndTelemetry', () => { { event: 'Script Loaded', properties: { - mongosh_version, + mongosh_version: '1.0.0', nested: false }, userId: '53defe995fa47e6c13102d9d' @@ -281,7 +280,7 @@ describe('setupLoggerAndTelemetry', () => { { event: 'Mongoshrc Loaded', properties: { - mongosh_version, + mongosh_version: '1.0.0', }, userId: '53defe995fa47e6c13102d9d' } @@ -291,7 +290,7 @@ describe('setupLoggerAndTelemetry', () => { { event: 'Mongorc Warning', properties: { - mongosh_version, + mongosh_version: '1.0.0', }, userId: '53defe995fa47e6c13102d9d' } @@ -301,7 +300,7 @@ describe('setupLoggerAndTelemetry', () => { { event: 'Script Evaluated', properties: { - mongosh_version, + mongosh_version: '1.0.0', shell: true }, userId: '53defe995fa47e6c13102d9d' @@ -313,7 +312,7 @@ describe('setupLoggerAndTelemetry', () => { userId: '53defe995fa47e6c13102d9d', event: 'Snippet Install', properties: { - mongosh_version + mongosh_version: '1.0.0' } } ] @@ -321,11 +320,10 @@ describe('setupLoggerAndTelemetry', () => { }); it('buffers deprecated API calls', () => { - setupLoggerAndTelemetry(logId, bus, () => logger, () => analytics); - expect(logOutput).to.have.lengthOf(1); + setupLoggerAndTelemetry(bus, logger, () => analytics, {}, '1.0.0'); + expect(logOutput).to.have.lengthOf(0); expect(analyticsOutput).to.be.empty; - const mongosh_version = require('../package.json').version; bus.emit('mongosh:new-user', userId, true); logOutput = []; @@ -354,7 +352,7 @@ describe('setupLoggerAndTelemetry', () => { userId: '53defe995fa47e6c13102d9d', event: 'Deprecated Method', properties: { - mongosh_version, + mongosh_version: '1.0.0', class: 'Database', method: 'cloneDatabase', } @@ -366,7 +364,7 @@ describe('setupLoggerAndTelemetry', () => { userId: '53defe995fa47e6c13102d9d', event: 'Deprecated Method', properties: { - mongosh_version, + mongosh_version: '1.0.0', class: 'Database', method: 'copyDatabase', } @@ -391,10 +389,10 @@ describe('setupLoggerAndTelemetry', () => { }); it('works when analytics are not available', () => { - setupLoggerAndTelemetry('5fb3c20ee1507e894e5340f3', bus, () => logger, () => { throw new Error(); }); + setupLoggerAndTelemetry(bus, logger, () => { throw new Error(); }, {}, '1.0.0'); bus.emit('mongosh:new-user', userId, true); expect(analyticsOutput).to.be.empty; - expect(logOutput).to.have.lengthOf(2); - expect(logOutput[1].s).to.equal('E'); + expect(logOutput).to.have.lengthOf(1); + expect(logOutput[0].s).to.equal('E'); }); }); diff --git a/packages/cli-repl/src/setup-logger-and-telemetry.ts b/packages/logging/src/setup-logger-and-telemetry.ts similarity index 95% rename from packages/cli-repl/src/setup-logger-and-telemetry.ts rename to packages/logging/src/setup-logger-and-telemetry.ts index 6e3583e7f6..72f77e7d0a 100644 --- a/packages/cli-repl/src/setup-logger-and-telemetry.ts +++ b/packages/logging/src/setup-logger-and-telemetry.ts @@ -32,13 +32,12 @@ import type { SpMissingOptionalDependencyEvent } from '@mongosh/types'; import { inspect } from 'util'; -import { buildInfo } from './build-info'; import { MongoLogWriter, mongoLogId } from 'mongodb-log-writer'; /** * General interface for an Analytics provider that mongosh can use. */ -interface MongoshAnalytics { +export interface MongoshAnalytics { identify(message: { userId: string, traits: { platform: string } @@ -67,29 +66,19 @@ class NoopAnalytics implements MongoshAnalytics { /** * Connect a MongoshBus instance that emits events to logging and analytics providers. * - * @param logId The id of the current session * @param bus A MongoshBus instance - * @param makeLogger A function that returns a log file writer - * @param makeAnalytics A function that returns an analytics provider + * @param log A MongoLogWriter instance + * @param makeAnalytics A function that returns an analytics provider (or throws otherwise) */ -export default function setupLoggerAndTelemetry( - logId: string, +export function setupLoggerAndTelemetry( bus: MongoshBus, - makeLogger: () => MongoLogWriter, - makeAnalytics: () => MongoshAnalytics): void { - const log = makeLogger(); - const mongosh_version = require('../package.json').version; + log: MongoLogWriter, + makeAnalytics: () => MongoshAnalytics, + userTraits: any, + mongosh_version: string): void { + const { logId } = log; let userId: string; let telemetry: boolean; - const userTraits = { - platform: process.platform, - arch: process.arch - }; - - log.info('MONGOSH', mongoLogId(1_000_000_000), 'log', 'Starting log', { - execPath: process.execPath, - ...buildInfo() - }); let analytics: MongoshAnalytics = new NoopAnalytics(); try { diff --git a/packages/logging/tsconfig.json b/packages/logging/tsconfig.json new file mode 100644 index 0000000000..eff2d14183 --- /dev/null +++ b/packages/logging/tsconfig.json @@ -0,0 +1,13 @@ +{ + "extends": "../../config/tsconfig.base.json", + "compilerOptions": { + "outDir": "./lib", + "allowJs": true + }, + "include": [ + "./src/**/*" + ], + "exclude": [ + "./src/**/*.spec.*" + ] +} diff --git a/packages/logging/tsconfig.lint.json b/packages/logging/tsconfig.lint.json new file mode 100644 index 0000000000..e797cf64f6 --- /dev/null +++ b/packages/logging/tsconfig.lint.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.json", + "include": [ + "./src/**/*", + "./test/**/*" + ], + "exclude": [] +}