Skip to content

Commit fece9fa

Browse files
authored
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.
1 parent 69f1bea commit fece9fa

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

src/logger.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ interface LoggerWithLogMethod extends Logger {
3333
export interface Logger extends BaseLogger {
3434
/**
3535
* Create a child logger.
36+
* This child will apply the methodFactory of the parent. So any log extensions applied to the parent
37+
* at the time of calling `getChild` will be applied to the child as well.
38+
* It will NOT apply changes to the parents methodFactory after the child was created.
39+
* Those changes will not be applied manually to the child.
3640
*
3741
* @param namespace - name to add to the current logger to generate the child. Some implementations of `Logger`
3842
* use this as a prefix; others use a different mechanism.
@@ -128,14 +132,24 @@ interface PrefixedLogger extends loglevel.Logger, LoggerWithLogMethod {
128132
*
129133
* @param prefix Prefix to add to each logged line. If undefined, no prefix will be added.
130134
*/
131-
function getPrefixedLogger(prefix?: string): LoggerWithLogMethod {
135+
function getPrefixedLogger(prefix?: string): PrefixedLogger {
132136
const loggerName = DEFAULT_NAMESPACE + (prefix === undefined ? "" : `-${prefix}`);
133137
const prefixLogger = loglevel.getLogger(loggerName) as PrefixedLogger;
134138

135139
if (prefixLogger.getChild === undefined) {
136140
// This is a new loglevel Logger which has not been turned into a PrefixedLogger yet.
137141
prefixLogger.prefix = prefix;
138-
prefixLogger.getChild = (childPrefix): Logger => getPrefixedLogger((prefix ?? "") + childPrefix);
142+
prefixLogger.getChild = (childPrefix): Logger => {
143+
// create the new child logger
144+
const childLogger = getPrefixedLogger((prefix ?? "") + childPrefix);
145+
// assign the same methodFactory as the root logger.
146+
// this is useful if we add extensions to the root logger that modify
147+
// its methodFactory. (an example extension is: storing each log to a rageshake db)
148+
childLogger.methodFactory = prefixLogger.methodFactory;
149+
// rebuild the child logger with the new methodFactory.
150+
childLogger.rebuild();
151+
return childLogger;
152+
};
139153
prefixLogger.setLevel(loglevel.levels.DEBUG, false);
140154
}
141155

@@ -146,7 +160,7 @@ function getPrefixedLogger(prefix?: string): LoggerWithLogMethod {
146160
* Drop-in replacement for `console` using {@link https://www.npmjs.com/package/loglevel|loglevel}.
147161
* Can be tailored down to specific use cases if needed.
148162
*/
149-
export const logger = getPrefixedLogger();
163+
export const logger = getPrefixedLogger() as LoggerWithLogMethod;
150164

151165
/**
152166
* A "span" for grouping related log lines together.

src/matrixrtc/MatrixRTCSessionManager.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import { logger as rootLogger } from "../logger.ts";
17+
import { logger as rootLogger, type Logger } from "../logger.ts";
1818
import { type MatrixClient, ClientEvent } from "../client.ts";
1919
import { TypedEventEmitter } from "../models/typed-event-emitter.ts";
2020
import { type Room } from "../models/room.ts";
@@ -23,8 +23,6 @@ import { type MatrixEvent } from "../models/event.ts";
2323
import { MatrixRTCSession } from "./MatrixRTCSession.ts";
2424
import { EventType } from "../@types/event.ts";
2525

26-
const logger = rootLogger.getChild("[MatrixRTCSessionManager]");
27-
2826
export enum MatrixRTCSessionManagerEvents {
2927
// A member has joined the MatrixRTC session, creating an active session in a room where there wasn't previously
3028
SessionStarted = "session_started",
@@ -50,8 +48,10 @@ export class MatrixRTCSessionManager extends TypedEventEmitter<MatrixRTCSessionM
5048
// longer the correct session object for the room.
5149
private roomSessions = new Map<string, MatrixRTCSession>();
5250

51+
private logger: Logger;
5352
public constructor(private client: MatrixClient) {
5453
super();
54+
this.logger = rootLogger.getChild("[MatrixRTCSessionManager]");
5555
}
5656

5757
public start(): void {
@@ -105,7 +105,7 @@ export class MatrixRTCSessionManager extends TypedEventEmitter<MatrixRTCSessionM
105105
private onRoomState = (event: MatrixEvent, _state: RoomState): void => {
106106
const room = this.client.getRoom(event.getRoomId());
107107
if (!room) {
108-
logger.error(`Got room state event for unknown room ${event.getRoomId()}!`);
108+
this.logger.error(`Got room state event for unknown room ${event.getRoomId()}!`);
109109
return;
110110
}
111111

@@ -129,10 +129,10 @@ export class MatrixRTCSessionManager extends TypedEventEmitter<MatrixRTCSessionM
129129
const nowActive = session.memberships.length > 0;
130130

131131
if (wasActiveAndKnown && !nowActive) {
132-
logger.trace(`Session ended for ${room.roomId} (${session.memberships.length} members)`);
132+
this.logger.trace(`Session ended for ${room.roomId} (${session.memberships.length} members)`);
133133
this.emit(MatrixRTCSessionManagerEvents.SessionEnded, room.roomId, this.roomSessions.get(room.roomId)!);
134134
} else if (!wasActiveAndKnown && nowActive) {
135-
logger.trace(`Session started for ${room.roomId} (${session.memberships.length} members)`);
135+
this.logger.trace(`Session started for ${room.roomId} (${session.memberships.length} members)`);
136136
this.emit(MatrixRTCSessionManagerEvents.SessionStarted, room.roomId, this.roomSessions.get(room.roomId)!);
137137
}
138138
}

0 commit comments

Comments
 (0)