Skip to content

Commit

Permalink
chore: adds redis transport
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonydmays committed Jan 16, 2025
1 parent 5fdeeea commit ed68978
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
30 changes: 30 additions & 0 deletions lib/javascript/fullstack_demo/src/util/redis-transport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Redis } from '@upstash/redis';
import { v4 as uuidv4 } from 'uuid';
import Transport from 'winston-transport';

export class RedisTransport extends Transport {
private readonly redis: Redis;

constructor(opts: any) {
super(opts);
this.redis = new Redis({
url: process.env.KV_REST_API_URL,
token: process.env.KV_REST_API_TOKEN,
});
}

async log(info: any, callback: () => void): Promise<void> {
setImmediate(() => {
this.emit('logged', info);
});

try {
const uuid = uuidv4();
await this.redis.append(`logs:${uuid}`, JSON.stringify(info));
} catch (ex) {
console.error('Failed to log to Redis', ex);
}

callback();
}
}
17 changes: 13 additions & 4 deletions lib/javascript/fullstack_demo/src/util/server-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@ import { LogEvent } from '@/models';
import { auth } from '@clerk/nextjs/server';
import { headers } from 'next/headers';
import winston, { format } from 'winston';
import Transport from 'winston-transport';
import { Logger, LogLevel } from './logger';
import { PrismaTransport } from './prisma-transport';
import { RedisTransport } from './redis-transport';
const { combine, timestamp, json } = format;

export class WinstonLogger implements Logger {
private readonly winstonLogger: winston.Logger;

constructor() {
const transports: Transport[] = [
new winston.transports.Console({ level: 'info' }),
];

if (process.env.LOGGING_DB_TYPE?.toUpperCase() === 'POSTGRES') {
transports.push(new PrismaTransport({ level: 'debug' }));
} else if (process.env.LOGGING_DB_TYPE?.toUpperCase() === 'REDIS') {
transports.push(new RedisTransport({ level: 'debug' }));
}

this.winstonLogger = winston.createLogger({
level: 'debug',
format: combine(
Expand All @@ -22,10 +34,7 @@ export class WinstonLogger implements Logger {
timestamp(),
json(),
),
transports: [
new winston.transports.Console({ level: 'info' }),
new PrismaTransport({ level: 'debug' }),
],
transports,
});
}

Expand Down

0 comments on commit ed68978

Please sign in to comment.