diff --git a/.changeset/odd-socks-appear.md b/.changeset/odd-socks-appear.md new file mode 100644 index 000000000..f069181f0 --- /dev/null +++ b/.changeset/odd-socks-appear.md @@ -0,0 +1,6 @@ +--- +"@onflow/util-logger": patch +"@onflow/config": patch +--- + +Fix @onflow/util-logger <-> @onflow/config circular dependency diff --git a/packages/config/src/config.js b/packages/config/src/config.js index 00472281f..f294a6a8a 100644 --- a/packages/config/src/config.js +++ b/packages/config/src/config.js @@ -9,6 +9,9 @@ import * as logger from "@onflow/util-logger" import {invariant} from "@onflow/util-invariant" import {getContracts, cleanNetwork, anyHasPrivateKeys} from "../utils/utils" +// Inject config into logger to break circular dependency +logger.setConfig(config) + const NAME = "config" const PUT = "PUT_CONFIG" const GET = "GET_CONFIG" diff --git a/packages/util-logger/package.json b/packages/util-logger/package.json index 444b13e73..d507b8157 100644 --- a/packages/util-logger/package.json +++ b/packages/util-logger/package.json @@ -28,7 +28,14 @@ "start": "fcl-bundle --watch" }, "dependencies": { - "@babel/runtime": "^7.18.6", - "@onflow/config": "^1.1.1" + "@babel/runtime": "^7.18.6" + }, + "peerDependencies": { + "@onflow/util-config": ">1.1.1" + }, + "peerDependenciesMeta": { + "@onflow/util-config": { + "optional": true + } } } diff --git a/packages/util-logger/src/util-logger.js b/packages/util-logger/src/util-logger.js index e57cbb0e1..489cbd11f 100644 --- a/packages/util-logger/src/util-logger.js +++ b/packages/util-logger/src/util-logger.js @@ -1,15 +1,19 @@ -import {config} from "@onflow/config" +// Config dependency injected into logger to break circular dependency +let config = null +export const setConfig = _config => { + config = _config +} /** * The levels of the logger - * + * * @typedef {Object} LEVELS * @property {number} debug - The debug level * @property {number} info - The info level * @property {number} log - The log level * @property {number} warn - The warn level * @property {number} error - The error level - * + * */ export const LEVELS = Object.freeze({ debug: 5, @@ -21,12 +25,12 @@ export const LEVELS = Object.freeze({ /** * Builds a message formatted for the logger - * + * * @param {Object} options - The options for the log * @param {string} options.title - The title of the log * @param {string} options.message - The message of the log * @returns {Array} - The message formatted for the logger - * + * * @example * buildLoggerMessageArgs({ title: "My Title", message: "My Message" }) */ @@ -49,20 +53,21 @@ const buildLoggerMessageArgs = ({title, message}) => { /** * Logs messages based on the level of the message and the level set in the config - * + * * @param {Object} options - The options for the log * @param {string} options.title - The title of the log * @param {string} options.message - The message of the log * @param {number} options.level - The level of the log * @param {boolean} options.always - Whether to always show the log * @returns {Promise} - * + * * @example * log({ title: "My Title", message: "My Message", level: LEVELS.warn, always: false }) - * + * */ export const log = async ({title, message, level, always = false}) => { - const configLoggerLevel = await config.get("logger.level", LEVELS.warn) + const configLoggerLevel = + (await config?.()?.get("logger.level")) ?? LEVELS.warn // If config level is below message level then don't show it if (!always && configLoggerLevel < level) return @@ -89,7 +94,7 @@ export const log = async ({title, message, level, always = false}) => { /** * Logs a deprecation notice - * + * * @param {Object} options - The options for the log * @param {string} options.pkg - The package that is being deprecated * @param {string} options.subject - The subject of the deprecation @@ -98,10 +103,10 @@ export const log = async ({title, message, level, always = false}) => { * @param {string} options.message - The message of the log * @param {Function} options.callback - A callback to run after the log * @returns {Promise} - * + * * @example * log.deprecate({ pkg: "@onflow/fcl", subject: "Some item", transition: "https://github.com/onflow/flow-js-sdk", message: "Descriptive message", level: LEVELS.warn, callback: () => {} }) - * + * */ log.deprecate = ({ pkg, diff --git a/packages/util-logger/src/util-logger.test.js b/packages/util-logger/src/util-logger.test.js index 0710764b7..9c4a7c447 100644 --- a/packages/util-logger/src/util-logger.test.js +++ b/packages/util-logger/src/util-logger.test.js @@ -21,6 +21,7 @@ describe("logger", () => { beforeEach(() => { configRef = config() + logger.setConfig(config) }) it("should not fire logger if config level is less than log level", async () => {