logpipes
Version:
Console.log transformation pipes
99 lines (98 loc) • 4.38 kB
TypeScript
import { JsonSimplifierOptions } from './JsonSimplifier';
import { LogLevel, LogPipe } from './ConsoleOverrides';
/** Options for JsonPipe. */
export interface JsonPipeOptions extends JsonSimplifierOptions {
/**
* Top-level property name that includes a concatenated message of all strings and primitive types passed to console.log.
* Default: 'message'.
*/
messagePropertyName: string;
/**
* Log level property name.
* If <null>, no log level info is added to the result JSON.
* Default: 'level'.
*/
levelPropertyName: string | null;
/** Log level value formatter. User only if @levelPropertyName is not <null>. */
levelPropertyFormatter: (level: LogLevel) => string;
/**
* Timestamp property name.
* If <null>, no timestamp is added to the result JSON.
* Default: 'timestamp'.
*/
timestampPropertyName: string | null;
/** Timestamp formatter. User only if @timestampPropertyName is not <null>. */
timestampPropertyFormatter: (timeInMillis: number) => string;
/**
* Message id property name.
* If <null>, no message id is added to the result JSON.
* Default: 'message_id'.
*/
messageIdPropertyName: string | null;
/**
* Message id property value provider.
* Default: a uuid like value generated by *generateUuidSimple*.
* When *undefined* is returned falls back to *generateUuidSimple* result.
*/
messageIdPropertyProvider: (level: LogLevel, ...args: Array<unknown>) => string | undefined;
/**
* Builds object token for the message.
* By default, uses '$N' as a pattern where 'N' is positional a number (@messageArgumentIndex + 1)
* of the console.log argument not inlined into the message.
* @originalArgumentIndex is the original index of the argument in console.log() call.
* Example:
* console.log('a', {f:0}, 'b', {f:0});
* @messageArgumentIndex = 0, 1.
* @originalArgumentIndex = 1, 3.
*/
getObjectMessageToken: (messageArgumentIndex: number, argument: object, originalArgumentIndex: number) => string;
/**
* For a single top-level field objects uses field name as an argument name and includes only a field sub-objects into arguments.
*
* Example: console("Hello", {headers: {header1:'', header2: ''}});
*
* pickFieldNameAsObjectMessageTokenForSingleFieldObjects = true:
* {"message":"Hello $headers","$headers":{"header1":"","header2":""}}
*
* pickFieldNameAsObjectMessageTokenForSingleFieldObjects = false:
* {"message":"Hello $1","$1":{"headers":{"header1":"","header2":""}}}
*
* Default: false. Overrides 'getObjectMessageToken'.
*/
pickFieldNameAsObjectMessageTokenForSingleFieldObjects: boolean;
/**
* Used to provide a default value to reveal present but undefined fields.
* The default value is <undefined> which results the fields with undefined value be excluded from the log.
*/
undefinedMessageValue: undefined | string;
}
/** Returns default properties used by 'createJsonPipe'. */
export declare function getDefaultJsonPipeOptions(): JsonPipeOptions & {
levelPropertyName: string;
timestampPropertyName: string;
messageIdPropertyName: string;
messageIdPropertyProvider: () => string;
};
export interface JsonPipe extends LogPipe<unknown[]> {
/**
* Returns the last message ID assigned by the pipe.
* If no messages were emitted yet contains an empty string.
* If 'messageIdPropertyName' is null on the JsonPipe configuration (no ID is assigned), the value stays empty.
*/
getLastMessageId: () => string;
/**
* Sets next emitted message id.
* When set the *messageIdPropertyProvider* won't be used to generate the next message id.
*/
setNextMessageId: (messageId: string) => void;
}
/**
* Creates a pipe that converts console arguments into a serializable JSON object.
*/
export declare function createJsonPipe(inputOptions?: Partial<JsonPipeOptions>): JsonPipe;
/**
* Generates a random uuid-like string.
* The method does not provide the same level of randomness like 'crypto' based UUID generators.
* but it is good for most logging purposes were message id uniqueness is required only on some time-local period.
*/
export declare function generateUuidSimple(): string;