@graphql-markdown/logger
Advanced tools
Comparing version 1.0.0-next.0 to 1.0.0
@@ -11,23 +11,49 @@ /** | ||
/** | ||
* Returns a logger module, if a package name is provided then instantiates it. | ||
* Log levels. | ||
* | ||
*/ | ||
export declare enum LogLevel { | ||
debug = "debug", | ||
error = "error", | ||
info = "info", | ||
log = "log", | ||
success = "success", | ||
warn = "warn" | ||
} | ||
/** | ||
* Instantiate a logger module. | ||
* By default, the logger module uses `global.console` | ||
* | ||
* @internal | ||
* | ||
* @param moduleName - optional name of the logger package. | ||
* | ||
* @returns an instance of the logger. | ||
* | ||
* @example | ||
* ```js | ||
* import { Logger } from "@graphql-markdown/utils/logger"; | ||
* import { Logger, log } from "@graphql-markdown/utils/logger"; | ||
* | ||
* Logger().info("Info message"); // Expected console output "Info message" | ||
* log("Info message"); // Expected console output "Info message" | ||
* | ||
* const loggerModule = require.resolve("@docusaurus/logger"); | ||
* Logger(loggerModule).info("Info message"); // Expected Docusaurus log output "Info message" | ||
* Logger("@docusaurus/logger"); | ||
* log("Info message", "info"); // Expected Docusaurus log output "Info message" | ||
* ``` | ||
* | ||
*/ | ||
export declare const Logger: (moduleName?: string) => LoggerType; | ||
export declare const Logger: (moduleName?: string) => void; | ||
/** | ||
* Logs a message by calling the active logger instance. | ||
* | ||
* @remarks | ||
* If a log level is not supported by the logger instance, then it defaults to `"info"`. | ||
* | ||
* @param message - a string to be logged. | ||
* @param level - optional log level, `"info"` by default. | ||
* | ||
* @example | ||
* ```js | ||
* import { log } from "@graphql-markdown/utils/logger"; | ||
* | ||
* log("Info message"); // Expected console output "Info message" | ||
* ``` | ||
* | ||
*/ | ||
export declare const log: (message: string, level?: LogLevel | keyof typeof LogLevel) => void; | ||
export default Logger; |
@@ -8,21 +8,30 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Logger = void 0; | ||
exports.log = exports.Logger = exports.LogLevel = void 0; | ||
/** | ||
* Returns a logger module, if a package name is provided then instantiates it. | ||
* Log levels. | ||
* | ||
*/ | ||
var LogLevel; | ||
(function (LogLevel) { | ||
LogLevel["debug"] = "debug"; | ||
LogLevel["error"] = "error"; | ||
LogLevel["info"] = "info"; | ||
LogLevel["log"] = "log"; | ||
LogLevel["success"] = "success"; | ||
LogLevel["warn"] = "warn"; | ||
})(LogLevel || (exports.LogLevel = LogLevel = {})); | ||
/** | ||
* Instantiate a logger module. | ||
* By default, the logger module uses `global.console` | ||
* | ||
* @internal | ||
* | ||
* @param moduleName - optional name of the logger package. | ||
* | ||
* @returns an instance of the logger. | ||
* | ||
* @example | ||
* ```js | ||
* import { Logger } from "@graphql-markdown/utils/logger"; | ||
* import { Logger, log } from "@graphql-markdown/utils/logger"; | ||
* | ||
* Logger().info("Info message"); // Expected console output "Info message" | ||
* log("Info message"); // Expected console output "Info message" | ||
* | ||
* const loggerModule = require.resolve("@docusaurus/logger"); | ||
* Logger(loggerModule).info("Info message"); // Expected Docusaurus log output "Info message" | ||
* Logger("@docusaurus/logger"); | ||
* log("Info message", "info"); // Expected Docusaurus log output "Info message" | ||
* ``` | ||
@@ -32,29 +41,38 @@ * | ||
const Logger = (moduleName) => { | ||
if (global.logger && typeof moduleName === "undefined") { | ||
return global.logger; | ||
if (global.logger?.instance && typeof moduleName === "undefined") { | ||
return; | ||
} | ||
if (typeof moduleName !== "string" || moduleName === "") { | ||
global.logger = global.console; | ||
} | ||
else { | ||
global.logger = require(moduleName); | ||
} | ||
if (typeof global.logger.log === "undefined") { | ||
global.logger.log = (...args) => global.logger.info.apply(null, args); | ||
} | ||
if (typeof global.logger.success === "undefined") { | ||
global.logger.success = (...args) => global.logger.info.apply(null, args); | ||
} | ||
if (typeof global.logger.debug === "undefined") { | ||
global.logger.debug = (...args) => global.logger.info.apply(null, args); | ||
} | ||
if (typeof global.logger.warn === "undefined") { | ||
global.logger.warn = (...args) => global.logger.info.apply(null, args); | ||
} | ||
if (typeof global.logger.error === "undefined") { | ||
global.logger.error = (...args) => global.logger.info.apply(null, args); | ||
} | ||
return global.logger; | ||
const instance = typeof moduleName === "string" && moduleName !== "" | ||
? require(moduleName) | ||
: global.console; | ||
const _log = (message, level = LogLevel.info) => { | ||
const fallback = instance[LogLevel.info]; | ||
const callback = typeof instance[level] === "function" ? instance[level] : fallback; | ||
callback?.apply(this, [message]); | ||
}; | ||
global.logger = { instance, _log }; | ||
}; | ||
exports.Logger = Logger; | ||
/** | ||
* Logs a message by calling the active logger instance. | ||
* | ||
* @remarks | ||
* If a log level is not supported by the logger instance, then it defaults to `"info"`. | ||
* | ||
* @param message - a string to be logged. | ||
* @param level - optional log level, `"info"` by default. | ||
* | ||
* @example | ||
* ```js | ||
* import { log } from "@graphql-markdown/utils/logger"; | ||
* | ||
* log("Info message"); // Expected console output "Info message" | ||
* ``` | ||
* | ||
*/ | ||
const log = (message, level = LogLevel.info) => { | ||
(0, exports.Logger)(); | ||
global.logger?._log(message, level); | ||
}; | ||
exports.log = log; | ||
exports.default = exports.Logger; |
@@ -8,7 +8,7 @@ { | ||
}, | ||
"version": "1.0.0-next.0", | ||
"version": "1.0.0", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "graphql-markdown/graphql-markdown", | ||
"url": "git+https://github.com/graphql-markdown/graphql-markdown.git", | ||
"directory": "packages/logger" | ||
@@ -46,3 +46,3 @@ }, | ||
"devDependencies": { | ||
"@graphql-markdown/types": "^1.0.0-next.0" | ||
"@graphql-markdown/types": "^1.0.0" | ||
}, | ||
@@ -49,0 +49,0 @@ "directories": { |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
6386
134
0