From f8477958a4f97a02a09292d978f87eee58690751 Mon Sep 17 00:00:00 2001 From: diced Date: Tue, 4 Jul 2023 22:56:21 -0700 Subject: [PATCH] feat: new logger stuff --- src/lib/db/index.ts | 2 +- src/lib/db/migration/index.ts | 4 +-- src/lib/logger.ts | 35 ++++++++++++++++---------- src/pages/api/healthcheck.ts | 2 +- src/pages/api/upload.ts | 2 +- src/pages/api/user/files/[id]/index.ts | 4 +-- src/server/index.ts | 9 +++++-- 7 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/lib/db/index.ts b/src/lib/db/index.ts index 4cf0e4b2..8897f219 100644 --- a/src/lib/db/index.ts +++ b/src/lib/db/index.ts @@ -19,7 +19,7 @@ if (process.env.NODE_ENV === 'production') { function getClient() { const logger = log('db'); - logger.info('connecting to database', process.env.DATABASE_URL); + logger.info('connecting to database ' + process.env.DATABASE_URL); const client = new PrismaClient(); client.$connect(); diff --git a/src/lib/db/migration/index.ts b/src/lib/db/migration/index.ts index 7eb51205..5cc98c2a 100644 --- a/src/lib/db/migration/index.ts +++ b/src/lib/db/migration/index.ts @@ -14,7 +14,7 @@ export async function runMigrations() { logger.info('database created'); } } catch (e) { - logger.error('failed to create database', e); + logger.error('failed to create database' + e); logger.error('try creating the database manually and running the server again'); migrate.stop(); @@ -27,7 +27,7 @@ export async function runMigrations() { const { appliedMigrationNames } = await migrate.applyMigrations(); migrationIds = appliedMigrationNames; } catch (e) { - logger.error('failed to apply migrations', e); + logger.error('failed to apply migrations' + e); migrate.stop(); process.exit(1); diff --git a/src/lib/logger.ts b/src/lib/logger.ts index 2a9326e0..6f471b96 100644 --- a/src/lib/logger.ts +++ b/src/lib/logger.ts @@ -1,5 +1,5 @@ import dayjs from 'dayjs'; -import { green, red, yellow, gray, white, bold } from 'colorette'; +import { green, red, yellow, gray, white, bold, blue } from 'colorette'; export type LoggerLevel = 'info' | 'warn' | 'error' | 'debug' | 'trace'; @@ -38,35 +38,44 @@ export default class Logger { } } - private write(message: string, level: LoggerLevel) { - process.stdout.write(`${this.format(message, level)}\n`); + private formatExtra(extra: Record) { + return ( + ' ' + + Object.entries(extra) + .map(([key, value]) => `${blue(key)}${gray('=')}${JSON.stringify(value)}`) + .join(' ') + ); } - public info(...args: unknown[]) { - this.write(args.join(' '), 'info'); + private write(message: string, level: LoggerLevel, extra?: Record) { + process.stdout.write(`${this.format(message, level)}${extra ? this.formatExtra(extra) : ''}\n`); + } + + public info(args: string, extra?: Record) { + this.write(args, 'info', extra); return this; } - public warn(...args: unknown[]) { - this.write(args.join(' '), 'warn'); + public warn(args: string, extra?: Record) { + this.write(args, 'warn', extra); return this; } - public error(...args: unknown[]) { - this.write(args.join(' '), 'error'); + public error(args: string | Error, extra?: Record) { + this.write(args.toString(), 'error', extra); return this; } - public debug(...args: unknown[]) { + public debug(args: string, extra?: Record) { if (process.env.DEBUG !== 'zipline') return this; - this.write(args.join(' '), 'debug'); + this.write(args, 'debug', extra); return this; } - public trace(...args: unknown[]) { - this.write(args.join(' '), 'trace'); + public trace(args: string, extra?: Record) { + this.write(args, 'trace', extra); return this; } } diff --git a/src/pages/api/healthcheck.ts b/src/pages/api/healthcheck.ts index c6b75c8e..a19bf820 100644 --- a/src/pages/api/healthcheck.ts +++ b/src/pages/api/healthcheck.ts @@ -19,7 +19,7 @@ export async function handler(_: NextApiReq, res: NextApiRes, res: Nex await datasource.put(fileUpload.name, file.buffer); - logger.info(`${req.user.username} uploaded ${fileUpload.name} (size=${bytes(fileUpload.size)})`); + logger.info(`${req.user.username} uploaded ${fileUpload.name}`, { size: bytes(fileUpload.size) }); const responseUrl = `${domain}${ zconfig.files.route === '/' || zconfig.files.route === '' ? '' : `${zconfig.files.route}` diff --git a/src/pages/api/user/files/[id]/index.ts b/src/pages/api/user/files/[id]/index.ts index ad2db604..e3a65ac5 100644 --- a/src/pages/api/user/files/[id]/index.ts +++ b/src/pages/api/user/files/[id]/index.ts @@ -40,7 +40,7 @@ export async function handler(req: NextApiReq, res: NextApiRes, res: NextApiRes { - logger.info(`server listening on port ${config.core.port}`); + logger.info(`server listening`, { + hostname: config.core.hostname, + port: config.core.port, + }); }); }