Skip to content

Commit

Permalink
Merge pull request #280 from matrix-org/hs/js-sdk-log
Browse files Browse the repository at this point in the history
Hook into the matrix-js-sdk logger and output through winston
  • Loading branch information
Half-Shot authored Nov 20, 2020
2 parents 37bae25 + d9643bb commit 1f701e0
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 22 deletions.
1 change: 1 addition & 0 deletions changelog.d/280.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Logs from `matrix-js-sdk` will now be passed through the bridge `Logger` to keep logging in one place.
43 changes: 25 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"is-my-json-valid": "^2.20.5",
"js-yaml": "^3.14.0",
"matrix-appservice": "^0.7.1",
"matrix-js-sdk": "^8.4.1",
"matrix-js-sdk": "^9.1.0",
"nedb": "^1.8.0",
"nopt": "^4.0.3",
"p-queue": "^6.6.2",
Expand All @@ -41,7 +41,7 @@
"winston-daily-rotate-file": "^4.5.0"
},
"devDependencies": {
"@types/express": "^4.17.8",
"@types/express": "^4.17",
"@types/extend": "^3.0.1",
"@types/js-yaml": "^3.12.5",
"@types/nedb": "^1.8.10",
Expand All @@ -53,7 +53,7 @@
"jasmine": "^3.6.0",
"nyc": "^15.1.0",
"typedoc": "^0.19.0",
"typescript": "^4.0.2",
"typescript": "4.0",
"winston-transport": "^4.4.0"
}
}
5 changes: 4 additions & 1 deletion src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import * as util from "util";
import yaml from "js-yaml";
import { Application, Request as ExRequest, Response as ExResponse, NextFunction } from "express";

// eslint-disable-next-line @typescript-eslint/no-var-requires
const MatrixScheduler = require("matrix-js-sdk").MatrixScheduler;

import { AppServiceRegistration, AppService, AppServiceOutput } from "matrix-appservice";
Expand Down Expand Up @@ -1000,7 +1001,8 @@ export class Bridge {
throw Error('Cannot call getIntent before calling .run()');
}
return this.botIntent;
} else if (userId === this.botUserId) {
}
else if (userId === this.botUserId) {
if (!this.botIntent) {
// This will be defined when .run is called.
throw Error('Cannot call getIntent before calling .run()');
Expand Down Expand Up @@ -1311,6 +1313,7 @@ export class Bridge {
const content = event.content as {
membership: UserMembership;
displayname?: string;
// eslint-disable-next-line camelcase
avatar_url?: string;
};
const profile: UserProfile = {};
Expand Down
30 changes: 30 additions & 0 deletions src/components/client-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ limitations under the License.
*/

import { Request } from "./request";
import logging from "./logging";
// from the js-sdk
import loglevel from "loglevel";

type LogWrapCallback = (err: Error, response: { statusCode: number }, body: Record<string, unknown>) => void;
type OriginalRequest = (opts: Record<string, unknown>, cb: LogWrapCallback) => void;
Expand Down Expand Up @@ -116,6 +119,33 @@ export class ClientFactory {
callback(err, response, body);
});
});

// matrix-js-sdk uses the `loglevel` logging library for it's logging
// but the only way to get it to log to winston is to modify the
// global methodFactory.
loglevel.methodFactory = (methodName, _logLevel, loggerName) => {
return (...args) => {
const loggerInstance = logging.get(`js-sdk:${loggerName}`);
if (methodName === "debug" ||
methodName == "warn" || methodName === "error") {
loggerInstance[methodName](...args);
}
else {
loggerInstance.debug(...args)
}
}
}
// Set the factory on the default instance.
// eslint-disable-next-line @typescript-eslint/no-var-requires
const loggerInstance = require("matrix-js-sdk/lib/logger").logger;
loggerInstance.methodFactory = loglevel.methodFactory;
// Ensure these functions use winston
loggerInstance.info = loglevel.methodFactory("info", 1, "matrix");
loggerInstance.warn = loglevel.methodFactory("warn", 1, "matrix");
loggerInstance.debug = loglevel.methodFactory("debug", 1, "matrix");
loggerInstance.error = loglevel.methodFactory("error", 1, "matrix");
loggerInstance.trace = loglevel.methodFactory("trace", 1, "matrix");
loggerInstance.log = loglevel.methodFactory("debug", 1, "matrix");
}

/**
Expand Down

0 comments on commit 1f701e0

Please sign in to comment.