@obsidize/logger
Version:
A tiny javascript logging library
107 lines (106 loc) • 3.54 kB
TypeScript
import type { LogEventLike } from './types';
/**
* Anonymous function variant of `LogEventSerializerLike`
*/
export type LogEventSerializerDelegate = (ev: LogEventLike) => string;
/**
* Delegate that transforms a value for a log event property into a string.
*/
export type LogEventSerializerPropertyFormatter = (value: any, serializer: LogEventSerializer) => string;
/**
* Common API for serializing log events
*/
export interface LogEventSerializerLike {
serialize(ev: LogEventLike): string;
}
/**
* Options that can be used to generate a serialization function
*/
export interface LogEventSerializerDelegateConfig {
/**
* Direct serializer function reference.
* Takes highest precedence if provided.
*/
serializeEvent?: LogEventSerializerDelegate;
/**
* A serializer-like interface.
* Will take precedence over `serializerConfig` if provided.
*/
serializer?: LogEventSerializerLike;
/**
* Configuration for a new `LogEventSerializer` instance.
* Lowest precedence, only used if no other options are provided.
*/
serializerConfig?: Partial<LogEventSerializerConfig>;
}
export interface LogEventSerializerConfig {
/**
* Object where keys are integer levels and values are a string which is the name of the level.
* @default inverse of the `LogLevel` object literal
*/
levelNameMap: Record<number, string>;
/**
* Seperator string that will be used as a prefix for each log parameter.
* @default ' :: '
*/
paramsSeperator: string;
/**
* The maximum allowed length for a serialized parameter before it is truncated.
* @default 250
*/
maxParamStringLength: number;
/**
* The minimum required length for level names.
* This can help with alignment issues in large volume log output.
* @default 0
*/
levelNameFixedLength: number;
/**
* Custom format for the serialized output.
* When this is set, all `include***` flags will be ignored.
* @default '\{timestamp\} [\{level\}] [\{tag\}] \{message\}\{params\}'
*/
format?: string;
/**
* Transformers for each individual log event property.
*/
propertyFormatters: Record<string, LogEventSerializerPropertyFormatter>;
/**
* Includes the timestamp in the serialized output
* @default true
*/
includeTimestamp: boolean;
/**
* Includes the level in the serialized output
* @default true
*/
includeLevel: boolean;
/**
* Includes the tag in the serialized output
* @default true
*/
includeTag: boolean;
/**
* Includes all params in the serialized output
* @default true
*/
includeParams: boolean;
}
/**
* Configurable transformer to convert log events into string output.
*/
export declare class LogEventSerializer implements LogEventSerializerLike {
readonly config: LogEventSerializerConfig;
private format;
constructor(config?: Partial<LogEventSerializerConfig>);
/**
* Obtains a serializer function from the given config
* @param config - the options to parse a serializer delegate from
* @returns a function that will convert
*/
static parseDelegateFrom(config: LogEventSerializerDelegateConfig): LogEventSerializerDelegate;
extend(config?: Partial<LogEventSerializerConfig>): LogEventSerializer;
serialize(ev: LogEventLike): string;
serializeParamList(params: any[] | undefined): string;
serializeParam(param: any): string;
}