You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-08-09 10:22:46 +03:00
Inherit methodFactory
extensions from the parent to the child loggers. (#4809)
* use methodFactory extensions from the rootLogger in child loggers. * use simple method factory copy AND `childLogger.setLevel(childLogger.getLevel());` This is the important part that actually registers the new methods. * add comments and find a way to make it clearer that the types are correct. * review * additionally fix MatrixRTCSessionManager being initialized before the extension is in place. * Add comment to clarify order of log extensions and creating childs. * review * Set "loglevel" min version to guarantee access to `logger.rebuild`
This commit is contained in:
@@ -56,7 +56,7 @@
|
|||||||
"bs58": "^6.0.0",
|
"bs58": "^6.0.0",
|
||||||
"content-type": "^1.0.4",
|
"content-type": "^1.0.4",
|
||||||
"jwt-decode": "^4.0.0",
|
"jwt-decode": "^4.0.0",
|
||||||
"loglevel": "^1.7.1",
|
"loglevel": "^1.9.2",
|
||||||
"matrix-events-sdk": "0.0.1",
|
"matrix-events-sdk": "0.0.1",
|
||||||
"matrix-widget-api": "^1.10.0",
|
"matrix-widget-api": "^1.10.0",
|
||||||
"oidc-client-ts": "^3.0.1",
|
"oidc-client-ts": "^3.0.1",
|
||||||
|
@@ -34,6 +34,11 @@ export interface Logger extends BaseLogger {
|
|||||||
/**
|
/**
|
||||||
* Create a child logger.
|
* Create a child logger.
|
||||||
*
|
*
|
||||||
|
* This child will use the `methodFactory` of the parent, so any log extensions applied to the parent
|
||||||
|
* at the time of calling `getChild` will be applied to the child as well.
|
||||||
|
* It will NOT apply changes to the parent's `methodFactory` after the child was created.
|
||||||
|
* Those changes need to be applied to the child manually.
|
||||||
|
*
|
||||||
* @param namespace - name to add to the current logger to generate the child. Some implementations of `Logger`
|
* @param namespace - name to add to the current logger to generate the child. Some implementations of `Logger`
|
||||||
* use this as a prefix; others use a different mechanism.
|
* use this as a prefix; others use a different mechanism.
|
||||||
*/
|
*/
|
||||||
@@ -128,14 +133,24 @@ interface PrefixedLogger extends loglevel.Logger, LoggerWithLogMethod {
|
|||||||
*
|
*
|
||||||
* @param prefix Prefix to add to each logged line. If undefined, no prefix will be added.
|
* @param prefix Prefix to add to each logged line. If undefined, no prefix will be added.
|
||||||
*/
|
*/
|
||||||
function getPrefixedLogger(prefix?: string): LoggerWithLogMethod {
|
function getPrefixedLogger(prefix?: string): PrefixedLogger {
|
||||||
const loggerName = DEFAULT_NAMESPACE + (prefix === undefined ? "" : `-${prefix}`);
|
const loggerName = DEFAULT_NAMESPACE + (prefix === undefined ? "" : `-${prefix}`);
|
||||||
const prefixLogger = loglevel.getLogger(loggerName) as PrefixedLogger;
|
const prefixLogger = loglevel.getLogger(loggerName) as PrefixedLogger;
|
||||||
|
|
||||||
if (prefixLogger.getChild === undefined) {
|
if (prefixLogger.getChild === undefined) {
|
||||||
// This is a new loglevel Logger which has not been turned into a PrefixedLogger yet.
|
// This is a new loglevel Logger which has not been turned into a PrefixedLogger yet.
|
||||||
prefixLogger.prefix = prefix;
|
prefixLogger.prefix = prefix;
|
||||||
prefixLogger.getChild = (childPrefix): Logger => getPrefixedLogger((prefix ?? "") + childPrefix);
|
prefixLogger.getChild = (childPrefix): Logger => {
|
||||||
|
// create the new child logger
|
||||||
|
const childLogger = getPrefixedLogger((prefix ?? "") + childPrefix);
|
||||||
|
// Assign the methodFactory from the parent logger.
|
||||||
|
// This is useful if we add extensions to the parent logger that modifies
|
||||||
|
// its methodFactory. (An example extension is: storing each log to a rageshake db)
|
||||||
|
childLogger.methodFactory = prefixLogger.methodFactory;
|
||||||
|
// Rebuild the child logger with the new methodFactory.
|
||||||
|
childLogger.rebuild();
|
||||||
|
return childLogger;
|
||||||
|
};
|
||||||
prefixLogger.setLevel(loglevel.levels.DEBUG, false);
|
prefixLogger.setLevel(loglevel.levels.DEBUG, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,7 +161,7 @@ function getPrefixedLogger(prefix?: string): LoggerWithLogMethod {
|
|||||||
* Drop-in replacement for `console` using {@link https://www.npmjs.com/package/loglevel|loglevel}.
|
* Drop-in replacement for `console` 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 = getPrefixedLogger();
|
export const logger = getPrefixedLogger() as LoggerWithLogMethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A "span" for grouping related log lines together.
|
* A "span" for grouping related log lines together.
|
||||||
|
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { logger as rootLogger } from "../logger.ts";
|
import { logger as rootLogger, type Logger } from "../logger.ts";
|
||||||
import { type MatrixClient, ClientEvent } from "../client.ts";
|
import { type MatrixClient, ClientEvent } from "../client.ts";
|
||||||
import { TypedEventEmitter } from "../models/typed-event-emitter.ts";
|
import { TypedEventEmitter } from "../models/typed-event-emitter.ts";
|
||||||
import { type Room } from "../models/room.ts";
|
import { type Room } from "../models/room.ts";
|
||||||
@@ -23,8 +23,6 @@ import { type MatrixEvent } from "../models/event.ts";
|
|||||||
import { MatrixRTCSession } from "./MatrixRTCSession.ts";
|
import { MatrixRTCSession } from "./MatrixRTCSession.ts";
|
||||||
import { EventType } from "../@types/event.ts";
|
import { EventType } from "../@types/event.ts";
|
||||||
|
|
||||||
const logger = rootLogger.getChild("[MatrixRTCSessionManager]");
|
|
||||||
|
|
||||||
export enum MatrixRTCSessionManagerEvents {
|
export enum MatrixRTCSessionManagerEvents {
|
||||||
// A member has joined the MatrixRTC session, creating an active session in a room where there wasn't previously
|
// A member has joined the MatrixRTC session, creating an active session in a room where there wasn't previously
|
||||||
SessionStarted = "session_started",
|
SessionStarted = "session_started",
|
||||||
@@ -50,8 +48,10 @@ export class MatrixRTCSessionManager extends TypedEventEmitter<MatrixRTCSessionM
|
|||||||
// longer the correct session object for the room.
|
// longer the correct session object for the room.
|
||||||
private roomSessions = new Map<string, MatrixRTCSession>();
|
private roomSessions = new Map<string, MatrixRTCSession>();
|
||||||
|
|
||||||
|
private logger: Logger;
|
||||||
public constructor(private client: MatrixClient) {
|
public constructor(private client: MatrixClient) {
|
||||||
super();
|
super();
|
||||||
|
this.logger = rootLogger.getChild("[MatrixRTCSessionManager]");
|
||||||
}
|
}
|
||||||
|
|
||||||
public start(): void {
|
public start(): void {
|
||||||
@@ -105,7 +105,7 @@ export class MatrixRTCSessionManager extends TypedEventEmitter<MatrixRTCSessionM
|
|||||||
private onRoomState = (event: MatrixEvent, _state: RoomState): void => {
|
private onRoomState = (event: MatrixEvent, _state: RoomState): void => {
|
||||||
const room = this.client.getRoom(event.getRoomId());
|
const room = this.client.getRoom(event.getRoomId());
|
||||||
if (!room) {
|
if (!room) {
|
||||||
logger.error(`Got room state event for unknown room ${event.getRoomId()}!`);
|
this.logger.error(`Got room state event for unknown room ${event.getRoomId()}!`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,10 +129,10 @@ export class MatrixRTCSessionManager extends TypedEventEmitter<MatrixRTCSessionM
|
|||||||
const nowActive = session.memberships.length > 0;
|
const nowActive = session.memberships.length > 0;
|
||||||
|
|
||||||
if (wasActiveAndKnown && !nowActive) {
|
if (wasActiveAndKnown && !nowActive) {
|
||||||
logger.trace(`Session ended for ${room.roomId} (${session.memberships.length} members)`);
|
this.logger.trace(`Session ended for ${room.roomId} (${session.memberships.length} members)`);
|
||||||
this.emit(MatrixRTCSessionManagerEvents.SessionEnded, room.roomId, this.roomSessions.get(room.roomId)!);
|
this.emit(MatrixRTCSessionManagerEvents.SessionEnded, room.roomId, this.roomSessions.get(room.roomId)!);
|
||||||
} else if (!wasActiveAndKnown && nowActive) {
|
} else if (!wasActiveAndKnown && nowActive) {
|
||||||
logger.trace(`Session started for ${room.roomId} (${session.memberships.length} members)`);
|
this.logger.trace(`Session started for ${room.roomId} (${session.memberships.length} members)`);
|
||||||
this.emit(MatrixRTCSessionManagerEvents.SessionStarted, room.roomId, this.roomSessions.get(room.roomId)!);
|
this.emit(MatrixRTCSessionManagerEvents.SessionStarted, room.roomId, this.roomSessions.get(room.roomId)!);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -5081,7 +5081,7 @@ log-update@^6.1.0:
|
|||||||
strip-ansi "^7.1.0"
|
strip-ansi "^7.1.0"
|
||||||
wrap-ansi "^9.0.0"
|
wrap-ansi "^9.0.0"
|
||||||
|
|
||||||
loglevel@^1.7.1:
|
loglevel@^1.9.2:
|
||||||
version "1.9.2"
|
version "1.9.2"
|
||||||
resolved "https://registry.yarnpkg.com/loglevel/-/loglevel-1.9.2.tgz#c2e028d6c757720107df4e64508530db6621ba08"
|
resolved "https://registry.yarnpkg.com/loglevel/-/loglevel-1.9.2.tgz#c2e028d6c757720107df4e64508530db6621ba08"
|
||||||
integrity sha512-HgMmCqIJSAKqo68l0rS2AanEWfkxaZ5wNiEFb5ggm08lDs9Xl2KxBlX3PTcaD2chBM1gXAYf491/M2Rv8Jwayg==
|
integrity sha512-HgMmCqIJSAKqo68l0rS2AanEWfkxaZ5wNiEFb5ggm08lDs9Xl2KxBlX3PTcaD2chBM1gXAYf491/M2Rv8Jwayg==
|
||||||
|
Reference in New Issue
Block a user