From d0975097b4f96d19ea5fe924617318c372e8cabe Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Wed, 14 Sep 2022 15:16:59 +0100 Subject: [PATCH] Hopefully final tweaks --- src/components/encrypted-intent.ts | 6 ++-- src/components/encryption.ts | 5 ++-- src/components/intent.ts | 3 +- src/components/logging.ts | 10 ++++--- src/provisioning/api.ts | 4 +-- src/provisioning/request.ts | 9 ++---- src/utils/matrix-host-resolver.ts | 4 +-- yarn.lock | 47 ++---------------------------- 8 files changed, 20 insertions(+), 68 deletions(-) diff --git a/src/components/encrypted-intent.ts b/src/components/encrypted-intent.ts index d6a21a3a..c687ed18 100644 --- a/src/components/encrypted-intent.ts +++ b/src/components/encrypted-intent.ts @@ -1,12 +1,10 @@ -import { UserMembership } from "./membership-cache"; import { APPSERVICE_LOGIN_TYPE, ClientEncryptionSession } from "./encryption"; -import Logging from "./logging"; -import { ReadStream } from "fs"; +import { Logger } from ".."; import BotSdk, { MatrixClient } from "matrix-bot-sdk"; import { FileUploadOpts, Intent, IntentOpts } from "./intent"; import { WeakStateEvent } from "./event-types"; -const log = Logging.get("EncryptedIntent"); +const log = new Logger("EncryptedIntent"); export interface EncryptedIntentOpts { sessionPromise: Promise; diff --git a/src/components/encryption.ts b/src/components/encryption.ts index e2d8ec62..4e8e0443 100644 --- a/src/components/encryption.ts +++ b/src/components/encryption.ts @@ -2,15 +2,14 @@ import { MembershipCache } from "./membership-cache"; import { AppServiceBot } from "./app-service-bot"; import { WeakEvent } from "./event-types"; import { Intent } from "./intent"; -import Logging from "./logging"; +import { Logger } from ".."; import { MatrixClient } from "matrix-bot-sdk"; import LRU from "@alloc/quick-lru" export const APPSERVICE_LOGIN_TYPE = "m.login.application_service"; const EVENT_CACHE_FOR_MS = 5 * 60000; // 5 minutes -export const APPSERVICE_LOGIN_TYPE = "m.login.application_service"; -const EVENT_CACHE_FOR_MS = 5 * 60000; // 5 minutes +const log = new Logger('EncryptedEventBroker'); interface PantalaimonWeakEvent extends WeakEvent { decrypted: true; diff --git a/src/components/intent.ts b/src/components/intent.ts index 72a8e1d0..084b3d7c 100644 --- a/src/components/intent.ts +++ b/src/components/intent.ts @@ -19,10 +19,9 @@ import { defer } from "../utils/promiseutil"; import { UserMembership } from "./membership-cache"; import { unstable } from "../errors"; import BridgeErrorReason = unstable.BridgeErrorReason; -import Logging from "./logging"; -import { ReadStream } from "fs"; import BotSdk, { MatrixClient, MatrixProfileInfo, PresenceState } from "matrix-bot-sdk"; import { WeakStateEvent } from "./event-types"; +import { Logger } from '..'; const log = new Logger("Intent"); export type IntentBackingStore = { diff --git a/src/components/logging.ts b/src/components/logging.ts index 3b77cd06..a4b3296d 100644 --- a/src/components/logging.ts +++ b/src/components/logging.ts @@ -155,12 +155,14 @@ export class GlobalLogger { if (cfg.json) { const formatter = format((info) => { - const hsData = [...(info as LogEntry).data]; + const logEntry = info as LogEntry; + const hsData = [...logEntry.data]; const firstArg = hsData.shift() ?? 'undefined'; const result: winston.Logform.TransformableInfo = { - level: info.level, - module: info.module, - timestamp: info.timestamp, + level: logEntry.level, + module: logEntry.module, + timestamp: logEntry.timestamp, + requestId: logEntry.requestId, // Find the first instance of an error, subsequent errors are treated as args. error: hsData.find(d => d instanceof Error)?.message, message: "", // Always filled out diff --git a/src/provisioning/api.ts b/src/provisioning/api.ts index 11855b2d..81cf415c 100644 --- a/src/provisioning/api.ts +++ b/src/provisioning/api.ts @@ -3,7 +3,6 @@ import { ProvisioningStore } from "./store"; import { Server } from "http"; import { v4 as uuid } from "uuid"; import axios from "axios"; -import Logs from "../components/logging"; import { ErrCode, IApiError, ProvisioningRequest, ApiError } from "."; import { URL } from "url"; import { MatrixHostResolver } from "../utils/matrix-host-resolver"; @@ -12,6 +11,7 @@ import { isIP } from "net"; import { promises as dns } from "dns"; import ratelimiter, { RateLimitInfo, Options as RatelimitOptions, AugmentedRequest } from "express-rate-limit"; import { Methods } from "./request"; +import { Logger } from ".."; // Borrowed from // https://github.com/matrix-org/synapse/blob/91221b696156e9f1f9deecd425ae58af03ebb5d3/docs/sample_config.yaml#L215 @@ -37,7 +37,7 @@ export const DefaultDisallowedIpRanges = [ 'fec0::/10' ] -const log = Logs.get("ProvisioningApi"); +const log = new Logger("ProvisioningApi"); interface ExpRequestProvisioner extends Request { matrixWidgetToken?: string; diff --git a/src/provisioning/request.ts b/src/provisioning/request.ts index 778bcde5..acc13fb8 100644 --- a/src/provisioning/request.ts +++ b/src/provisioning/request.ts @@ -1,6 +1,5 @@ -import Logging, { LogWrapper } from "../components/logging"; import crypto from "crypto"; -import { ThinRequest } from ".."; +import { ThinRequest, Logger } from ".."; import { Request } from "express"; import { ParsedQs } from "qs"; @@ -41,7 +40,7 @@ export class ProvisioningRequest< // eslint-disable-next-line @typescript-eslint/no-explicit-any ReqBody = any, ReqQuery = ParsedQs> implements ThinRequest { - public readonly log: LogWrapper; + public readonly log: Logger; public readonly id: string; constructor( @@ -53,9 +52,7 @@ export class ProvisioningRequest< ) { this.id = crypto.randomBytes(4).toString('hex'); this.fnName = fnName || expressReq.path; - this.log = Logging.get( - `ProvisionRequest ${[this.id, fnName].filter(n => !!n).join(" ")}` - ); + this.log = new Logger('ProvisionRequest', { requestId: [this.id, fnName].filter(n => !!n).join(" ") }); this.log.debug(`Request ${userId} (${requestSource}) ${this.fnName}`); } diff --git a/src/utils/matrix-host-resolver.ts b/src/utils/matrix-host-resolver.ts index cb7333a7..57c29e2f 100644 --- a/src/utils/matrix-host-resolver.ts +++ b/src/utils/matrix-host-resolver.ts @@ -2,7 +2,7 @@ import { Axios } from "axios"; import { URL } from "url"; import { isIP } from "net"; import { promises as dns, SrvRecord } from "dns" -import Logging from "../components/logging"; +import { Logger } from ".."; interface MatrixServerWellKnown { "m.server": string; @@ -19,7 +19,7 @@ const DefaultMatrixServerPort = 8448; const MaxPortNumber = 65535; const WellKnownTimeout = 10000; -const log = Logging.get('MatrixHostResolver'); +const log = new Logger('MatrixHostResolver'); type CachedResult = {timestamp: number, result: HostResolveResult}|{timestamp: number, error: Error}; diff --git a/yarn.lock b/yarn.lock index fe9d78d3..c9cd71f3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -459,7 +459,7 @@ resolved "https://registry.yarnpkg.com/@types/serve-static/-/serve-static-1.13.10.tgz#f5e0ce8797d2d7cc5ebeda48a52c96c4fa47a8d9" integrity sha512-nCkHGI4w7ZgAdNkrEu0bv+4xNV/XDqW+DydknebMOQwkpDGx8G+HTlj7R7ABI8i8nKxVw0wtKPi1D+lPOkh4YQ== dependencies: - "@types/mime" "*" + "@types/mime" "^1" "@types/node" "*" "@types/uuid@^8.3.1": @@ -1236,6 +1236,7 @@ eslint@^8.1.5: strip-ansi "^6.0.1" strip-json-comments "^3.1.0" text-table "^0.2.0" + v8-compile-cache "^2.0.3" espree@^9.3.2: version "9.3.2" @@ -1436,14 +1437,6 @@ find-up@^4.0.0, find-up@^4.1.0: locate-path "^5.0.0" path-exists "^4.0.0" -find-up@^5.0.0: - version "5.0.0" - resolved "https://registry.yarnpkg.com/find-up/-/find-up-5.0.0.tgz#4c92819ecb7083561e4f4a240a86be5198f536fc" - integrity sha512-78/PXT1wlLLDgTzDs7sjq9hzz0vXD+zn+7wypEe4fXQxCmdmqfGsEPQxmiCSQI3ajFV91bVSsvNtrJRiW6nGng== - dependencies: - locate-path "^6.0.0" - path-exists "^4.0.0" - flat-cache@^3.0.4: version "3.0.4" resolved "https://registry.yarnpkg.com/flat-cache/-/flat-cache-3.0.4.tgz#61b0338302b2fe9f957dcc32fc2a87f1c3048b11" @@ -1604,17 +1597,6 @@ glob@^7.1.3, glob@^7.1.4, glob@^7.1.6: once "^1.3.0" path-is-absolute "^1.0.0" -glob@^8.0.3: - version "8.0.3" - resolved "https://registry.yarnpkg.com/glob/-/glob-8.0.3.tgz#415c6eb2deed9e502c68fa44a272e6da6eeca42e" - integrity sha512-ull455NHSHI/Y1FqGaaYFaLGkNMMJbavMrEGFXG/PGrg6y7sutWHUHrz6gy6WEBH6akM1M414dWKCNs+IhKdiQ== - dependencies: - fs.realpath "^1.0.0" - inflight "^1.0.4" - inherits "2" - minimatch "^5.0.1" - once "^1.3.0" - globals@^11.1.0: version "11.12.0" resolved "https://registry.yarnpkg.com/globals/-/globals-11.12.0.tgz#ab8795338868a0babd8525758018c2a7eb95c42e" @@ -2485,13 +2467,6 @@ p-limit@^2.2.0: dependencies: p-try "^2.0.0" -p-limit@^3.0.2: - version "3.1.0" - resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-3.1.0.tgz#e1daccbe78d0d1388ca18c64fea38e3e57e3706b" - integrity sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ== - dependencies: - yocto-queue "^0.1.0" - p-locate@^4.1.0: version "4.1.0" resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-4.1.0.tgz#a3428bb7088b3a60292f66919278b7c297ad4f07" @@ -2499,13 +2474,6 @@ p-locate@^4.1.0: dependencies: p-limit "^2.2.0" -p-locate@^5.0.0: - version "5.0.0" - resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-5.0.0.tgz#83c8315c6785005e3bd021839411c9e110e6d834" - integrity sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw== - dependencies: - p-limit "^3.0.2" - p-map@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/p-map/-/p-map-3.0.0.tgz#d704d9af8a2ba684e2600d9a215983d4141a979d" @@ -3268,11 +3236,6 @@ v8-compile-cache-lib@^3.0.1: resolved "https://registry.yarnpkg.com/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz#6336e8d71965cb3d35a1bbb7868445a7c05264bf" integrity sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg== -v8-compile-cache@^2.0.3: - version "2.3.0" - resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.3.0.tgz#2de19618c66dc247dcfb6f99338035d8245a2cee" - integrity sha512-l8lCEmLcLYZh4nbunNZvQCJc5pv7+RCwa8q/LdUx8u7lsWvPDKmpodJAJNwkAhJC//dFY48KuIEmjtd4RViDrA== - vary@~1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc" @@ -3333,7 +3296,6 @@ winston@^3.3.3: resolved "https://registry.yarnpkg.com/winston/-/winston-3.8.1.tgz#76f15b3478cde170b780234e0c4cf805c5a7fb57" integrity sha512-r+6YAiCR4uI3N8eQNOg8k3P3PqwAm20cLKlzVD9E66Ch39+LZC+VH1UKf9JemQj2B3QoUHfKD7Poewn0Pr3Y1w== dependencies: - "@colors/colors" "1.5.0" "@dabh/diagnostics" "^2.0.2" async "^3.2.3" is-stream "^2.0.0" @@ -3418,8 +3380,3 @@ yn@3.1.1: version "3.1.1" resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50" integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q== - -yocto-queue@^0.1.0: - version "0.1.0" - resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b" - integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==