smyld-lib-common
Version:
SMYLD Javascript Core Library, contains several core APIs that benefit the developers working on SPA applications
266 lines (265 loc) • 8.38 kB
TypeScript
import { LogMessage } from './LogMessage';
import { LogSettings, LogLevel } from './LogSettings';
/**
* @interface
* Configuration options for creating a Logger instance.
*/
interface LoggerConfig {
/**
* The source identifier for the logger.
* This will be displayed in log messages to identify which component generated the log.
*/
source: string;
/**
* The initial log level for the logger.
* If not provided, defaults to LogLevel.DEBUG for MainLogger and LogLevel.DEFAULT for other loggers.
* @see LogLevel
*/
logLevel?: LogLevel;
}
/**
* @class
* Core logging utility that provides methods for logging messages with different severity levels.
* Supports log caching, custom formatting, and integration with browser console.
*/
export declare class Logger {
/** Date format string for log timestamps */
dateFormat: string;
/** Date formatting options */
options: {
month: string;
day: string;
year: string;
};
/** Locale for date formatting */
locale: string;
/** Array to store cached log messages */
logs: any[];
/** Reference to the standard log function */
stdlog: Function;
/** Array to store console logs */
clogs: any[];
/** Source identifier for this logger */
source: string;
/** Current log level */
private _logLevel;
/** Abbriviated Source identifier for this logger */
sourceLog: string;
/**
* Creates a new Logger instance and registers it with the LogManager.
*
* @param {LoggerConfig} [params={ source: 'MainLogger', logLevel: LogLevel.DEBUG }] - Configuration options
*
* @example
* // Create the default MainLogger
* const mainLogger = new Logger();
*
* @example
* // Create a custom logger for a specific component
* const authLogger = new Logger({
* source: 'AuthService',
* logLevel: LogLevel.ERROR
* });
*/
constructor(params?: LoggerConfig);
/**
* Gets the current log level.
*
* @returns {LogLevel} The current log level
*/
get logLevel(): LogLevel;
/**
* Sets the log level.
*
* @param {LogLevel} value - The new log level
*/
set logLevel(value: LogLevel);
/**
* Creates a formatted date string for log timestamps.
*
* @private
* @returns {string} Formatted date string
*/
private createDate;
/**
* Initializes the source log format by abbreviating namespace parts.
* For namespaced sources (e.g. 'app.service.component'), abbreviates all parts except the last one.
* Example: 'app.service.component' becomes 'a.s.component'
*/
private initSourceLog;
/**
* Formats a namespaced source by abbreviating all parts except the last one.
* @param tokens - Array of namespace tokens
* @param lastToken - The last token to keep unchanged
* @returns Formatted source string
*/
private formatNamespacedSource;
/**
* Applies settings to the logger.
*
* @param {LogSettings} logSettings - The settings to apply
*
* @example
* logger.setLogSettings({
* cacheLogs: true,
* logLevel: LogLevel.DEBUG
* });
*/
setLogSettings(logSettings: LogSettings): void;
/**
* Saves a console message to the logs cache.
* This method is used internally by the console method overrides.
*
* @param {string} type - The type of log message (Log, Debug, Info, Error, Warn)
* @param {any} args - The arguments passed to the console method
* @param {Function} origFun - The original console function to call after caching
*/
doSaveMessage(type: string, args: any, origFun: Function): void;
/**
* Sets up log caching by overriding console methods.
* When enabled, all console logs will be captured in the logs array.
*
* @private
*/
private handleLogsCache;
/**
* Retrieves all cached log messages.
*
* @returns {any[]} Array of cached log messages
*
* @example
* const logs = logger.getCachedLogs();
* console.log(`Collected ${logs.length} log entries`);
*/
getCachedLogs(): any[];
/**
* Deletes all cached logs.
* This will clear the logs array and remove all stored log messages.
*
* @example
* logger.deleteCachedLogs();
*/
deleteCachedLogs(): void;
/**
* Retrieves cached logs as a Blob object for downloading or storage.
* Works in both browser and Node.js environments.
*
* @returns {Blob} A Blob containing all cached logs
*
* @example
* // In browser: Create a download link for logs
* const blob = logger.getCachedLogsAsBlob();
* const url = URL.createObjectURL(blob);
* const a = document.createElement('a');
* a.href = url;
* a.download = 'application-logs.txt';
* a.click();
*/
getCachedLogsAsBlob(): any;
/**
* Logs a message to the console with basic formatting.
*
* @param {any} text - The message to log
*/
log(text: any): void;
/**
* Logs an informational message.
* Only displayed if the current log level is INFO or higher.
*
* @param {any} text - The message or object to log
* @param {boolean} [compact=false] - Whether to format objects in compact mode
*
* @example
* logger.info("Operation completed successfully");
*
* @example
* // Log an object with pretty-printing
* logger.info({ user: "john", status: "active" });
*
* @example
* // Log an object in compact format
* logger.info({ user: "john", status: "active" }, true);
*/
info(text: any, compact?: boolean): void;
/**
* Logs an error message.
* Only displayed if the current log level is ERROR or higher.
*
* @param {any} text - The error message or object to log
* @param {boolean} [compact=false] - Whether to format objects in compact mode
*
* @example
* logger.error("Failed to connect to the server");
*
* @example
* // Log an error object
* try {
* // Some code that might throw
* } catch (error) {
* logger.error(error);
* }
*/
error(text: any, compact?: boolean): void;
/**
* Logs a warning message.
* Only displayed if the current log level is WARN or higher.
*
* @param {any} text - The warning message or object to log
* @param {boolean} [compact=false] - Whether to format objects in compact mode
*
* @example
* logger.warn("API rate limit approaching");
*/
warn(text: any, compact?: boolean): void;
/**
* Logs a debug message.
* Only displayed if the current log level is DEBUG or higher.
*
* @param {any} text - The debug message or object to log
* @param {boolean} [compact=false] - Whether to format objects in compact mode
*
* @example
* logger.debug("Variable state:", { count: 5, items: [...] });
*/
debug(text: any, compact?: boolean): void;
/**
* Legacy debug method.
* @deprecated Use debug() instead
* @private
*/
private debugOld;
/**
* Processes and displays a log message based on its type and the current log level.
*
* @param {LogMessage} msg - The log message to process
*/
logMessage(msg: LogMessage): void;
/**
* Determines the color to use for a log message based on its type.
*
* @private
* @param {LogMessage} msg - The log message
* @returns {string} The CSS color value
*/
private getMsgLogColor;
/**
* Composes a formatted log message string with styling placeholders.
*
* @private
* @param {LogMessage} msg - The log message to format
* @returns {string} Formatted message string with CSS style placeholders
*/
private composeLogMessage;
}
/**
* Default logger instance for the application.
* This is the main entry point for logging and is exported as the default export.
*
* @example
* import MainLogger from 'smyld-lib-common';
*
* MainLogger.info('Application started');
* MainLogger.debug('Debug information', { version: '1.0.0' });
*/
declare const MainLogger: Logger;
export default MainLogger;