@sap-cloud-sdk/util
Version:
SAP Cloud SDK for JavaScript general utilities
144 lines (143 loc) • 6.87 kB
TypeScript
import type { Format } from 'logform';
import type { Logger } from 'winston';
import type TransportStream from 'winston-transport';
/**
* Log formats provided by the util package.
*/
export declare const logFormat: {
kibana: Format;
local: Format;
};
/**
* Mute all logger output created by the SAP Cloud SDK Logger. This also applies to future loggers created. Useful for tests.
*/
export declare function muteLoggers(): void;
/**
* Unmute all logger output created by the SAP Cloud SDK Logger. This also applies to future loggers created. Useful for tests.
*/
export declare function unmuteLoggers(): void;
/**
* Default logger for the SAP Cloud SDK for unhandled exceptions.
*/
export declare const cloudSdkExceptionLogger: Logger;
/**
* Disable logging of exceptions. Enabled by default.
*/
export declare function disableExceptionLogger(): void;
/**
* Enable logging of exceptions. Enabled by default.
*/
export declare function enableExceptionLogger(): void;
/**
* Create a logger for the given message context, if available.
*
* Usage:
* To create a logger in your module, it is recommended to pass a module identifier that will be logged as `messageContext` for all messages from this logger:
* `const logger = createLogger('my-module');`. Not setting any module identifier will retrieve the default logger.
* Use this logger throughout your module. If the module is spread over multiple files, you can retrieve the logger instance by calling the `createLogger` function with the respective module identifier.
* There will always be only one instance of a logger per module identifier.
* You can pass any custom data that you want to be logged in addition by passing an object instead. You can change the default logging level (`INFO`) using the `level` key in the object.
* In those cases, provide the `messageContext` as a key in the object:
* ```
* const logger = createLogger({
* messageContext: 'my-module',
* myCustomKey: 'my-custom-data',
* level: 'debug'
* });
* ```
* You will find these information under the _custom_fields_ key in your Cloud Foundry logs.
*
* To retrieve a logger after its creation use {@link getLogger}.
* If you want to change the log level of a logger use {@link setLogLevel}.
* @param messageContext - Either a key for the message context of all messages produced by the logger or an object with additional keys to set in the message.
* @returns A newly created or an already existing logger for the given context.
*/
export declare function createLogger(messageContext?: string | (MessageContextObj & LoggerOptions)): Logger;
/**
* Get logger for a given message context, if available.
* @param messageContext - A key for the message context of all messages produced by the logger.
* @returns The logger for the given messageContext if it was created before.
*/
export declare function getLogger(messageContext?: string): Logger | undefined;
/**
* Change the log level of a logger based on its message context.
* e.g., to set the log level for the destination accessor module of the SDK to _debug_, simply call `setLogLevel('debug', 'destination-accessor')`.
* @param level - Level to set the logger to. Use an empty string '' as level to unset context level.
* @param messageContextOrLogger - Message context of the logger to change the log level for or the logger itself.
*/
export declare function setLogLevel(level: LogLevel | '', messageContextOrLogger?: string | Logger): void;
/**
* Change the global log level of the container which will set default level for all active loggers.
* e.g., to set the global log level call `setGlobalLogLevel('debug')`.
* @param level - The log level to set the global log level to.
*/
export declare function setGlobalLogLevel(level: LogLevel): void;
/**
* Get the global log level of the container.
* @returns The global log level, or `undefined` when not defined.
*/
export declare function getGlobalLogLevel(): string | undefined;
/**
* Change the global transport of the container which will set default transport for all active loggers.
* e.g., to set the global transport call `setGlobalTransports(httpTransport)`.
* @param customTransports - The transport to set the global transport to. Both single transport and an array with multiple transports are supported.
*/
export declare function setGlobalTransports(customTransports: TransportStream | TransportStream[]): void;
/**
* Change the log format of a logger based on its message context.
* e.g., to set the log format for the destination accessor module of the SDK to `local`, simply call `setLogFormat(logFormat.local, 'destination-accessor')`.
* @param format - Format to set the logger to. Use `logFormat` to get the pre-defined log formats or use a custom log format.
* @param messageContextOrLogger - Message context of the logger to change the log level for or the logger itself.
*/
export declare function setLogFormat(format: Format, messageContextOrLogger?: string | Logger): void;
/**
* Change the global log format of the container which will set default format for all active loggers.
* e.g., to set the global log format to `local` call `setGlobalLogLevel(logFormat.local)` or use a custom log format.
* @param format - The log format to set the global log format to.
*/
export declare function setGlobalLogFormat(format: Format): void;
/**
* Get the global log format of the container.
* @returns The global log format, or `undefined` when not defined.
*/
export declare function getGlobalLogFormat(): Format | undefined;
/**
* Potentially sensitive keys will be matched case-insensitive and as substrings.
* Matches will be replaced with a placeholder string.
* @param input - The record to be sanitized.
* @param replacementString - The placeholder string.
* @param sensitiveKeys - The list of keys to be replaced. This overrides the default list.
* @returns The sanitized copy of the input record.
*/
export declare function sanitizeRecord<T = any>(input: Record<string, T>, replacementString?: string, sensitiveKeys?: string[]): Record<string, T>;
/**
* Reset all the custom log levels for loggers and message context.
*/
export declare function resetCustomLogLevels(): void;
/**
* Reset all the custom log formats for loggers and message context.
*/
export declare function resetCustomLogFormats(): void;
/**
* Npm log levels used for the SAP Cloud SDK logger.
*/
export type LogLevel = 'error' | 'warn' | 'info' | 'verbose' | 'debug' | 'silly';
/**
* Configurable logger options.
*/
export interface LoggerOptions {
/**
* The log level of the logger.
*/
level?: LogLevel;
}
/**
* Log message context for a logger with additional custom data.
*/
export interface MessageContextObj {
/**
* Name of the message context.
*/
messageContext?: string;
[key: string]: any;
}