mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 03:19:46 +00:00
replace logi handler
This commit is contained in:
parent
6778a42d54
commit
82e7f854b9
2 changed files with 74 additions and 0 deletions
|
|
@ -152,6 +152,8 @@ import dittoNamesRoute from '@/routes/dittoNamesRoute.ts';
|
|||
import pleromaAdminPermissionGroupsRoute from '@/routes/pleromaAdminPermissionGroupsRoute.ts';
|
||||
import pleromaStatusesRoute from '@/routes/pleromaStatusesRoute.ts';
|
||||
import { DittoRelayStore } from '@/storages/DittoRelayStore.ts';
|
||||
import { logi } from '@soapbox/logi';
|
||||
import { createLogiHandler } from '@/utils/logi.ts';
|
||||
|
||||
export interface AppEnv extends DittoEnv {
|
||||
Variables: DittoEnv['Variables'] & {
|
||||
|
|
@ -179,6 +181,7 @@ type AppMiddleware = MiddlewareHandler<AppEnv>;
|
|||
type AppController<P extends string = any> = Handler<AppEnv, P, HonoInput, Response | Promise<Response>>;
|
||||
|
||||
const conf = new DittoConf(Deno.env);
|
||||
logi.handler = createLogiHandler(conf, logi.handler);
|
||||
|
||||
startSentry(conf);
|
||||
|
||||
|
|
|
|||
71
packages/ditto/utils/logi.ts
Normal file
71
packages/ditto/utils/logi.ts
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
import type { LogiHandler, LogiLog, LogiValue } from '@soapbox/logi';
|
||||
import { DittoConf } from '@ditto/conf';
|
||||
|
||||
type Level = LogiLog['level'];
|
||||
|
||||
const levels: Level[] = ['trace', 'debug', 'info', 'warn', 'error', 'fatal', 'critical'];
|
||||
|
||||
const lowerLevels: Record<Level, Level[]> = levels.reduce((acc, level, index) => {
|
||||
acc[level] = levels.slice(index);
|
||||
return acc;
|
||||
}, {} as Record<Level, Level[]>);
|
||||
|
||||
const colors: Record<Level, string> = {
|
||||
trace: 'grey',
|
||||
debug: 'white',
|
||||
info: 'blue',
|
||||
warn: 'yellow',
|
||||
error: 'orange',
|
||||
fatal: 'red',
|
||||
critical: 'red',
|
||||
};
|
||||
|
||||
const levelSet = new Set(levels);
|
||||
const isLevel = (str: string): str is Level => levelSet.has(str as Level);
|
||||
|
||||
const prettyPrint = (msg: LogiValue): string => {
|
||||
const message = msg || '';
|
||||
const type = typeof message;
|
||||
switch (type) {
|
||||
case 'string':
|
||||
case 'bigint':
|
||||
case 'number':
|
||||
case 'boolean':
|
||||
return message.toString();
|
||||
case 'function':
|
||||
case 'symbol':
|
||||
case 'undefined':
|
||||
return `<${type}>`;
|
||||
case 'object':
|
||||
if (message === null) return "<null>";
|
||||
return JSON.stringify(message, (_, v) => {
|
||||
if (Array.isArray(v)) {
|
||||
return `[${v.map(itm => JSON.stringify(itm)).join(', ')}]`;
|
||||
}
|
||||
if (typeof v === 'string') return `\`${v}\``;
|
||||
return v;
|
||||
}, 2)
|
||||
.replaceAll('\\"', '"')
|
||||
.replace(/^"/, '')
|
||||
.replace(/"$/, '');
|
||||
}
|
||||
}
|
||||
|
||||
const pair = (key: string, value: LogiValue | undefined) => {
|
||||
return `${key} => ${prettyPrint(value || '')}`
|
||||
}
|
||||
|
||||
export const createLogiHandler = (conf: DittoConf, defaultHandler: LogiHandler) => (log: LogiLog) => {
|
||||
const { fmt, level, scopes } = conf.logConfig;
|
||||
if (fmt === 'jsonl') return defaultHandler(log);
|
||||
if (!isLevel(level)) throw new Error(`Invalid log level ${level} specified`);
|
||||
if (!lowerLevels[level].includes(log.level)) return;
|
||||
if (scopes.length && !scopes.includes(log.ns)) return;
|
||||
const message = prettyPrint(log.message || log.msg || '');
|
||||
const remaining = Object.entries(log)
|
||||
.filter(([key]) => !['ns', 'level', 'message', 'msg'].includes(key));
|
||||
|
||||
console.group(`%c${log.level.toUpperCase()} %c${log.ns} %c${message || ''}`, `color: ${colors[log.level]}; font-weight: bold`, 'font-weight: normal; color: yellow', 'color: unset');
|
||||
console.log(remaining.map(itm => pair(...itm)).join('\n'));
|
||||
console.groupEnd();
|
||||
};
|
||||
Loading…
Add table
Reference in a new issue