cutie-logs
v0.1.5
Published
Pretty console logging for Axios and WebSocket traffic.
Downloads
112
Readme
cutie-logs
Pretty console logging for Axios and WebSocket traffic.
Install
npm install cutie-logsAxios logging is optional. Install Axios in your app if you use cutie-logs/axios:
npm install axiosAxios
import axios from 'axios';
import {attachAxiosLogger} from 'cutie-logs/axios';
const api = axios.create({
baseURL: '/api/v1',
withCredentials: true,
});
attachAxiosLogger(api, {
enabled: process.env.NEXT_PUBLIC_API_LOGGING !== 'false',
label: 'API',
maxPayloadKB: 100,
logRequestsDelay: true,
logRequestsTime: true,
stripUrlPrefixes: ['http://localhost:8000', 'https://xlartas.com'],
});attachAxiosLogger returns a cleanup function:
const detach = attachAxiosLogger(api);
detach();WebSocket
WebSocket logging is explicit because patching global WebSocket is too magical for application code.
import {createWsLogger} from 'cutie-logs/ws';
const wsLogger = createWsLogger({
enabled: process.env.NEXT_PUBLIC_API_LOGGING !== 'false',
label: 'WS',
maxPayloadKB: 100,
stripUrlPrefixes: ['ws://localhost:8000', 'wss://xlartas.com'],
});
wsLogger.connecting('/ws/chat/', url);
const ws = new WebSocket(url);
ws.onopen = () => wsLogger.open('/ws/chat/', url);
ws.onclose = (event) => wsLogger.close('/ws/chat/', event.code, event.reason);
ws.onerror = (event) => wsLogger.error('/ws/chat/', event);
ws.onmessage = (event) => {
const payload = JSON.parse(event.data);
wsLogger.message('/ws/chat/', payload.event ?? 'message', payload);
};Options
type CutieLogOptions = {
enabled?: boolean;
label?: string;
maxPayloadKB?: number | null;
redactFields?: readonly string[];
stripUrlPrefixes?: readonly string[];
logRequestsDelay?: boolean;
logRequestsTime?: boolean;
timeLocale?: string;
timestampFormatter?: (date: Date) => string;
console?: Pick<Console, 'log' | 'error' | 'warn' | 'groupCollapsed' | 'groupEnd'>;
colors?: Partial<CutieLogColors>;
};enableddefaults totrue.labeldefaults toAPIfor Axios andWSfor WebSocket.maxPayloadKBdefaults tonull, so payloads are not truncated.redactFieldsdefaults to common sensitive fields such aspassword,access,refresh,token,authorization,api_key, andsecret.stripUrlPrefixesremoves noisy URL prefixes from logs, for examplehttp://localhost:8000.logRequestsDelayshows response/error delay such as[124ms]. It defaults tofalse.logRequestsTimeshows the current browser-local time. It defaults totrue.timestampFormatteroverrides the defaultHH:MMbrowser-local time formatter.consoleis mainly for tests or custom environments.
Exports
import {attachAxiosLogger} from 'cutie-logs/axios';
import {createWsLogger} from 'cutie-logs/ws';
import {sanitizePayload, formatPayload} from 'cutie-logs';Development
npm ci
npm run check
npm test
npm run build
npm run pack:dry-runRelease
See Release Guide.
