@altostra/core
Version:
Core library for shared types and logic
86 lines (85 loc) • 4.57 kB
TypeScript
import type { Maybe } from "../Maybe";
import type { DefaultSeverity, Logger, LoggerMetadata, LogMessage, OperationSeverity } from "./Common";
/**
* Options for logger creation
*/
export interface LoggerCreationOptions<TSeverity extends string> {
/** A list of severities to use instead of the default severities */
severities?: readonly TSeverity[];
/** An object that contain metadata that would be attached to logged messages */
metadata?: LoggerMetadata;
}
/**
* Creates a logger that uses the given logging function to write the logs
* Use this function to create new loggers like ConsoleLogger, NetworkLogger etc'
* @param source The source of logged messages
* @param logging A function to log LogMessages
* @param severities An optional array of severities the logger supports
* @returns A logger from the specified source, with the specified severities (or the default ones)
*/
export declare function createLogger<TSeverity extends string = DefaultSeverity>(source: string, logging: (message: LogMessage<TSeverity>) => void, severities?: readonly TSeverity[]): Logger<TSeverity>;
/**
* Creates a logger that uses the given logging function to write the logs
* Use this function to create new loggers like ConsoleLogger, NetworkLogger etc'
* @param source The source of logged messages
* @param options Options for logger creation
* @returns A logger from the specified source, with the specified severities (or the default ones)
*/
export declare function createLogger<TSeverity extends string = DefaultSeverity>(source: string, logging: (message: LogMessage<TSeverity>) => void, options?: LoggerCreationOptions<TSeverity>): Logger<TSeverity>;
/**
* Creates an implementation of a logger that write to the console
* @param verbose Optional value that determines if verbose messages are logged
* @param source Optional logger source
* @returns A logger that logs messages to the console
*/
export declare function consoleLogger(verbose?: boolean, source?: string): Logger;
/**
*Creates a logger that logs nothing
* @param source Optional source for the logger
* @param severities An optional array of severities the logger supports
* @returns A logger that logs nothing
*/
export declare function nullLogger<TSeverity extends string = DefaultSeverity>(source?: string, severities?: readonly TSeverity[]): Logger<TSeverity>;
export declare type MessageMapper<TIn extends string, TOut extends string> = (logMessage: LogMessage<TIn>) => LogMessage<TOut>;
export declare type SeverityMapper<TIn extends string, TOut extends string> = Maybe<MessageMapper<TIn, TOut> | TOut>;
export declare type MessageMapping<TIn extends string, TOut extends string> = {
[K in TIn]?: SeverityMapper<TIn, TOut>;
};
/**
* combineLoggers parameters
*/
export interface CombineLoggersParams<TFirstSeverity extends string, TSecondSeverity extends string> {
/**
* The source of the result logger.
*/
source: string;
/**
* The first logger to combine.
*/
first: Logger<TFirstSeverity>;
/**
* The second logger to combine
*/
second: Logger<TSecondSeverity>;
/**
* A mapping from `first` logger's severities to `second` logger's severities,
* or to a function that map messages from the specified severity into messages of the `second` logger's severities
*/
mapFromFirstLoggerSeverities?: MessageMapping<TFirstSeverity, TSecondSeverity>;
/**
* A mapping from `second` logger's severities to `first` logger's severities,
* or to a function that map messages from the specified severity into messages of the `first` logger's severities
*/
mapFromSecondLoggerSeverities?: MessageMapping<TSecondSeverity, TFirstSeverity>;
}
/**
* Create a logger that logs messages into two different loggers, allowing mapping of log messages severities.
*
* If a message arrives with severity which is unsupported by one of the loggers,
* and there's no applicable severity mapping
* for that message severity, the message would only be sent to the applicable logger
* @param param0 Parameters object
* @returns A logger that logs messages into the two specified loggers
*/
export declare function combineLoggers<TFirstSeverity extends string, TSecondSeverity extends string>({ source, first, second, mapFromFirstLoggerSeverities, mapFromSecondLoggerSeverities, }: CombineLoggersParams<TFirstSeverity, TSecondSeverity>): Logger<TFirstSeverity | TSecondSeverity>;
export declare const operationToDefaultMessageSeverityMapping: MessageMapping<OperationSeverity, DefaultSeverity>;