mirror of
https://github.com/diced/zipline.git
synced 2025-12-27 13:14:00 -08:00
feat: new logger stuff
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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<string, unknown>) {
|
||||
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<string, unknown>) {
|
||||
process.stdout.write(`${this.format(message, level)}${extra ? this.formatExtra(extra) : ''}\n`);
|
||||
}
|
||||
|
||||
public info(args: string, extra?: Record<string, unknown>) {
|
||||
this.write(args, 'info', extra);
|
||||
return this;
|
||||
}
|
||||
|
||||
public warn(...args: unknown[]) {
|
||||
this.write(args.join(' '), 'warn');
|
||||
public warn(args: string, extra?: Record<string, unknown>) {
|
||||
this.write(args, 'warn', extra);
|
||||
return this;
|
||||
}
|
||||
|
||||
public error(...args: unknown[]) {
|
||||
this.write(args.join(' '), 'error');
|
||||
public error(args: string | Error, extra?: Record<string, unknown>) {
|
||||
this.write(args.toString(), 'error', extra);
|
||||
return this;
|
||||
}
|
||||
|
||||
public debug(...args: unknown[]) {
|
||||
public debug(args: string, extra?: Record<string, unknown>) {
|
||||
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<string, unknown>) {
|
||||
this.write(args, 'trace', extra);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ export async function handler(_: NextApiReq, res: NextApiRes<ApiHealthcheckRespo
|
||||
|
||||
return res.ok({ pass: true });
|
||||
} catch (e) {
|
||||
logger.error('there was an error during a healthcheck').error(e);
|
||||
logger.error('there was an error during a healthcheck').error(e as Error);
|
||||
|
||||
return res.serverError('there was an error during a healthcheck', {
|
||||
pass: false,
|
||||
|
||||
@@ -115,7 +115,7 @@ export async function handler(req: NextApiReq<any, any, UploadHeaders>, 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}`
|
||||
|
||||
@@ -40,7 +40,7 @@ export async function handler(req: NextApiReq<Body, Query>, res: NextApiRes<ApiU
|
||||
select: fileSelect,
|
||||
});
|
||||
|
||||
logger.info(`${req.user.username} updated file ${newFile.name} (favorite=${newFile.favorite})`);
|
||||
logger.info(`${req.user.username} updated file ${newFile.name}`, { favorite: newFile.favorite });
|
||||
|
||||
return res.ok(newFile);
|
||||
} else if (req.method === 'DELETE') {
|
||||
@@ -53,7 +53,7 @@ export async function handler(req: NextApiReq<Body, Query>, res: NextApiRes<ApiU
|
||||
|
||||
await datasource.delete(deletedFile.name);
|
||||
|
||||
logger.info(`${req.user.username} deleted file ${deletedFile.name} (size=${bytes(deletedFile.size)})`);
|
||||
logger.info(`${req.user.username} deleted file ${deletedFile.name}`, { size: bytes(deletedFile.size) });
|
||||
|
||||
return res.ok(deletedFile);
|
||||
}
|
||||
|
||||
@@ -12,12 +12,14 @@ import { guess } from '@/lib/mimes';
|
||||
import { extname } from 'path';
|
||||
import { verifyPassword } from '@/lib/crypto';
|
||||
|
||||
import { version } from '../../package.json';
|
||||
|
||||
const MODE = process.env.NODE_ENV || 'production';
|
||||
|
||||
const logger = log('server');
|
||||
|
||||
async function main() {
|
||||
logger.info(`starting zipline in ${MODE} mode`);
|
||||
logger.info('starting zipline', { mode: MODE, version: version });
|
||||
|
||||
const server = express();
|
||||
|
||||
@@ -120,7 +122,10 @@ async function main() {
|
||||
});
|
||||
|
||||
server.listen(config.core.port, config.core.hostname, () => {
|
||||
logger.info(`server listening on port ${config.core.port}`);
|
||||
logger.info(`server listening`, {
|
||||
hostname: config.core.hostname,
|
||||
port: config.core.port,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user