Skip to content

Commit d12eae7

Browse files
authored
Merge pull request #128 from ulixee/named-logger
feat(client): add sessionid/name to logs + errors
2 parents 4c9025e + 7d88f35 commit d12eae7

File tree

7 files changed

+52
-17
lines changed

7 files changed

+52
-17
lines changed

client/connections/CoreClientConnection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export default abstract class CoreClientConnection {
101101

102102
public async createSession(options: ICreateSessionOptions): Promise<CoreSession> {
103103
const sessionMeta = await this.commandQueue.run<ISessionMeta>('createSession', options);
104-
const session = new CoreSession(sessionMeta, this);
104+
const session = new CoreSession({ ...sessionMeta, sessionName: options.sessionName }, this);
105105
this.coreSessions.track(session);
106106
return session;
107107
}

client/lib/CoreCommandQueue.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,25 @@ export default class CoreCommandQueue {
1111
public lastCommandId = 0;
1212
private isProcessing = false;
1313

14+
private readonly sessionMarker: string = '';
15+
1416
constructor(
15-
private readonly meta: ISessionMeta | null,
17+
private readonly meta: (ISessionMeta & { sessionName: string }) | null,
1618
private readonly connection: CoreClientConnection,
1719
parentCommandQueue?: CoreCommandQueue,
1820
) {
1921
if (parentCommandQueue) {
2022
this.type = parentCommandQueue.type;
2123
}
24+
if (meta) {
25+
const markers = [
26+
''.padEnd(50, '-'),
27+
`------${meta.sessionName ?? ''}`.padEnd(50, '-'),
28+
`------${meta.sessionId ?? ''}`.padEnd(50, '-'),
29+
''.padEnd(50, '-'),
30+
].join('\n');
31+
this.sessionMarker = `\n\n${markers}`;
32+
}
2233
}
2334

2435
public run<T>(command: string, ...args: any[]): Promise<T> {
@@ -62,7 +73,9 @@ export default class CoreCommandQueue {
6273
}
6374
item.resolve(data);
6475
} catch (error) {
65-
error.stack += `\n${'------CORE COMMANDS'.padEnd(50, '-')}${item.stack}`;
76+
error.stack += `\n${'------CORE COMMANDS'.padEnd(50, '-')}${item.stack}${
77+
this.sessionMarker
78+
}`;
6679
item.reject(error);
6780
}
6881
// force next loop so promises don't simulate synchronous-ity when local core

client/lib/CoreSession.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import ISessionMeta from '@secret-agent/core-interfaces/ISessionMeta';
22
import IConfigureSessionOptions from '@secret-agent/core-interfaces/IConfigureSessionOptions';
33
import { IJsPath } from 'awaited-dom/base/AwaitedPath';
4+
import { loggerSessionIdNames } from '@secret-agent/commons/Logger';
45
import CoreCommandQueue from './CoreCommandQueue';
56
import CoreEventHeap from './CoreEventHeap';
67
import CoreTab from './CoreTab';
@@ -10,6 +11,7 @@ import CoreClientConnection from '../connections/CoreClientConnection';
1011
export default class CoreSession implements IJsPathEventTarget {
1112
public tabsById = new Map<string, CoreTab>();
1213
public sessionId: string;
14+
public sessionName: string;
1315
public sessionsDataLocation: string;
1416
public replayApiServer: string;
1517
public commandQueue: CoreCommandQueue;
@@ -22,17 +24,23 @@ export default class CoreSession implements IJsPathEventTarget {
2224
protected readonly meta: ISessionMeta;
2325
private readonly connection: CoreClientConnection;
2426

25-
constructor(sessionMeta: ISessionMeta, connection: CoreClientConnection) {
26-
const { sessionId, sessionsDataLocation, replayApiServer } = sessionMeta;
27+
constructor(
28+
sessionMeta: ISessionMeta & { sessionName: string },
29+
connection: CoreClientConnection,
30+
) {
31+
const { sessionId, sessionsDataLocation, replayApiServer, sessionName } = sessionMeta;
2732
this.sessionId = sessionId;
33+
this.sessionName = sessionName;
2834
this.sessionsDataLocation = sessionsDataLocation;
2935
this.replayApiServer = replayApiServer;
3036
this.meta = {
3137
sessionId,
3238
};
3339
this.connection = connection;
34-
this.commandQueue = new CoreCommandQueue(this.meta, connection);
40+
loggerSessionIdNames.set(sessionId, sessionName);
41+
this.commandQueue = new CoreCommandQueue({ sessionId, sessionName }, connection);
3542
this.eventHeap = new CoreEventHeap(this.meta, connection);
43+
3644
this.addTab(sessionMeta);
3745
}
3846

@@ -59,7 +67,10 @@ export default class CoreSession implements IJsPathEventTarget {
5967

6068
public addTab(tabMeta: ISessionMeta): void {
6169
if (!this.tabsById.has(tabMeta.tabId)) {
62-
this.tabsById.set(tabMeta.tabId, new CoreTab(tabMeta, this.connection));
70+
this.tabsById.set(
71+
tabMeta.tabId,
72+
new CoreTab({ ...tabMeta, sessionName: this.sessionName }, this.connection),
73+
);
6374
}
6475
}
6576

@@ -70,6 +81,7 @@ export default class CoreSession implements IJsPathEventTarget {
7081
public async close(): Promise<void> {
7182
await this.commandQueue.run('closeSession');
7283
process.nextTick(() => this.connection.closeSession(this));
84+
loggerSessionIdNames.delete(this.sessionId);
7385
}
7486

7587
public async addEventListener(

client/lib/CoreTab.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,20 @@ export default class CoreTab implements IJsPathEventTarget {
2626
public commandQueue: CoreCommandQueue;
2727
public eventHeap: CoreEventHeap;
2828

29-
protected readonly meta: ISessionMeta;
29+
protected readonly meta: ISessionMeta & { sessionName: string };
3030
private readonly connection: CoreClientConnection;
3131

32-
constructor({ tabId, sessionId }: ISessionMeta, connection: CoreClientConnection) {
32+
constructor(meta: ISessionMeta & { sessionName: string }, connection: CoreClientConnection) {
33+
const { tabId, sessionId, sessionName } = meta;
3334
this.tabId = tabId;
3435
this.sessionId = sessionId;
3536
this.meta = {
3637
sessionId,
3738
tabId,
39+
sessionName,
3840
};
3941
this.connection = connection;
40-
this.commandQueue = new CoreCommandQueue(this.meta, connection, connection.commandQueue);
42+
this.commandQueue = new CoreCommandQueue(meta, connection, connection.commandQueue);
4143
this.eventHeap = new CoreEventHeap(this.meta, connection);
4244

4345
if (!this.eventHeap.hasEventInterceptors('resource')) {
@@ -146,7 +148,7 @@ export default class CoreTab implements IJsPathEventTarget {
146148
const sessionMeta = await this.commandQueue.run<ISessionMeta>('waitForNewTab', opts);
147149
const session = this.connection.getSession(sessionMeta.sessionId);
148150
session.addTab(sessionMeta);
149-
return new CoreTab(sessionMeta, this.connection);
151+
return new CoreTab({ ...this.meta, tabId: sessionMeta.tabId }, this.connection);
150152
}
151153

152154
public async focusTab(): Promise<void> {

client/lib/ScriptInstance.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { v1 as uuidv1 } from 'uuid';
22
import IScriptInstanceMeta from '@secret-agent/core-interfaces/IScriptInstanceMeta';
3-
import Log from '@secret-agent/commons/Logger';
43
import CoreSession from './CoreSession';
54

6-
const { log } = Log(module);
7-
85
export default class ScriptInstance {
96
public readonly id: string = uuidv1();
107
public readonly entrypoint: string = process.argv[1];

commons/Logger.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class Log implements ILog {
7575
.replace('.js', '')
7676
.replace('.ts', '')
7777
.replace('build/', '');
78-
const printData = {};
78+
const printData: any = {};
7979
for (const [key, value] of Object.entries(entry.data)) {
8080
if (value === undefined || value === null) continue;
8181
if (value instanceof Error) {
@@ -86,6 +86,12 @@ class Log implements ILog {
8686
printData[key] = value;
8787
}
8888
}
89+
90+
if (level === 'warn' || level === 'error') {
91+
printData.sessionId = sessionId;
92+
printData.sessionName = loggerSessionIdNames.get(sessionId) ?? undefined;
93+
}
94+
8995
const params = Object.keys(printData).length ? [printData] : [];
9096
// eslint-disable-next-line no-console
9197
console.log(
@@ -116,6 +122,8 @@ export default function logger(module: NodeModule): ILogBuilder {
116122

117123
let idCounter = 0;
118124

125+
const loggerSessionIdNames = new Map<string, string>();
126+
119127
class LogEvents {
120128
private static subscriptions: { [id: number]: (log: ILogEntry) => any } = {};
121129

@@ -135,7 +143,7 @@ class LogEvents {
135143
}
136144
}
137145

138-
export { LogEvents };
146+
export { LogEvents, loggerSessionIdNames };
139147

140148
export function injectLogger(builder: (module: NodeModule) => ILogBuilder): void {
141149
logCreator = builder;

session-state/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import IWebsocketMessage from '@secret-agent/core-interfaces/IWebsocketMessage';
88
import IResourceMeta from '@secret-agent/core-interfaces/IResourceMeta';
99
import ICommandMeta from '@secret-agent/core-interfaces/ICommandMeta';
1010
import { IBoundLog } from '@secret-agent/core-interfaces/ILog';
11-
import Log, { ILogEntry, LogEvents } from '@secret-agent/commons/Logger';
11+
import Log, { ILogEntry, LogEvents, loggerSessionIdNames } from '@secret-agent/commons/Logger';
1212
import { IDomChangeEvent } from '@secret-agent/injected-scripts/interfaces/IDomChangeEvent';
1313
import { LocationStatus } from '@secret-agent/core-interfaces/Location';
1414
import IViewport from '@secret-agent/core-interfaces/IViewport';
@@ -114,6 +114,8 @@ export default class SessionState {
114114
viewport,
115115
);
116116

117+
loggerSessionIdNames.set(sessionId, sessionName);
118+
117119
this.logSubscriptionId = LogEvents.subscribe(this.onLogEvent.bind(this));
118120
}
119121

@@ -373,6 +375,7 @@ export default class SessionState {
373375
this.closeDate = new Date();
374376
this.db.session.close(this.sessionId, this.closeDate);
375377
LogEvents.unsubscribe(this.logSubscriptionId);
378+
loggerSessionIdNames.delete(this.sessionId);
376379
this.db.flush();
377380
this.db.close();
378381
SessionState.registry.delete(this.sessionId);

0 commit comments

Comments
 (0)