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 { isGitHubAction } from "@/utils/environment";
|
||||||
import * as console from "console";
|
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;
|
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;
|
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;
|
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;
|
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;
|
debug(message: string | Error): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getDefaultLogger(): Logger {
|
/**
|
||||||
return {
|
* A constant representing the {@link NullLogger} instance, which does not log any message.
|
||||||
fatal: actions.setFailed,
|
*/
|
||||||
error: actions.warning,
|
export const NULL_LOGGER: Logger = new NullLogger();
|
||||||
warn: actions.warning,
|
|
||||||
info: actions.info,
|
|
||||||
debug: actions.debug
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getConsoleLogger(): Logger {
|
/**
|
||||||
return {
|
* A constant representing the {@link ConsoleLogger} instance, which logs messages to the console.
|
||||||
fatal: console.error,
|
*/
|
||||||
error: console.error,
|
export const CONSOLE_LOGGER: Logger = new ConsoleLogger();
|
||||||
warn: console.warn,
|
|
||||||
info: console.info,
|
|
||||||
debug: console.debug
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getEmptyLogger(): Logger {
|
/**
|
||||||
return {
|
* A constant representing the {@link ProcessLogger} instance, which dumps log messages to the `stdout`.
|
||||||
fatal: () => {},
|
*/
|
||||||
error: () => {},
|
export const PROCESS_LOGGER: Logger = new ProcessLogger();
|
||||||
warn: () => {},
|
|
||||||
info: () => {},
|
/**
|
||||||
debug: () => {}
|
* 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