diff --git a/src/utils/logging/logger.ts b/src/utils/logging/logger.ts index c7c65fd..a1cc3b8 100644 --- a/src/utils/logging/logger.ts +++ b/src/utils/logging/logger.ts @@ -1,40 +1,73 @@ -import * as actions from "@actions/core"; -import * as console from "console"; +import { isGitHubAction } from "@/utils/environment"; +import { ConsoleLogger } from "./console-logger"; +import { NullLogger } from "./null-logger"; +import { ProcessLogger } from "./process-logger"; -export default interface Logger { +/** + * 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. + */ fatal(message: string | Error): void; + + /** + * Logs a message or error as error level log. + * + * @param message - The message or error to log. + */ error(message: string | Error): void; + + /** + * Logs a message or error as warning level log. + * + * @param message - The message or error to log. + */ warn(message: string | Error): void; + + /** + * Logs a message or error as informational level log. + * + * @param message - The message or error to log. + */ info(message: string | Error): void; + + /** + * Logs a message or error as debug level log. + * + * @param message - The message or error to log. + */ debug(message: string | Error): void; } -export function getDefaultLogger(): Logger { - return { - fatal: actions.setFailed, - error: actions.warning, - warn: actions.warning, - info: actions.info, - debug: actions.debug - }; -} +/** + * A constant representing the {@link NullLogger} instance, which does not log any message. + */ +export const NULL_LOGGER: Logger = new NullLogger(); -export function getConsoleLogger(): Logger { - return { - fatal: console.error, - error: console.error, - warn: console.warn, - info: console.info, - debug: console.debug - }; -} +/** + * A constant representing the {@link ConsoleLogger} instance, which logs messages to the console. + */ +export const CONSOLE_LOGGER: Logger = new ConsoleLogger(); -export function getEmptyLogger(): Logger { - return { - fatal: () => {}, - error: () => {}, - warn: () => {}, - info: () => {}, - debug: () => {} - }; +/** + * A constant representing the {@link ProcessLogger} instance, which dumps log messages to the `stdout`. + */ +export const PROCESS_LOGGER: Logger = new ProcessLogger(); + +/** + * 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): Logger { + return isGitHubAction(env) ? PROCESS_LOGGER : CONSOLE_LOGGER; } diff --git a/tests/unit/utils/logging/logger.spec.ts b/tests/unit/utils/logging/logger.spec.ts new file mode 100644 index 0000000..349a5ab --- /dev/null +++ b/tests/unit/utils/logging/logger.spec.ts @@ -0,0 +1,15 @@ +import { + CONSOLE_LOGGER, + PROCESS_LOGGER, + getDefaultLogger, +} from "@/utils/logging/logger"; + +describe("getDefaultLogger", () => { + test("returns PROCESS_LOGGER if we are in a GitHub Actions environment", () => { + expect(getDefaultLogger({ GITHUB_ACTIONS: "true" })).toBe(PROCESS_LOGGER); + }); + + test("returns CONSOLE_LOGGER if we are not in a GitHub Actions environment", () => { + expect(getDefaultLogger({})).toBe(CONSOLE_LOGGER); + }); +});