logpipes
Version:
Console.log transformation pipes
60 lines (59 loc) • 2.7 kB
TypeScript
import { LogLevel, LogPipe } from './ConsoleOverrides';
/**
* LogCachePipe caches up to 'cacheSize' messages,
* notifies about cache overflow using 'onCacheSizeReached'
* and provides access to the cache.
*/
export interface LogCachePipe extends LogPipe<unknown[]> {
/** Returns a list of all cached messages. */
getMessages(): Array<LogCachePipeMessage>;
/** Clears messages cache. */
clearMessages(): void;
}
/** Options for 'createLogCachePipe'. */
export interface LogCachePipeOptions {
/**
* How many messages to keep in the cache.
* Once the limit is reached and a new message arrives, 'onCacheSizeReached' is called and
* the oldest message is removed from the cache.
*
* Default: 1000.
*/
cacheSize: number;
/**
* How much data to keep in the cache using 'sum(stringify(...args))' formula.
* Once the limit is reached and a new message arrives, onCacheSizeReached is called and a set of the oldest
* messages are removed from the cache to keep the cache size within the limit.
* The size estimator is optimized for string message parameters (for example, a stringifies JSON messages).
*
* Default: not used (-1).
*/
cacheSizeByStringify: number;
/**
* A callback to notify about cache overflow.
* Called every time the cache is full: count of messages is > cacheSize or 'cacheSizeByStringify' is met.
*
* A callee can use 'getMessages' or 'clearMessages' inside the callback to access the current cache state.
* It is recommended to clear all messages in the callback to avoid callback calls on every new console log event.
* Default: undefined, no action.
*
* Warning: avoid calling console.log from inside 'onCacheSizeReached'.
* If console log method is called from 'onCacheSizeReached' the pipe will ignore these messages.
* In order to avoid this problem and still be able to use console in methods called from 'onCacheSizeReached',
* wrap the code that logs to console into an asynchronous task like a 'setTimeout()' call.
*
* Default: undefined.
*/
onCacheSizeReached?: (pipe: LogCachePipe) => void;
}
/** A cached message model. */
export interface LogCachePipeMessage {
level: LogLevel;
/** Time the message was cached. */
timestamp: number;
args: unknown[];
}
export declare function getDefaultLogCachePipeOptions(): LogCachePipeOptions;
export declare function createLogCachePipe(inputOptions?: Partial<LogCachePipeOptions>): LogCachePipe;
/** Estimates args array size using simplifyJson call. */
export declare function estimateArgsSizeByStringify(...args: Array<unknown>): number;