2022-12-16 04:15:44 -05:00
|
|
|
import { isGitHubAction } from "@/utils/environment";
|
|
|
|
import { ConsoleLogger } from "./console-logger";
|
|
|
|
import { NullLogger } from "./null-logger";
|
|
|
|
import { ProcessLogger } from "./process-logger";
|
2022-07-05 13:44:29 -04:00
|
|
|
|
2022-12-16 04:15:44 -05:00
|
|
|
/**
|
|
|
|
* Interface describing an object capable of logging user-provided information.
|
|
|
|
*/
|
|
|
|
export interface Logger {
|
|
|
|
/**
|
|
|
|
* Logs a message or error as fatal level log.
|
|
|
|
*
|
|
|
|
* @param message - The message or error to log.
|
|
|
|
*/
|
2022-07-05 13:44:29 -04:00
|
|
|
fatal(message: string | Error): void;
|
2022-12-16 04:15:44 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs a message or error as error level log.
|
|
|
|
*
|
|
|
|
* @param message - The message or error to log.
|
|
|
|
*/
|
2022-07-05 13:44:29 -04:00
|
|
|
error(message: string | Error): void;
|
2022-12-16 04:15:44 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs a message or error as warning level log.
|
|
|
|
*
|
|
|
|
* @param message - The message or error to log.
|
|
|
|
*/
|
2022-07-05 13:44:29 -04:00
|
|
|
warn(message: string | Error): void;
|
2022-12-16 04:15:44 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs a message or error as informational level log.
|
|
|
|
*
|
|
|
|
* @param message - The message or error to log.
|
|
|
|
*/
|
2022-07-05 13:44:29 -04:00
|
|
|
info(message: string | Error): void;
|
2022-12-16 04:15:44 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs a message or error as debug level log.
|
|
|
|
*
|
|
|
|
* @param message - The message or error to log.
|
|
|
|
*/
|
2022-07-05 13:44:29 -04:00
|
|
|
debug(message: string | Error): void;
|
|
|
|
}
|
2021-09-22 09:21:12 -04:00
|
|
|
|
2022-12-16 04:15:44 -05:00
|
|
|
/**
|
|
|
|
* A constant representing the {@link NullLogger} instance, which does not log any message.
|
|
|
|
*/
|
|
|
|
export const NULL_LOGGER: Logger = new NullLogger();
|
2021-09-22 09:21:12 -04:00
|
|
|
|
2022-12-16 04:15:44 -05:00
|
|
|
/**
|
|
|
|
* A constant representing the {@link ConsoleLogger} instance, which logs messages to the console.
|
|
|
|
*/
|
|
|
|
export const CONSOLE_LOGGER: Logger = new ConsoleLogger();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A constant representing the {@link ProcessLogger} instance, which dumps log messages to the `stdout`.
|
|
|
|
*/
|
|
|
|
export const PROCESS_LOGGER: Logger = new ProcessLogger();
|
2021-09-22 09:21:12 -04:00
|
|
|
|
2022-12-16 04:15:44 -05:00
|
|
|
/**
|
|
|
|
* Returns a logger instance that is the most suitable for the current environment.
|
|
|
|
*
|
|
|
|
* - If we are currently in a GitHub Actions environment, the logger will write to `process.stdout`.
|
|
|
|
* - Otherwise, logs will be written to the console.
|
|
|
|
*
|
|
|
|
* @param env - An optional set of the environment variables to check. Defaults to `process.env`.
|
|
|
|
*
|
|
|
|
* @returns A logger instance suitable for the current environment.
|
|
|
|
*/
|
|
|
|
export function getDefaultLogger(env?: Record<string, string>): Logger {
|
|
|
|
return isGitHubAction(env) ? PROCESS_LOGGER : CONSOLE_LOGGER;
|
2021-09-22 09:21:12 -04:00
|
|
|
}
|