mirror of
https://github.com/Kir-Antipov/mc-publish.git
synced 2024-11-22 08:20:58 -05:00
Updated logger instantiation logic
This commit is contained in:
parent
d043e7f23f
commit
a33f82afe1
2 changed files with 77 additions and 29 deletions
|
@ -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<string, string>): Logger {
|
||||
return isGitHubAction(env) ? PROCESS_LOGGER : CONSOLE_LOGGER;
|
||||
}
|
||||
|
|
15
tests/unit/utils/logging/logger.spec.ts
Normal file
15
tests/unit/utils/logging/logger.spec.ts
Normal file
|
@ -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);
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue