Skip to content

Commit

Permalink
chore: standardize logging (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbsp authored Sep 30, 2024
1 parent c0cce8a commit d3db7cf
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-radios-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@livekit/agents": patch
---

standardize logging
3 changes: 2 additions & 1 deletion agents/src/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class AudioByteStream {
#numChannels: number;
#bytesPerFrame: number;
#buf: Int8Array;
#logger = log();

constructor(sampleRate: number, numChannels: number, samplesPerChannel: number | null = null) {
this.#sampleRate = sampleRate;
Expand Down Expand Up @@ -46,7 +47,7 @@ export class AudioByteStream {

flush(): AudioFrame[] {
if (this.#buf.length % (2 * this.#numChannels) !== 0) {
log().warn('AudioByteStream: incomplete frame during flush, dropping');
this.#logger.warn('AudioByteStream: incomplete frame during flush, dropping');
return [];
}

Expand Down
9 changes: 5 additions & 4 deletions agents/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,37 @@ type CliArgs = {

const runWorker = async (args: CliArgs) => {
initializeLogger({ pretty: !args.production, level: args.opts.logLevel });
const logger = log();

// though `production` is defined in WorkerOptions, it will always be overriddden by CLI.
const { production: _, ...opts } = args.opts; // eslint-disable-line @typescript-eslint/no-unused-vars
const worker = new Worker(new WorkerOptions({ production: args.production, ...opts }));

if (args.room) {
worker.event.once('worker_registered', () => {
log().info(`connecting to room ${args.room}`);
logger.info(`connecting to room ${args.room}`);
worker.simulateJob(args.room!, args.participantIdentity);
});
}

process.once('SIGINT', async () => {
// allow C-c C-c for force interrupt
process.once('SIGINT', () => {
log().info('worker closed forcefully');
logger.info('worker closed forcefully');
process.exit(130); // SIGINT exit code
});
if (args.production) {
await worker.drain();
}
await worker.close();
log().info('worker closed');
logger.info('worker closed');
process.exit(130); // SIGINT exit code
});

try {
await worker.run();
} catch {
log().fatal('worker failed');
logger.fatal('worker failed');
process.exit(1);
}
};
Expand Down
3 changes: 2 additions & 1 deletion agents/src/http_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export class HTTPServer {
host: string;
port: number;
app: Server;
#logger = log();

constructor(host: string, port: number) {
this.host = host;
Expand All @@ -34,7 +35,7 @@ export class HTTPServer {
if (err) reject(err);
const address = this.app.address();
if (typeof address! !== 'string') {
log().info(`Server is listening on port ${address!.port}`);
this.#logger.info(`Server is listening on port ${address!.port}`);
}
resolve();
});
Expand Down
3 changes: 2 additions & 1 deletion agents/src/transcription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class BasicTranscriptionForwarder implements TranscriptionForwarder {
#charsPerSecond: number = this.#DEFAULT_CHARS_PER_SECOND;
#messageId: string;
#isRunning: boolean = false;
#logger = log();
currentCharacterIndex: number = 0;

constructor(room: Room, participantIdentity: string, trackSid: string, messageId: string) {
Expand All @@ -38,7 +39,7 @@ export class BasicTranscriptionForwarder implements TranscriptionForwarder {
if (!this.#isRunning) {
this.#isRunning = true;
this.#startPublishingLoop().catch((error) => {
log().error('Error in publishing loop:', error);
this.#logger.error('Error in publishing loop:', error);
this.#isRunning = false;
});
}
Expand Down

0 comments on commit d3db7cf

Please sign in to comment.