darkness-dependent-vision/src/logger.js

32 lines
868 B
JavaScript
Raw Normal View History

2021-06-29 05:27:27 +02:00
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
2021-06-29 20:40:54 +02:00
const loggingContext = 'Darkness Dependent Vision';
2021-06-29 05:27:27 +02:00
const loggingSeparator = '|';
/**
2021-06-29 20:09:34 +02:00
* Get a logging function for the requested log level.
* @typedef {'debug' | 'info' | 'warn' | 'error'} LogLevel
2021-06-29 05:27:27 +02:00
* @typedef {(...args: unknown[]) => void} LoggingFunction
* @param {LogLevel} [type = 'info'] - The log level of the requested logger
* @returns {LoggingFunction}
*/
function getLoggingFunction(type = 'info') {
2021-06-29 20:09:34 +02:00
const log = console[type];
2021-06-29 05:27:27 +02:00
return (...data) => log(loggingContext, loggingSeparator, ...data);
}
2021-06-29 20:09:34 +02:00
/**
* A singleton logger object.
*/
2021-06-29 05:27:27 +02:00
const logger = Object.freeze({
debug: getLoggingFunction('debug'),
info: getLoggingFunction('info'),
warn: getLoggingFunction('warn'),
error: getLoggingFunction('error'),
getLoggingFunction,
});
export default logger;