@graphql-markdown/logger 1.0.0-next.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -8,25 +8,51 @@ declare global {
8
8
  var logger: Maybe<LoggerType>;
9
9
  }
10
10
  /**
11
- * Returns a logger module, if a package name is provided then instantiates it.
12
- * By default, the logger module uses `global.console`
11
+ * Log levels.
13
12
  *
14
- * @internal
13
+ */
14
+ export declare enum LogLevel {
15
+ debug = "debug",
16
+ error = "error",
17
+ info = "info",
18
+ log = "log",
19
+ success = "success",
20
+ warn = "warn"
21
+ }
22
+ /**
23
+ * Instantiate a logger module.
24
+ * By default, the logger module uses `global.console`
15
25
  *
16
26
  * @param moduleName - optional name of the logger package.
17
27
  *
18
- * @returns an instance of the logger.
19
- *
20
28
  * @example
21
29
  * ```js
22
- * import { Logger } from "@graphql-markdown/utils/logger";
30
+ * import { Logger, log } from "@graphql-markdown/utils/logger";
31
+ *
32
+ * log("Info message"); // Expected console output "Info message"
33
+ *
34
+ * Logger("@docusaurus/logger");
35
+ * log("Info message", "info"); // Expected Docusaurus log output "Info message"
36
+ * ```
37
+ *
38
+ */
39
+ export declare const Logger: (moduleName?: string) => void;
40
+ /**
41
+ * Logs a message by calling the active logger instance.
23
42
  *
24
- * Logger().info("Info message"); // Expected console output "Info message"
43
+ * @remarks
44
+ * If a log level is not supported by the logger instance, then it defaults to `"info"`.
45
+ *
46
+ * @param message - a string to be logged.
47
+ * @param level - optional log level, `"info"` by default.
48
+ *
49
+ * @example
50
+ * ```js
51
+ * import { log } from "@graphql-markdown/utils/logger";
25
52
  *
26
- * const loggerModule = require.resolve("@docusaurus/logger");
27
- * Logger(loggerModule).info("Info message"); // Expected Docusaurus log output "Info message"
53
+ * log("Info message"); // Expected console output "Info message"
28
54
  * ```
29
55
  *
30
56
  */
31
- export declare const Logger: (moduleName?: string) => LoggerType;
57
+ export declare const log: (message: string, level?: LogLevel | keyof typeof LogLevel) => void;
32
58
  export default Logger;
package/dist/index.js CHANGED
@@ -5,54 +5,72 @@
5
5
  * @packageDocumentation
6
6
  */
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
- exports.Logger = void 0;
8
+ exports.log = exports.Logger = exports.LogLevel = void 0;
9
9
  /**
10
- * Returns a logger module, if a package name is provided then instantiates it.
11
- * By default, the logger module uses `global.console`
10
+ * Log levels.
12
11
  *
13
- * @internal
12
+ */
13
+ var LogLevel;
14
+ (function (LogLevel) {
15
+ LogLevel["debug"] = "debug";
16
+ LogLevel["error"] = "error";
17
+ LogLevel["info"] = "info";
18
+ LogLevel["log"] = "log";
19
+ LogLevel["success"] = "success";
20
+ LogLevel["warn"] = "warn";
21
+ })(LogLevel || (exports.LogLevel = LogLevel = {}));
22
+ /**
23
+ * Instantiate a logger module.
24
+ * By default, the logger module uses `global.console`
14
25
  *
15
26
  * @param moduleName - optional name of the logger package.
16
27
  *
17
- * @returns an instance of the logger.
18
- *
19
28
  * @example
20
29
  * ```js
21
- * import { Logger } from "@graphql-markdown/utils/logger";
30
+ * import { Logger, log } from "@graphql-markdown/utils/logger";
22
31
  *
23
- * Logger().info("Info message"); // Expected console output "Info message"
32
+ * log("Info message"); // Expected console output "Info message"
24
33
  *
25
- * const loggerModule = require.resolve("@docusaurus/logger");
26
- * Logger(loggerModule).info("Info message"); // Expected Docusaurus log output "Info message"
34
+ * Logger("@docusaurus/logger");
35
+ * log("Info message", "info"); // Expected Docusaurus log output "Info message"
27
36
  * ```
28
37
  *
29
38
  */
30
39
  const Logger = (moduleName) => {
31
- if (global.logger && typeof moduleName === "undefined") {
32
- return global.logger;
33
- }
34
- if (typeof moduleName !== "string" || moduleName === "") {
35
- global.logger = global.console;
36
- }
37
- else {
38
- global.logger = require(moduleName);
39
- }
40
- if (typeof global.logger.log === "undefined") {
41
- global.logger.log = (...args) => global.logger.info.apply(null, args);
42
- }
43
- if (typeof global.logger.success === "undefined") {
44
- global.logger.success = (...args) => global.logger.info.apply(null, args);
45
- }
46
- if (typeof global.logger.debug === "undefined") {
47
- global.logger.debug = (...args) => global.logger.info.apply(null, args);
48
- }
49
- if (typeof global.logger.warn === "undefined") {
50
- global.logger.warn = (...args) => global.logger.info.apply(null, args);
51
- }
52
- if (typeof global.logger.error === "undefined") {
53
- global.logger.error = (...args) => global.logger.info.apply(null, args);
40
+ if (global.logger?.instance && typeof moduleName === "undefined") {
41
+ return;
54
42
  }
55
- return global.logger;
43
+ const instance = typeof moduleName === "string" && moduleName !== ""
44
+ ? require(moduleName)
45
+ : global.console;
46
+ const _log = (message, level = LogLevel.info) => {
47
+ const fallback = instance[LogLevel.info];
48
+ const callback = typeof instance[level] === "function" ? instance[level] : fallback;
49
+ callback?.apply(this, [message]);
50
+ };
51
+ global.logger = { instance, _log };
56
52
  };
57
53
  exports.Logger = Logger;
54
+ /**
55
+ * Logs a message by calling the active logger instance.
56
+ *
57
+ * @remarks
58
+ * If a log level is not supported by the logger instance, then it defaults to `"info"`.
59
+ *
60
+ * @param message - a string to be logged.
61
+ * @param level - optional log level, `"info"` by default.
62
+ *
63
+ * @example
64
+ * ```js
65
+ * import { log } from "@graphql-markdown/utils/logger";
66
+ *
67
+ * log("Info message"); // Expected console output "Info message"
68
+ * ```
69
+ *
70
+ */
71
+ const log = (message, level = LogLevel.info) => {
72
+ (0, exports.Logger)();
73
+ global.logger?._log(message, level);
74
+ };
75
+ exports.log = log;
58
76
  exports.default = exports.Logger;
package/package.json CHANGED
@@ -5,11 +5,11 @@
5
5
  "bugs": {
6
6
  "url": "https://github.com/graphql-markdown/graphql-markdown/issues"
7
7
  },
8
- "version": "1.0.0-next.0",
8
+ "version": "1.0.0",
9
9
  "license": "MIT",
10
10
  "repository": {
11
11
  "type": "git",
12
- "url": "graphql-markdown/graphql-markdown",
12
+ "url": "git+https://github.com/graphql-markdown/graphql-markdown.git",
13
13
  "directory": "packages/logger"
14
14
  },
15
15
  "author": {
@@ -42,7 +42,7 @@
42
42
  "docs": "typedoc"
43
43
  },
44
44
  "devDependencies": {
45
- "@graphql-markdown/types": "^1.0.0-next.0"
45
+ "@graphql-markdown/types": "^1.0.0"
46
46
  },
47
47
  "directories": {
48
48
  "test": "tests"