@pbrgld/loggify
Version:
A lightweight, dependency-free logging library for Bun.js and Node.js – featuring emoji support, styled output, contextual logging, and high-performance stdout logging.
152 lines (151 loc) • 7.87 kB
TypeScript
import type { LogType, LogConsoleOptions, LogLevel, GrafanaLoki, GrafanaLokiEntry, GrafanaLokiLabels, ConstructorOptions, ObjectSizeResponse, FlushOptions, BannerOptions, BannerContent } from './types';
/**
* Todo section
*/
/**
* Console class
*/
export default class Loggify {
private logLevel;
private logTimestamp;
private logTimestampType;
private logTypeBadge;
private logCallerInformation;
private logCallerCallStackLevel;
private logMemory;
private logBuffer;
private grafanaLoki;
private ansi;
private emoji;
/**
* Initializing the log class to be used over console.log, as it should hopefully be faster and less resource
* demanding, while supporting quite some nice features, such as ANSI coloring and styling without requiring third
* party packages.
* @param {ConstructorOptions} options Constructor Options
*/
constructor(options?: ConstructorOptions);
/** Logs the current initialization and setup of Loggify */
logInit(): void;
/** Method to set/change the log level */
setLogLevel(logLevel: LogLevel): boolean;
/** Initialize Grafana Loki */
grafanaLokiInit(options: GrafanaLoki): void;
/**
* Build Authorization Header Value for GrafanaLoki REST API requests
* This is done based on the auth initialozation of the GrafanaLoki configuration. Will either use none/empty value,
* basic auth - using user name and password or bearer token
* @returns {String} Returns either empty string for none, basic or bearer token value string for HTTP request header
*/
grafanaLokiAuthorizationHeader(): string;
/**
* Test connection to Grafana Loki will test the server and port settings provided as well as user and password or
* bearer token. In case of success the endpoint will provide some information such as version and revision into
* the server info object and the function returns TRUE in case of success and FALSE in case of an error
* @returns {Boolean}
*/
grafanaLokiTestConnection(): Promise<boolean>;
/**
* Pushes stream with a set of labels and a set of log records to the connected GrafanaLoki Server. Will not do anythins when there is no tested connection is class property
* @param {GrafanaLokiEntry} entries Array of records you want to log. TS will be automatically set if not provided, must be in nanoseconds. Message can be either a string or an object which will be stringified and can be parsed by GrafanaLoki later
* @param {GrafanaLokiLabels} labels A set of labels defining the stream combining the global labels set during GrafanaLoki init and this method. NOTE: Globals will overwrite locals
* @param {any} options Options - not yet implemented
* @returns {Boolean} In case of SUCCESS will return TRUE otherwise will return FALSE
*/
grafanaLokiPush(entries: Array<GrafanaLokiEntry>, labels?: GrafanaLokiLabels, options?: any): Promise<boolean>;
/**
* Console output
* Based on the log level and the type lines will be added to the console
* @param {String} message Message you want to display - can contain ANSI colors and styling
* @param {String} type Type of styling to be applied to output (e.g. info, error, success, debug, etc ...)
* @param {Object} object Along with the message provide an object to be displayed within the output
* @param {Object} options Set of options to be applied to the output (e.g. timestamp: true)
*/
console(message?: any, type?: LogType, options?: LogConsoleOptions, object?: any): false | undefined;
banner(content: string | BannerContent, options?: BannerOptions): void;
/**
* A function that uses the trick to throw an error to access the call stack and extract the levels of them
* @param {number} depth Put in the number of the level you want to scan
* @returns {Object} Information from where the function has been called, such as path to file and line
*/
getCallerLocation(depth?: number): {
function: string | undefined;
file: string | undefined;
line: number;
column: number;
} | {
function: null;
file: string | undefined;
line: number;
column: number;
} | null;
/**
* Flatten object is required to make to logging of object work with certain outputs.
* @param {Object} obj Object to be flattened
* @returns {Object} flattened object
*/
flattenObject(obj: any): Record<string, any>;
/**
* Generates a unique context ID to be used grouping functions and parallel executions together
* @returns {String} Unique context ID
*/
generateContextId(): string;
/**
* Flush specific context from log buffer will dump a specific log record set to the standard output and remove it
* from log buffer. When a context ID is not found or does not exist anymore the function will log an error and
* return FALSE, welse it will print the logs and return TRUE.
* @param {String} contextId Unique ID to identify the context area that holds all relevant logs
* @param {FlushOptions} options Options like discarding the logs and not render to console
* @returns {Boolean} Returns FALSE when context ID cannot found in log buffer, otherwise will return TRUE
*/
flush(contextId: string, options?: FlushOptions): boolean;
/**
* Replace ANSI style commands to ANSI CODE in String
* @param {String} str String to be searched for ANSI commands and replaced into ANSI code
* @returns {String} String with ANSI characters
*/
replaceAnsi(str: string): string;
/**
* Strip all ANSI code from String and return a clean string
* @param {String} str Provide a string that may or may not have ANSI code in it
* @returns {String} Receive a string stripped by ANSI code
*/
stripAnsi(str: string): string;
/**
* Replace emoji code to actual emojis in string
* @param {String} str String to be searched for emoji commands and replace them with actual emojis.
* @returns {String} String with actual emoji icons
*/
replaceEmojis(str: string): string;
/**
* ANSI Safe version of padEnd() Function
* @param {String} str Message or label you want to handle
* @param {Number} targetLength The target length of chars you want to stretch the label to
* @param {String} padChar The character you want to use filling up the space
* @param {Boolean} center If TRUE, the label will be displayed in center and the ANSI style of the last field will be applied to the front of str filling chars
* @returns
*/
padEndAnsiSafe(str: string | undefined, targetLength: number, padChar?: string, center?: boolean): string;
/**
* Fast way to determine the current date based on the current plattform's time zone. Not using UTC!
* @returns {String} Local date in current time zone
*/
getLocalDateString(): string;
/**
* Fast way to determine the current time based on the current plattform's time zone. Not using UTC!
* @returns {String} Local time in current time zone
*/
getLocalTimeString(): string;
/**
* Converts miliseconds (ms) into human readable time
* @param {Number} ms Provide the miliseconds you want to convert into human readabe time
* @returns {String} Returns human readable string
*/
formatDuration(ms: number): string;
/**
* Converts object into string and than calculates the size
* @param {Object} obj Object you want to calculate the size of
* @returns {String} Returns a size formatted string of the object
*/
getObjectSize(obj: any): ObjectSizeResponse;
splitTextByNewLineLength(text: string, maxLength: number, wordWrap?: boolean): Array<string>;
}