Skip to content

Commit 8534bda

Browse files
authored
docs(scanner): add logger api doc (#640)
1 parent 2ddafa0 commit 8534bda

2 files changed

Lines changed: 159 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ Additional APIs are available at:
161161

162162
- [from](./workspaces/scanner/docs/from.md)
163163
- [extractors](./workspaces/scanner/docs/extractors.md)
164+
- [logger](./workspaces/scanner/docs/logger.md)
164165

165166
## Workspaces
166167

workspaces/scanner/docs/logger.md

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# Logger API
2+
3+
A lightweight event-based timing and monitoring utility for tracking code execution phases and errors.
4+
5+
## Usage example
6+
7+
Here's how to track the execution time of a custom operation:
8+
9+
```ts
10+
import { Logger } from "@nodesecure/scanner";
11+
12+
const logger = new Logger();
13+
14+
logger.on("start", (eventName) => {
15+
console.log(`Starting: ${eventName}`);
16+
});
17+
18+
logger.on("tick", (eventName) => {
19+
console.log(`Tick: ${eventName}`);
20+
});
21+
22+
logger.on("end", (eventName, data) => {
23+
console.log(`Finished: ${eventName}`);
24+
console.log(`Duration: ${data.executionTime}ms`);
25+
console.log(`Iterations: ${data.count}`);
26+
});
27+
28+
logger.start("my-operation");
29+
30+
for (let i = 0; i < 5; i++) {
31+
logger.tick("my-operation");
32+
}
33+
34+
logger.end("my-operation");
35+
```
36+
37+
## Events
38+
39+
The Logger class extends Node.js `EventEmitter`, allowing you to listen for lifecycle events during tracking.
40+
41+
### start
42+
43+
Emitted when a new event tracking is initiated:
44+
45+
```ts
46+
logger.on("start", (eventName: string) => {
47+
// Handle start event
48+
});
49+
```
50+
51+
### tick
52+
53+
Emitted each time `tick()` is called for a tracked event:
54+
55+
```ts
56+
logger.on("tick", (eventName: string) => {
57+
// Handle tick event
58+
});
59+
```
60+
61+
### end
62+
63+
Emitted when `end()` is called, with full execution metadata:
64+
65+
```ts
66+
logger.on("end", (eventName: string, data: LoggerEventData & { executionTime: number }) => {
67+
console.log(`Execution time: ${data.executionTime}ms`);
68+
console.log(`Total ticks: ${data.count}`);
69+
});
70+
```
71+
72+
### error
73+
74+
Emitted when an error occurs during the logging process:
75+
76+
```ts
77+
logger.on("error", (error: Error, phase?: string) => {
78+
console.error(`Error in ${phase ?? "unknown"}:`, error);
79+
});
80+
```
81+
82+
### depWalkerFinished
83+
84+
Emitted when the dependency walker completes its analysis:
85+
86+
```ts
87+
logger.on("depWalkerFinished", () => {
88+
console.log("Dependency analysis complete");
89+
});
90+
```
91+
92+
## API
93+
94+
### constructor()
95+
96+
Creates a new Logger instance.
97+
98+
```ts
99+
const logger = new Logger();
100+
```
101+
102+
### start(eventName: string): this
103+
104+
Begins tracking a new event. If the event is already being tracked, this is a no-op.
105+
106+
```ts
107+
logger.start("custom-event");
108+
```
109+
110+
### tick(eventName: string): this
111+
112+
Increments the tick count for a tracked event. Does nothing if the event is not being tracked.
113+
114+
```ts
115+
logger.tick("custom-event");
116+
```
117+
118+
### count(eventName: string): number
119+
120+
Returns the current tick count for a tracked event, or `0` if the event is not being tracked.
121+
122+
```ts
123+
const count = logger.count("custom-event");
124+
console.log(`Ticks: ${count}`);
125+
```
126+
127+
### end(eventName: string): this
128+
129+
Ends tracking for an event and emits the `end` event with execution metadata.
130+
131+
```ts
132+
logger.end("custom-event");
133+
```
134+
135+
## Interfaces
136+
137+
### LoggerEventData
138+
139+
```ts
140+
export interface LoggerEventData {
141+
/** UNIX Timestamp */
142+
startedAt: number;
143+
/** Count of triggered event */
144+
count: number;
145+
}
146+
```
147+
148+
### LoggerEventsMap
149+
150+
```ts
151+
export type LoggerEventsMap = {
152+
start: [eventName: string];
153+
tick: [eventName: string];
154+
end: [eventName: string, data: LoggerEventData & { executionTime: number; }];
155+
depWalkerFinished: [];
156+
error: [error: Error, phase?: string];
157+
};
158+
```

0 commit comments

Comments
 (0)