Skip to content

Commit

Permalink
Break @onflow/util-logger <-> @onflow/config circular dependency …
Browse files Browse the repository at this point in the history
…using DI (onflow#1771)
  • Loading branch information
jribbink authored Sep 1, 2023
1 parent 3c99c85 commit 5edbd82
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .changeset/odd-socks-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@onflow/util-logger": patch
"@onflow/config": patch
---

Fix @onflow/util-logger <-> @onflow/config circular dependency
3 changes: 3 additions & 0 deletions packages/config/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 9 additions & 2 deletions packages/util-logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
29 changes: 17 additions & 12 deletions packages/util-logger/src/util-logger.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<string>} - The message formatted for the logger
*
*
* @example
* buildLoggerMessageArgs({ title: "My Title", message: "My Message" })
*/
Expand All @@ -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<void>}
*
*
* @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
Expand All @@ -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
Expand All @@ -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<void>}
*
*
* @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,
Expand Down
1 change: 1 addition & 0 deletions packages/util-logger/src/util-logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 5edbd82

Please sign in to comment.