@sonatel-os/juf-xpress-logger-edge
Version:
JUF XPress logger for Node Edge
89 lines (88 loc) • 4.89 kB
TypeScript
/**
* LoggerService
* A service class for logging requests, responses, and custom events.
* Provides mechanisms to sanitize logs, manage log destinations, and interact with observability tools.
* @class
*/
export class LoggerService {
/**
* Constructs an instance of LoggerService with the specified options.
*
* @constructor
* @param {Object} options - Options for initializing the logger service.
* @param {string} [options.appName='app'] - Application name for log identification.
* @param {Array<string>} [options.crypt=[]] - List of keys to mask in the log data.
* @param {boolean} [options.logConsole=true] - Whether to log to the console.
* @param {string} [options.remoteUrl] - URL for sending logs to a remote server.
* @param {boolean} [options.sendToRemote=false] - Whether to send logs to a remote server.
* @param {string} [options.logLevel='info'] - Log level for APM configuration.
* @param {boolean} [options.startApmAgent=false] - Whether to start the APM agent.
* @throws {Error} Throws an error if APM or logger initialization fails.
*/
constructor({ appName, crypt, logLevel, startApmAgent }: {
appName?: string | undefined;
crypt?: string[] | undefined;
logConsole?: boolean | undefined;
remoteUrl?: string | undefined;
sendToRemote?: boolean | undefined;
logLevel?: string | undefined;
startApmAgent?: boolean | undefined;
});
/**
* Factory method to bootstrap and initialize a new instance of LoggerService with the provided configuration.
*
* @param {Object} config - Configuration options for LoggerService.
* @param {Array<string>} [config.crypt=[]] - List of keys to mask in the log data for sensitive information.
* @param {boolean} [config.logConsole=true] - Determines whether to log to the console.
* @param {string} [config.logLevel='info'] - Log level for both console and APM configuration ('info', 'warn', 'error', etc.).
* @param {boolean} [config.startApmAgent=false] - Indicates whether to start the APM agent for observability.
* @returns {LoggerService} An initialized instance of LoggerService.
* @throws {Error} Throws an error if the provided configuration is invalid.
*/
bootstrap({ crypt, logConsole, logLevel, startApmAgent }: {
crypt?: string[] | undefined;
logConsole?: boolean | undefined;
logLevel?: string | undefined;
startApmAgent?: boolean | undefined;
}): LoggerService;
/**
* Writes a log entry using the configured logger instance.
* Supports logging to the console or sending to a remote server.
* Handles structured logging with sensitive data masking and optional duration tracking.
*
* @param {Object} options - Options for logging.
* @param {Object} options.params - Log parameters including request and response details.
* @param {string} options.params.logFrom - The origin IP address of the log entry.
* @param {string} options.params.userIp - The user's IP address for the log entry.
* @param {string} options.params.method - The HTTP method of the request (e.g., 'GET', 'POST').
* @param {Object|string} options.params.payload - The request body content, can be an object or a stringified JSON.
* @param {Object|string} options.params.headers - The request headers, can be an object or a stringified JSON.
* @param {string} options.params.logTarget - The target URL of the log entry.
* @param {string} options.params.userAgent - The User-Agent header string.
* @param {number} options.params.logStatus - The HTTP status code of the response.
* @param {string} options.params.logStatusCode - The HTTP status message (e.g., 'OK', 'Not Found').
* @param {string} [options.userName='anonymousUser'] - The username associated with the log entry.
* @param {string} [options.logLevel='INFO'] - The log level (e.g., 'INFO', 'ERROR', 'WARN').
* @param {string} options.action - A brief description of the action being logged.
* @param {number} [options.duration] - Optional duration in milliseconds for how long the action took to complete.
* @throws {Error} Throws an error if logging fails due to invalid input or processing errors.
*/
writeLog({ params, userName, logLevel, action, duration }: {
params: {
logFrom: string;
userIp: string;
method: string;
payload: Object | string;
headers: Object | string;
logTarget: string;
userAgent: string;
logStatus: number;
logStatusCode: string;
};
userName?: string | undefined;
logLevel?: string | undefined;
action: string;
duration?: number | undefined;
}): void;
#private;
}