Skip to content

Commit e217bf9

Browse files
committed
Enable prefixed loggers to chain
1 parent 198c9a2 commit e217bf9

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

src/crypto/OlmDevice.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ See the License for the specific language governing permissions and
1616
limitations under the License.
1717
*/
1818

19-
import {getPrefixedLogger, logger} from '../logger';
19+
import {logger} from '../logger';
2020
import {IndexedDBCryptoStore} from './store/indexeddb-crypto-store';
2121
import * as algorithms from './algorithms';
2222

@@ -545,7 +545,7 @@ OlmDevice.prototype.createOutboundSession = async function(
545545
}
546546
});
547547
},
548-
getPrefixedLogger("[createOutboundSession]"),
548+
logger.withPrefix("[createOutboundSession]"),
549549
);
550550
return newSessionId;
551551
};
@@ -606,7 +606,7 @@ OlmDevice.prototype.createInboundSession = async function(
606606
}
607607
});
608608
},
609-
getPrefixedLogger("[createInboundSession]"),
609+
logger.withPrefix("[createInboundSession]"),
610610
);
611611

612612
return result;
@@ -621,7 +621,7 @@ OlmDevice.prototype.createInboundSession = async function(
621621
* @return {Promise<string[]>} a list of known session ids for the device
622622
*/
623623
OlmDevice.prototype.getSessionIdsForDevice = async function(theirDeviceIdentityKey) {
624-
const log = getPrefixedLogger("[getSessionIdsForDevice]");
624+
const log = logger.withPrefix("[getSessionIdsForDevice]");
625625

626626
if (this._sessionsInProgress[theirDeviceIdentityKey]) {
627627
log.debug(`Waiting for Olm session for ${theirDeviceIdentityKey} to be created`);
@@ -705,7 +705,7 @@ OlmDevice.prototype.getSessionIdForDevice = async function(
705705
* @return {Array.<{sessionId: string, hasReceivedMessage: Boolean}>}
706706
*/
707707
OlmDevice.prototype.getSessionInfoForDevice = async function(deviceIdentityKey, nowait) {
708-
const log = getPrefixedLogger("[getSessionInfoForDevice]");
708+
const log = logger.withPrefix("[getSessionInfoForDevice]");
709709

710710
if (this._sessionsInProgress[deviceIdentityKey] && !nowait) {
711711
log.debug(`Waiting for Olm session for ${deviceIdentityKey} to be created`);
@@ -769,7 +769,7 @@ OlmDevice.prototype.encryptMessage = async function(
769769
this._saveSession(theirDeviceIdentityKey, sessionInfo, txn);
770770
});
771771
},
772-
getPrefixedLogger("[encryptMessage]"),
772+
logger.withPrefix("[encryptMessage]"),
773773
);
774774
return res;
775775
};
@@ -803,7 +803,7 @@ OlmDevice.prototype.decryptMessage = async function(
803803
this._saveSession(theirDeviceIdentityKey, sessionInfo, txn);
804804
});
805805
},
806-
getPrefixedLogger("[decryptMessage]"),
806+
logger.withPrefix("[decryptMessage]"),
807807
);
808808
return payloadString;
809809
};
@@ -835,7 +835,7 @@ OlmDevice.prototype.matchesSession = async function(
835835
matches = sessionInfo.session.matches_inbound(ciphertext);
836836
});
837837
},
838-
getPrefixedLogger("[matchesSession]"),
838+
logger.withPrefix("[matchesSession]"),
839839
);
840840
return matches;
841841
};
@@ -1106,7 +1106,7 @@ OlmDevice.prototype.addInboundGroupSession = async function(
11061106
},
11071107
);
11081108
},
1109-
getPrefixedLogger("[addInboundGroupSession]"),
1109+
logger.withPrefix("[addInboundGroupSession]"),
11101110
);
11111111
};
11121112

@@ -1277,7 +1277,7 @@ OlmDevice.prototype.decryptGroupMessage = async function(
12771277
},
12781278
);
12791279
},
1280-
getPrefixedLogger("[decryptGroupMessage]"),
1280+
logger.withPrefix("[decryptGroupMessage]"),
12811281
);
12821282

12831283
if (error) {
@@ -1323,7 +1323,7 @@ OlmDevice.prototype.hasInboundSessionKeys = async function(roomId, senderKey, se
13231323
},
13241324
);
13251325
},
1326-
getPrefixedLogger("[hasInboundSessionKeys]"),
1326+
logger.withPrefix("[hasInboundSessionKeys]"),
13271327
);
13281328

13291329
return result;
@@ -1383,7 +1383,7 @@ OlmDevice.prototype.getInboundGroupSessionKey = async function(
13831383
},
13841384
);
13851385
},
1386-
getPrefixedLogger("[getInboundGroupSessionKey]"),
1386+
logger.withPrefix("[getInboundGroupSessionKey]"),
13871387
);
13881388

13891389
return result;

src/crypto/algorithms/megolm.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ limitations under the License.
2222
* @module crypto/algorithms/megolm
2323
*/
2424

25-
import {getPrefixedLogger, logger} from '../../logger';
25+
import {logger} from '../../logger';
2626
import * as utils from "../../utils";
2727
import {polyfillSuper} from "../../utils";
2828
import * as olmlib from "../olmlib";
@@ -736,7 +736,7 @@ MegolmEncryption.prototype._shareKeyWithDevices = async function(
736736
logger.debug(`Ensuring Olm sessions for devices in ${this._roomId}`);
737737
const devicemap = await olmlib.ensureOlmSessionsForDevices(
738738
this._olmDevice, this._baseApis, devicesByUser, otkTimeout, failedServers,
739-
getPrefixedLogger(`[${this._roomId}]`),
739+
logger.withPrefix(`[${this._roomId}]`),
740740
);
741741
logger.debug(`Ensured Olm sessions for devices in ${this._roomId}`);
742742

src/logger.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,27 @@ log.methodFactory = function(methodName, logLevel, loggerName) {
5959
* Drop-in replacement for <code>console</code> using {@link https://www.npmjs.com/package/loglevel|loglevel}.
6060
* Can be tailored down to specific use cases if needed.
6161
*/
62-
export const logger = log.getLogger(DEFAULT_NAMESPACE);
62+
export const logger: PrefixedLogger = log.getLogger(DEFAULT_NAMESPACE);
6363
logger.setLevel(log.levels.DEBUG);
6464

6565
interface PrefixedLogger extends Logger {
66-
prefix?: any;
66+
withPrefix?: (prefix: string) => PrefixedLogger;
67+
prefix?: string;
6768
}
6869

69-
export function getPrefixedLogger(prefix): PrefixedLogger {
70+
function extendLogger(logger: PrefixedLogger) {
71+
logger.withPrefix = function(prefix: string) {
72+
return getPrefixedLogger(this.prefix + prefix);
73+
};
74+
}
75+
76+
extendLogger(logger);
77+
78+
function getPrefixedLogger(prefix): PrefixedLogger {
7079
const prefixLogger: PrefixedLogger = log.getLogger(`${DEFAULT_NAMESPACE}-${prefix}`);
7180
if (prefixLogger.prefix !== prefix) {
7281
// Only do this setup work the first time through, as loggers are saved by name.
82+
extendLogger(prefixLogger);
7383
prefixLogger.prefix = prefix;
7484
prefixLogger.setLevel(log.levels.DEBUG);
7585
}

0 commit comments

Comments
 (0)