UNPKG

@megaorm/logger

Version:

This package is designed for managing log files, with support for logging messages, retrieving entries, filtering by date, and more.

108 lines (107 loc) 3.63 kB
/** * Error class for logger-related issues. */ export declare class LoggerError extends Error { } /** * Log entry with a date and message. */ export type Log = { date: string; message: string; }; /** * Array of log entries. */ export type Logs = Array<Log>; /** * Interface representing the available getter methods for the logger. */ export interface Getter { /** * Returns the path of the log file. */ path: () => string; /** * Retrieves all log entries as an array of objects with date and message. * @returns A promise that resolves to an array of log entries. * @throws Throws an error if there is an issue reading the log file. */ logs: () => Promise<Logs>; /** * Retrieves all log messages as an array of strings. * @returns A promise that resolves to an array of log messages. * @throws Throws an error if there is an issue reading the log file. */ messages: () => Promise<Array<string>>; /** * Retrieves log messages from a specified date onward. * @param date - The date from which to retrieve log messages (format: 'YYYY-MM-DD hh:mm:ss'). * @returns A promise that resolves to an array of log messages from the specified date. * @throws Throws an error if there is an issue reading the log file. */ from: (date: string) => Promise<Array<string>>; } /** * A simple logger class for logging messages to a log file. * * This logger class is designed for applications that need to store log messages in a file, * with each log message automatically timestamped in UTC format. * * @example * // Create a logger instance with the path to the log file * const logger = new Logger('/path/to/log/file.log'); * * // Log a message * await logger.log('This is a simple log message'); * * // Retrieve all logs * const logs = await logger.get.logs(); * console.log(logs); // Output: [{ date: '2024-10-12 12:34:56', message: 'This is a simple log message' }] * * // Retrieve log messages only * const logs = await logger.get.messages(); * console.log(logs); // Output: ['This is a simple log message'] * * // Retrieve log messages from a spesific date * const logs = await logger.get.from('2024-10-12 00:00:00'); * console.log(logs); // Output: ['This is a simple log message'] * * // Clear all logs * await logger.clear(); * * @note * - Logs are timestamped in UTC timezone by default. * - Log entries are stored in the format `[YYYY-MM-DD hh:mm:ss] message`. * - The log file will automatically be created if it does not exist. * * @throws Throws a `LoggerError` if any file operation fails or if invalid input is provided. */ export declare class Logger { /** @private The file path where logs are stored */ private path; /** * Creates an logger new instance * @param path - The path to the log file. */ constructor(path: string); /** * Provides access to various getter methods related to the logger. */ get: Getter; /** * Logs a message to the log file. * * @param message The message to be logged. Must be a valid string. * @returns A promise that resolves when the message is successfully logged. * @throws `LoggerError` If the message is not a string or the log path is not set. */ log(message: string): Promise<void>; /** * Clears all log messages in the log file. * * @returns A promise that resolves when the log file is successfully cleared. * @throws `LoggerError` if there is an error during the file write operation. */ clear(): Promise<void>; }