1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-29 16:43:09 +03:00

Enable prefixed loggers to chain

This commit is contained in:
J. Ryan Stinnett
2021-02-26 17:47:52 +00:00
parent 198c9a2507
commit e217bf9e37
3 changed files with 27 additions and 17 deletions

View File

@@ -16,7 +16,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import {getPrefixedLogger, logger} from '../logger'; import {logger} from '../logger';
import {IndexedDBCryptoStore} from './store/indexeddb-crypto-store'; import {IndexedDBCryptoStore} from './store/indexeddb-crypto-store';
import * as algorithms from './algorithms'; import * as algorithms from './algorithms';
@@ -545,7 +545,7 @@ OlmDevice.prototype.createOutboundSession = async function(
} }
}); });
}, },
getPrefixedLogger("[createOutboundSession]"), logger.withPrefix("[createOutboundSession]"),
); );
return newSessionId; return newSessionId;
}; };
@@ -606,7 +606,7 @@ OlmDevice.prototype.createInboundSession = async function(
} }
}); });
}, },
getPrefixedLogger("[createInboundSession]"), logger.withPrefix("[createInboundSession]"),
); );
return result; return result;
@@ -621,7 +621,7 @@ OlmDevice.prototype.createInboundSession = async function(
* @return {Promise<string[]>} a list of known session ids for the device * @return {Promise<string[]>} a list of known session ids for the device
*/ */
OlmDevice.prototype.getSessionIdsForDevice = async function(theirDeviceIdentityKey) { OlmDevice.prototype.getSessionIdsForDevice = async function(theirDeviceIdentityKey) {
const log = getPrefixedLogger("[getSessionIdsForDevice]"); const log = logger.withPrefix("[getSessionIdsForDevice]");
if (this._sessionsInProgress[theirDeviceIdentityKey]) { if (this._sessionsInProgress[theirDeviceIdentityKey]) {
log.debug(`Waiting for Olm session for ${theirDeviceIdentityKey} to be created`); log.debug(`Waiting for Olm session for ${theirDeviceIdentityKey} to be created`);
@@ -705,7 +705,7 @@ OlmDevice.prototype.getSessionIdForDevice = async function(
* @return {Array.<{sessionId: string, hasReceivedMessage: Boolean}>} * @return {Array.<{sessionId: string, hasReceivedMessage: Boolean}>}
*/ */
OlmDevice.prototype.getSessionInfoForDevice = async function(deviceIdentityKey, nowait) { OlmDevice.prototype.getSessionInfoForDevice = async function(deviceIdentityKey, nowait) {
const log = getPrefixedLogger("[getSessionInfoForDevice]"); const log = logger.withPrefix("[getSessionInfoForDevice]");
if (this._sessionsInProgress[deviceIdentityKey] && !nowait) { if (this._sessionsInProgress[deviceIdentityKey] && !nowait) {
log.debug(`Waiting for Olm session for ${deviceIdentityKey} to be created`); log.debug(`Waiting for Olm session for ${deviceIdentityKey} to be created`);
@@ -769,7 +769,7 @@ OlmDevice.prototype.encryptMessage = async function(
this._saveSession(theirDeviceIdentityKey, sessionInfo, txn); this._saveSession(theirDeviceIdentityKey, sessionInfo, txn);
}); });
}, },
getPrefixedLogger("[encryptMessage]"), logger.withPrefix("[encryptMessage]"),
); );
return res; return res;
}; };
@@ -803,7 +803,7 @@ OlmDevice.prototype.decryptMessage = async function(
this._saveSession(theirDeviceIdentityKey, sessionInfo, txn); this._saveSession(theirDeviceIdentityKey, sessionInfo, txn);
}); });
}, },
getPrefixedLogger("[decryptMessage]"), logger.withPrefix("[decryptMessage]"),
); );
return payloadString; return payloadString;
}; };
@@ -835,7 +835,7 @@ OlmDevice.prototype.matchesSession = async function(
matches = sessionInfo.session.matches_inbound(ciphertext); matches = sessionInfo.session.matches_inbound(ciphertext);
}); });
}, },
getPrefixedLogger("[matchesSession]"), logger.withPrefix("[matchesSession]"),
); );
return matches; return matches;
}; };
@@ -1106,7 +1106,7 @@ OlmDevice.prototype.addInboundGroupSession = async function(
}, },
); );
}, },
getPrefixedLogger("[addInboundGroupSession]"), logger.withPrefix("[addInboundGroupSession]"),
); );
}; };
@@ -1277,7 +1277,7 @@ OlmDevice.prototype.decryptGroupMessage = async function(
}, },
); );
}, },
getPrefixedLogger("[decryptGroupMessage]"), logger.withPrefix("[decryptGroupMessage]"),
); );
if (error) { if (error) {
@@ -1323,7 +1323,7 @@ OlmDevice.prototype.hasInboundSessionKeys = async function(roomId, senderKey, se
}, },
); );
}, },
getPrefixedLogger("[hasInboundSessionKeys]"), logger.withPrefix("[hasInboundSessionKeys]"),
); );
return result; return result;
@@ -1383,7 +1383,7 @@ OlmDevice.prototype.getInboundGroupSessionKey = async function(
}, },
); );
}, },
getPrefixedLogger("[getInboundGroupSessionKey]"), logger.withPrefix("[getInboundGroupSessionKey]"),
); );
return result; return result;

View File

@@ -22,7 +22,7 @@ limitations under the License.
* @module crypto/algorithms/megolm * @module crypto/algorithms/megolm
*/ */
import {getPrefixedLogger, logger} from '../../logger'; import {logger} from '../../logger';
import * as utils from "../../utils"; import * as utils from "../../utils";
import {polyfillSuper} from "../../utils"; import {polyfillSuper} from "../../utils";
import * as olmlib from "../olmlib"; import * as olmlib from "../olmlib";
@@ -736,7 +736,7 @@ MegolmEncryption.prototype._shareKeyWithDevices = async function(
logger.debug(`Ensuring Olm sessions for devices in ${this._roomId}`); logger.debug(`Ensuring Olm sessions for devices in ${this._roomId}`);
const devicemap = await olmlib.ensureOlmSessionsForDevices( const devicemap = await olmlib.ensureOlmSessionsForDevices(
this._olmDevice, this._baseApis, devicesByUser, otkTimeout, failedServers, this._olmDevice, this._baseApis, devicesByUser, otkTimeout, failedServers,
getPrefixedLogger(`[${this._roomId}]`), logger.withPrefix(`[${this._roomId}]`),
); );
logger.debug(`Ensured Olm sessions for devices in ${this._roomId}`); logger.debug(`Ensured Olm sessions for devices in ${this._roomId}`);

View File

@@ -59,17 +59,27 @@ log.methodFactory = function(methodName, logLevel, loggerName) {
* Drop-in replacement for <code>console</code> using {@link https://www.npmjs.com/package/loglevel|loglevel}. * Drop-in replacement for <code>console</code> using {@link https://www.npmjs.com/package/loglevel|loglevel}.
* Can be tailored down to specific use cases if needed. * Can be tailored down to specific use cases if needed.
*/ */
export const logger = log.getLogger(DEFAULT_NAMESPACE); export const logger: PrefixedLogger = log.getLogger(DEFAULT_NAMESPACE);
logger.setLevel(log.levels.DEBUG); logger.setLevel(log.levels.DEBUG);
interface PrefixedLogger extends Logger { interface PrefixedLogger extends Logger {
prefix?: any; withPrefix?: (prefix: string) => PrefixedLogger;
prefix?: string;
} }
export function getPrefixedLogger(prefix): PrefixedLogger { function extendLogger(logger: PrefixedLogger) {
logger.withPrefix = function(prefix: string) {
return getPrefixedLogger(this.prefix + prefix);
};
}
extendLogger(logger);
function getPrefixedLogger(prefix): PrefixedLogger {
const prefixLogger: PrefixedLogger = log.getLogger(`${DEFAULT_NAMESPACE}-${prefix}`); const prefixLogger: PrefixedLogger = log.getLogger(`${DEFAULT_NAMESPACE}-${prefix}`);
if (prefixLogger.prefix !== prefix) { if (prefixLogger.prefix !== prefix) {
// Only do this setup work the first time through, as loggers are saved by name. // Only do this setup work the first time through, as loggers are saved by name.
extendLogger(prefixLogger);
prefixLogger.prefix = prefix; prefixLogger.prefix = prefix;
prefixLogger.setLevel(log.levels.DEBUG); prefixLogger.setLevel(log.levels.DEBUG);
} }