@onflow/util-logger
Advanced tools
Comparing version 1.3.0-alpha.0 to 1.3.0-typescript.0
# @onflow/util-logger | ||
## 1.3.0-alpha.0 | ||
## 1.3.0-typescript.0 | ||
### Minor Changes | ||
- TS build | ||
- [#1801](https://github.com/onflow/fcl-js/pull/1801) [`9fca84a3`](https://github.com/onflow/fcl-js/commit/9fca84a3151d7f6aeb33870a302e9793f024516b) Thanks [@nialexsan](https://github.com/nialexsan)! - Convert to Typescript | ||
- [#1801](https://github.com/onflow/fcl-js/pull/1801) [`86ce9f75`](https://github.com/onflow/fcl-js/commit/86ce9f75b5542a6bce76012e36a7a3d4fb6867f2) Thanks [@nialexsan](https://github.com/nialexsan)! - TS build | ||
## 1.2.2 | ||
### Patch Changes | ||
- Updated dependencies []: | ||
- @onflow/config@1.2.0-alpha.0 | ||
- [#1771](https://github.com/onflow/fcl-js/pull/1771) [`5edbd823`](https://github.com/onflow/fcl-js/commit/5edbd823b1a6d25eb7bb52dc55338f95beae73b1) Thanks [@jribbink](https://github.com/jribbink)! - Fix @onflow/util-logger <-> @onflow/config circular dependency | ||
@@ -14,0 +17,0 @@ ## 1.2.1 |
@@ -5,78 +5,35 @@ 'use strict'; | ||
var config = require('@onflow/config'); | ||
// Config dependency injected into logger to break circular dependency | ||
let config = null; | ||
const setConfig = _config => { | ||
config = _config; | ||
}; | ||
/****************************************************************************** | ||
Copyright (c) Microsoft Corporation. | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */ | ||
function __awaiter(thisArg, _arguments, P, generator) { | ||
function adopt(value) { | ||
return value instanceof P ? value : new P(function (resolve) { | ||
resolve(value); | ||
}); | ||
} | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { | ||
try { | ||
step(generator.next(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
} | ||
function rejected(value) { | ||
try { | ||
step(generator["throw"](value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
} | ||
function step(result) { | ||
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); | ||
} | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
} | ||
/** | ||
* 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 | ||
* | ||
*/ | ||
const LEVELS = Object.freeze({ | ||
debug: 5, | ||
info: 4, | ||
log: 3, | ||
warn: 2, | ||
error: 1, | ||
}); | ||
let LEVELS = /*#__PURE__*/function (LEVELS) { | ||
LEVELS[LEVELS["debug"] = 5] = "debug"; | ||
LEVELS[LEVELS["info"] = 4] = "info"; | ||
LEVELS[LEVELS["log"] = 3] = "log"; | ||
LEVELS[LEVELS["warn"] = 2] = "warn"; | ||
LEVELS[LEVELS["error"] = 1] = "error"; | ||
return LEVELS; | ||
}({}); | ||
/** | ||
* 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 | ||
* | ||
* @param options - The options for the log | ||
* @param options.title - The title of the log | ||
* @param options.message - The message of the log | ||
* @returns The message formatted for the logger | ||
* @example | ||
* buildLoggerMessageArgs({ title: "My Title", message: "My Message" }) | ||
*/ | ||
const buildLoggerMessageArgs = ({ title, message }) => { | ||
return [ | ||
` | ||
const buildLoggerMessageArgs = options => { | ||
const { | ||
title, | ||
message | ||
} = options; | ||
return [` | ||
%c${title} | ||
@@ -88,84 +45,92 @@ ============================ | ||
============================ | ||
` | ||
.replace(/\n[^\S\r\n]+/g, "\n") | ||
.trim(), | ||
, | ||
"font-weight:bold;font-family:monospace;", | ||
]; | ||
`.replace(/\n[^\S\r\n]+/g, "\n").trim(), "font-weight:bold;font-family:monospace;"]; | ||
}; | ||
/** | ||
* 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>} | ||
* | ||
* @param options - The options for the log | ||
* @param options.title - The title of the log | ||
* @param options.message - The message of the log | ||
* @param options.level - The level of the log | ||
* @param options.always - Whether to always show the log | ||
* @example | ||
* log({ title: "My Title", message: "My Message", level: LEVELS.warn, always: false }) | ||
* | ||
*/ | ||
const log = ({ title, message, level, always = false }) => __awaiter(void 0, void 0, void 0, function* () { | ||
const configLoggerLevel = yield config.config.get("logger.level", LEVELS.warn); | ||
// If config level is below message level then don't show it | ||
if (!always && configLoggerLevel < level) | ||
return; | ||
const loggerMessageArgs = buildLoggerMessageArgs({ title, message }); | ||
switch (level) { | ||
case LEVELS.debug: | ||
console.debug(...loggerMessageArgs); | ||
break; | ||
case LEVELS.info: | ||
console.info(...loggerMessageArgs); | ||
break; | ||
case LEVELS.warn: | ||
console.warn(...loggerMessageArgs); | ||
break; | ||
case LEVELS.error: | ||
console.error(...loggerMessageArgs); | ||
break; | ||
default: | ||
console.log(...loggerMessageArgs); | ||
} | ||
}); | ||
const log = async options => { | ||
const { | ||
title, | ||
message, | ||
level, | ||
always | ||
} = options; | ||
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; | ||
const loggerMessageArgs = buildLoggerMessageArgs({ | ||
title, | ||
message | ||
}); | ||
switch (level) { | ||
case LEVELS.debug: | ||
console.debug(...loggerMessageArgs); | ||
break; | ||
case LEVELS.info: | ||
console.info(...loggerMessageArgs); | ||
break; | ||
case LEVELS.warn: | ||
console.warn(...loggerMessageArgs); | ||
break; | ||
case LEVELS.error: | ||
console.error(...loggerMessageArgs); | ||
break; | ||
default: | ||
console.log(...loggerMessageArgs); | ||
} | ||
}; | ||
/** | ||
* 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 | ||
* @param {string} options.transition - The transition path for the deprecation | ||
* @param {number} options.level - The level of the log | ||
* @param {string} options.message - The message of the log | ||
* @param {Function} options.callback - A callback to run after the log | ||
* @returns {Promise<void>} | ||
* | ||
* Logs a deprecation notice. If a callback is provided this function returns a function that will call the callback and log the deprecation notice, otherwise it just logs the deprecation notice. | ||
* @param options - The options for the log | ||
* @param options.pkg - The package that is being deprecated | ||
* @param options.subject - The subject of the deprecation | ||
* @param options.transition - The transition path for the deprecation | ||
* @param options.level - The level of the log | ||
* @param options.message - The message of the log | ||
* @param options.callback - A callback to run after the log | ||
* @returns A function that will call the callback and log the deprecation notice if the callback is provided | ||
* @example | ||
* // Logs a deprecation notice | ||
* log.deprecate({ pkg: "@onflow/fcl", subject: "Some item", transition: "https://github.com/onflow/flow-js-sdk", message: "Descriptive message", level: LEVELS.warn, callback: () => {} }) | ||
* | ||
* @example | ||
* function someFunction() { ... } | ||
* const deprecatedFunction = log.deprecate({ pkg: "@onflow/fcl", subject: "Some item", transition: "https://github.com/foo/bar/TRANSITIONS.md", message: "Descriptive message", level: LEVELS.warn, callback: someFunction }) | ||
* deprecatedFunction() // Calls someFunction and logs the deprecation notice | ||
*/ | ||
log.deprecate = ({ pkg, subject, transition, level = LEVELS.warn, message = "", callback = null, }) => { | ||
const capitalizeFirstLetter = string => { | ||
return string.charAt(0).toUpperCase() + string.slice(1); | ||
log.deprecate = options => { | ||
const { | ||
pkg, | ||
subject, | ||
transition, | ||
level = LEVELS.warn, | ||
message = "", | ||
callback = null | ||
} = options; | ||
const capitalizeFirstLetter = str => { | ||
return str.charAt(0).toUpperCase() + str.slice(1); | ||
}; | ||
const logMessage = () => log({ | ||
title: `${pkg ? pkg + " " : ""}Deprecation Notice`, | ||
message: ` | ||
${subject ? `${capitalizeFirstLetter(subject)} is deprecated and will cease to work in future releases${pkg ? " of " + pkg : ""}.` : ""}${message ? "\n" + message : ""}${transition ? `\nYou can learn more (including a guide on common transition paths) here: ${transition}` : ""} | ||
`.trim(), | ||
level | ||
}); | ||
if (typeof callback === "function") { | ||
return async function () { | ||
await logMessage(); | ||
return await callback(...arguments); | ||
}; | ||
const logMessage = () => log({ | ||
title: `${pkg ? pkg + " " : ""}Deprecation Notice`, | ||
message: ` | ||
${subject | ||
? `${capitalizeFirstLetter(subject)} is deprecated and will cease to work in future releases${pkg ? " of " + pkg : ""}.` | ||
: ""}${message ? "\n" + message : ""}${transition | ||
? `\nYou can learn more (including a guide on common transition paths) here: ${transition}` | ||
: ""} | ||
`.trim(), | ||
level, | ||
}); | ||
if (typeof callback === "function") { | ||
return (...args) => __awaiter(void 0, void 0, void 0, function* () { | ||
yield logMessage(); | ||
return yield callback(...args); | ||
}); | ||
} | ||
return logMessage(); | ||
} | ||
return logMessage(); | ||
}; | ||
@@ -175,2 +140,3 @@ | ||
exports.log = log; | ||
exports.setConfig = setConfig; | ||
//# sourceMappingURL=util-logger.js.map |
@@ -1,77 +0,34 @@ | ||
import { config } from '@onflow/config'; | ||
// Config dependency injected into logger to break circular dependency | ||
let config = null; | ||
const setConfig = _config => { | ||
config = _config; | ||
}; | ||
/****************************************************************************** | ||
Copyright (c) Microsoft Corporation. | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */ | ||
function __awaiter(thisArg, _arguments, P, generator) { | ||
function adopt(value) { | ||
return value instanceof P ? value : new P(function (resolve) { | ||
resolve(value); | ||
}); | ||
} | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { | ||
try { | ||
step(generator.next(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
} | ||
function rejected(value) { | ||
try { | ||
step(generator["throw"](value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
} | ||
function step(result) { | ||
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); | ||
} | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
} | ||
/** | ||
* 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 | ||
* | ||
*/ | ||
const LEVELS = Object.freeze({ | ||
debug: 5, | ||
info: 4, | ||
log: 3, | ||
warn: 2, | ||
error: 1, | ||
}); | ||
let LEVELS = /*#__PURE__*/function (LEVELS) { | ||
LEVELS[LEVELS["debug"] = 5] = "debug"; | ||
LEVELS[LEVELS["info"] = 4] = "info"; | ||
LEVELS[LEVELS["log"] = 3] = "log"; | ||
LEVELS[LEVELS["warn"] = 2] = "warn"; | ||
LEVELS[LEVELS["error"] = 1] = "error"; | ||
return LEVELS; | ||
}({}); | ||
/** | ||
* 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 | ||
* | ||
* @param options - The options for the log | ||
* @param options.title - The title of the log | ||
* @param options.message - The message of the log | ||
* @returns The message formatted for the logger | ||
* @example | ||
* buildLoggerMessageArgs({ title: "My Title", message: "My Message" }) | ||
*/ | ||
const buildLoggerMessageArgs = ({ title, message }) => { | ||
return [ | ||
` | ||
const buildLoggerMessageArgs = options => { | ||
const { | ||
title, | ||
message | ||
} = options; | ||
return [` | ||
%c${title} | ||
@@ -83,87 +40,95 @@ ============================ | ||
============================ | ||
` | ||
.replace(/\n[^\S\r\n]+/g, "\n") | ||
.trim(), | ||
, | ||
"font-weight:bold;font-family:monospace;", | ||
]; | ||
`.replace(/\n[^\S\r\n]+/g, "\n").trim(), "font-weight:bold;font-family:monospace;"]; | ||
}; | ||
/** | ||
* 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>} | ||
* | ||
* @param options - The options for the log | ||
* @param options.title - The title of the log | ||
* @param options.message - The message of the log | ||
* @param options.level - The level of the log | ||
* @param options.always - Whether to always show the log | ||
* @example | ||
* log({ title: "My Title", message: "My Message", level: LEVELS.warn, always: false }) | ||
* | ||
*/ | ||
const log = ({ title, message, level, always = false }) => __awaiter(void 0, void 0, void 0, function* () { | ||
const configLoggerLevel = yield config.get("logger.level", LEVELS.warn); | ||
// If config level is below message level then don't show it | ||
if (!always && configLoggerLevel < level) | ||
return; | ||
const loggerMessageArgs = buildLoggerMessageArgs({ title, message }); | ||
switch (level) { | ||
case LEVELS.debug: | ||
console.debug(...loggerMessageArgs); | ||
break; | ||
case LEVELS.info: | ||
console.info(...loggerMessageArgs); | ||
break; | ||
case LEVELS.warn: | ||
console.warn(...loggerMessageArgs); | ||
break; | ||
case LEVELS.error: | ||
console.error(...loggerMessageArgs); | ||
break; | ||
default: | ||
console.log(...loggerMessageArgs); | ||
} | ||
}); | ||
const log = async options => { | ||
const { | ||
title, | ||
message, | ||
level, | ||
always | ||
} = options; | ||
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; | ||
const loggerMessageArgs = buildLoggerMessageArgs({ | ||
title, | ||
message | ||
}); | ||
switch (level) { | ||
case LEVELS.debug: | ||
console.debug(...loggerMessageArgs); | ||
break; | ||
case LEVELS.info: | ||
console.info(...loggerMessageArgs); | ||
break; | ||
case LEVELS.warn: | ||
console.warn(...loggerMessageArgs); | ||
break; | ||
case LEVELS.error: | ||
console.error(...loggerMessageArgs); | ||
break; | ||
default: | ||
console.log(...loggerMessageArgs); | ||
} | ||
}; | ||
/** | ||
* 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 | ||
* @param {string} options.transition - The transition path for the deprecation | ||
* @param {number} options.level - The level of the log | ||
* @param {string} options.message - The message of the log | ||
* @param {Function} options.callback - A callback to run after the log | ||
* @returns {Promise<void>} | ||
* | ||
* Logs a deprecation notice. If a callback is provided this function returns a function that will call the callback and log the deprecation notice, otherwise it just logs the deprecation notice. | ||
* @param options - The options for the log | ||
* @param options.pkg - The package that is being deprecated | ||
* @param options.subject - The subject of the deprecation | ||
* @param options.transition - The transition path for the deprecation | ||
* @param options.level - The level of the log | ||
* @param options.message - The message of the log | ||
* @param options.callback - A callback to run after the log | ||
* @returns A function that will call the callback and log the deprecation notice if the callback is provided | ||
* @example | ||
* // Logs a deprecation notice | ||
* log.deprecate({ pkg: "@onflow/fcl", subject: "Some item", transition: "https://github.com/onflow/flow-js-sdk", message: "Descriptive message", level: LEVELS.warn, callback: () => {} }) | ||
* | ||
* @example | ||
* function someFunction() { ... } | ||
* const deprecatedFunction = log.deprecate({ pkg: "@onflow/fcl", subject: "Some item", transition: "https://github.com/foo/bar/TRANSITIONS.md", message: "Descriptive message", level: LEVELS.warn, callback: someFunction }) | ||
* deprecatedFunction() // Calls someFunction and logs the deprecation notice | ||
*/ | ||
log.deprecate = ({ pkg, subject, transition, level = LEVELS.warn, message = "", callback = null, }) => { | ||
const capitalizeFirstLetter = string => { | ||
return string.charAt(0).toUpperCase() + string.slice(1); | ||
log.deprecate = options => { | ||
const { | ||
pkg, | ||
subject, | ||
transition, | ||
level = LEVELS.warn, | ||
message = "", | ||
callback = null | ||
} = options; | ||
const capitalizeFirstLetter = str => { | ||
return str.charAt(0).toUpperCase() + str.slice(1); | ||
}; | ||
const logMessage = () => log({ | ||
title: `${pkg ? pkg + " " : ""}Deprecation Notice`, | ||
message: ` | ||
${subject ? `${capitalizeFirstLetter(subject)} is deprecated and will cease to work in future releases${pkg ? " of " + pkg : ""}.` : ""}${message ? "\n" + message : ""}${transition ? `\nYou can learn more (including a guide on common transition paths) here: ${transition}` : ""} | ||
`.trim(), | ||
level | ||
}); | ||
if (typeof callback === "function") { | ||
return async function () { | ||
await logMessage(); | ||
return await callback(...arguments); | ||
}; | ||
const logMessage = () => log({ | ||
title: `${pkg ? pkg + " " : ""}Deprecation Notice`, | ||
message: ` | ||
${subject | ||
? `${capitalizeFirstLetter(subject)} is deprecated and will cease to work in future releases${pkg ? " of " + pkg : ""}.` | ||
: ""}${message ? "\n" + message : ""}${transition | ||
? `\nYou can learn more (including a guide on common transition paths) here: ${transition}` | ||
: ""} | ||
`.trim(), | ||
level, | ||
}); | ||
if (typeof callback === "function") { | ||
return (...args) => __awaiter(void 0, void 0, void 0, function* () { | ||
yield logMessage(); | ||
return yield callback(...args); | ||
}); | ||
} | ||
return logMessage(); | ||
} | ||
return logMessage(); | ||
}; | ||
export { LEVELS, log }; | ||
export { LEVELS, log, setConfig }; | ||
//# sourceMappingURL=util-logger.module.js.map |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@onflow/config')) : | ||
typeof define === 'function' && define.amd ? define(['exports', '@onflow/config'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["onflowUtil-logger"] = {}, global.config)); | ||
})(this, (function (exports, config) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["onflowUtil-logger"] = {})); | ||
})(this, (function (exports) { 'use strict'; | ||
/****************************************************************************** | ||
Copyright (c) Microsoft Corporation. | ||
// Config dependency injected into logger to break circular dependency | ||
let config = null; | ||
const setConfig = _config => { | ||
config = _config; | ||
}; | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted. | ||
/** | ||
* The levels of the logger | ||
*/ | ||
let LEVELS = /*#__PURE__*/function (LEVELS) { | ||
LEVELS[LEVELS["debug"] = 5] = "debug"; | ||
LEVELS[LEVELS["info"] = 4] = "info"; | ||
LEVELS[LEVELS["log"] = 3] = "log"; | ||
LEVELS[LEVELS["warn"] = 2] = "warn"; | ||
LEVELS[LEVELS["error"] = 1] = "error"; | ||
return LEVELS; | ||
}({}); | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */ | ||
function __awaiter(thisArg, _arguments, P, generator) { | ||
function adopt(value) { | ||
return value instanceof P ? value : new P(function (resolve) { | ||
resolve(value); | ||
}); | ||
} | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { | ||
try { | ||
step(generator.next(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
} | ||
function rejected(value) { | ||
try { | ||
step(generator["throw"](value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
} | ||
function step(result) { | ||
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); | ||
} | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
} | ||
/** | ||
* 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 | ||
* | ||
*/ | ||
const LEVELS = Object.freeze({ | ||
debug: 5, | ||
info: 4, | ||
log: 3, | ||
warn: 2, | ||
error: 1, | ||
}); | ||
/** | ||
* 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" }) | ||
*/ | ||
const buildLoggerMessageArgs = ({ title, message }) => { | ||
return [ | ||
` | ||
/** | ||
* Builds a message formatted for the logger | ||
* @param options - The options for the log | ||
* @param options.title - The title of the log | ||
* @param options.message - The message of the log | ||
* @returns The message formatted for the logger | ||
* @example | ||
* buildLoggerMessageArgs({ title: "My Title", message: "My Message" }) | ||
*/ | ||
const buildLoggerMessageArgs = options => { | ||
const { | ||
title, | ||
message | ||
} = options; | ||
return [` | ||
%c${title} | ||
@@ -87,92 +46,101 @@ ============================ | ||
============================ | ||
` | ||
.replace(/\n[^\S\r\n]+/g, "\n") | ||
.trim(), | ||
, | ||
"font-weight:bold;font-family:monospace;", | ||
]; | ||
`.replace(/\n[^\S\r\n]+/g, "\n").trim(), "font-weight:bold;font-family:monospace;"]; | ||
}; | ||
/** | ||
* Logs messages based on the level of the message and the level set in the config | ||
* @param options - The options for the log | ||
* @param options.title - The title of the log | ||
* @param options.message - The message of the log | ||
* @param options.level - The level of the log | ||
* @param options.always - Whether to always show the log | ||
* @example | ||
* log({ title: "My Title", message: "My Message", level: LEVELS.warn, always: false }) | ||
*/ | ||
const log = async options => { | ||
const { | ||
title, | ||
message, | ||
level, | ||
always | ||
} = options; | ||
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; | ||
const loggerMessageArgs = buildLoggerMessageArgs({ | ||
title, | ||
message | ||
}); | ||
switch (level) { | ||
case LEVELS.debug: | ||
console.debug(...loggerMessageArgs); | ||
break; | ||
case LEVELS.info: | ||
console.info(...loggerMessageArgs); | ||
break; | ||
case LEVELS.warn: | ||
console.warn(...loggerMessageArgs); | ||
break; | ||
case LEVELS.error: | ||
console.error(...loggerMessageArgs); | ||
break; | ||
default: | ||
console.log(...loggerMessageArgs); | ||
} | ||
}; | ||
/** | ||
* Logs a deprecation notice. If a callback is provided this function returns a function that will call the callback and log the deprecation notice, otherwise it just logs the deprecation notice. | ||
* @param options - The options for the log | ||
* @param options.pkg - The package that is being deprecated | ||
* @param options.subject - The subject of the deprecation | ||
* @param options.transition - The transition path for the deprecation | ||
* @param options.level - The level of the log | ||
* @param options.message - The message of the log | ||
* @param options.callback - A callback to run after the log | ||
* @returns A function that will call the callback and log the deprecation notice if the callback is provided | ||
* @example | ||
* // Logs a deprecation notice | ||
* log.deprecate({ pkg: "@onflow/fcl", subject: "Some item", transition: "https://github.com/onflow/flow-js-sdk", message: "Descriptive message", level: LEVELS.warn, callback: () => {} }) | ||
* @example | ||
* function someFunction() { ... } | ||
* const deprecatedFunction = log.deprecate({ pkg: "@onflow/fcl", subject: "Some item", transition: "https://github.com/foo/bar/TRANSITIONS.md", message: "Descriptive message", level: LEVELS.warn, callback: someFunction }) | ||
* deprecatedFunction() // Calls someFunction and logs the deprecation notice | ||
*/ | ||
log.deprecate = options => { | ||
const { | ||
pkg, | ||
subject, | ||
transition, | ||
level = LEVELS.warn, | ||
message = "", | ||
callback = null | ||
} = options; | ||
const capitalizeFirstLetter = str => { | ||
return str.charAt(0).toUpperCase() + str.slice(1); | ||
}; | ||
/** | ||
* 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 }) | ||
* | ||
*/ | ||
const log = ({ title, message, level, always = false }) => __awaiter(void 0, void 0, void 0, function* () { | ||
const configLoggerLevel = yield config.config.get("logger.level", LEVELS.warn); | ||
// If config level is below message level then don't show it | ||
if (!always && configLoggerLevel < level) | ||
return; | ||
const loggerMessageArgs = buildLoggerMessageArgs({ title, message }); | ||
switch (level) { | ||
case LEVELS.debug: | ||
console.debug(...loggerMessageArgs); | ||
break; | ||
case LEVELS.info: | ||
console.info(...loggerMessageArgs); | ||
break; | ||
case LEVELS.warn: | ||
console.warn(...loggerMessageArgs); | ||
break; | ||
case LEVELS.error: | ||
console.error(...loggerMessageArgs); | ||
break; | ||
default: | ||
console.log(...loggerMessageArgs); | ||
} | ||
const logMessage = () => log({ | ||
title: `${pkg ? pkg + " " : ""}Deprecation Notice`, | ||
message: ` | ||
${subject ? `${capitalizeFirstLetter(subject)} is deprecated and will cease to work in future releases${pkg ? " of " + pkg : ""}.` : ""}${message ? "\n" + message : ""}${transition ? `\nYou can learn more (including a guide on common transition paths) here: ${transition}` : ""} | ||
`.trim(), | ||
level | ||
}); | ||
/** | ||
* 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 | ||
* @param {string} options.transition - The transition path for the deprecation | ||
* @param {number} options.level - The level of the log | ||
* @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, subject, transition, level = LEVELS.warn, message = "", callback = null, }) => { | ||
const capitalizeFirstLetter = string => { | ||
return string.charAt(0).toUpperCase() + string.slice(1); | ||
}; | ||
const logMessage = () => log({ | ||
title: `${pkg ? pkg + " " : ""}Deprecation Notice`, | ||
message: ` | ||
${subject | ||
? `${capitalizeFirstLetter(subject)} is deprecated and will cease to work in future releases${pkg ? " of " + pkg : ""}.` | ||
: ""}${message ? "\n" + message : ""}${transition | ||
? `\nYou can learn more (including a guide on common transition paths) here: ${transition}` | ||
: ""} | ||
`.trim(), | ||
level, | ||
}); | ||
if (typeof callback === "function") { | ||
return (...args) => __awaiter(void 0, void 0, void 0, function* () { | ||
yield logMessage(); | ||
return yield callback(...args); | ||
}); | ||
} | ||
return logMessage(); | ||
}; | ||
if (typeof callback === "function") { | ||
return async function () { | ||
await logMessage(); | ||
return await callback(...arguments); | ||
}; | ||
} | ||
return logMessage(); | ||
}; | ||
exports.LEVELS = LEVELS; | ||
exports.log = log; | ||
exports.LEVELS = LEVELS; | ||
exports.log = log; | ||
exports.setConfig = setConfig; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
})); | ||
//# sourceMappingURL=util-logger.umd.js.map |
{ | ||
"name": "@onflow/util-logger", | ||
"version": "1.3.0-alpha.0", | ||
"version": "1.3.0-typescript.0", | ||
"description": "Logger for FCL-JS", | ||
@@ -16,3 +16,9 @@ "license": "Apache-2.0", | ||
"devDependencies": { | ||
"@onflow/fcl-bundle": "^1.4.0-alpha.0", | ||
"@babel/preset-typescript": "^7.22.5", | ||
"@onflow/fcl-bundle": "^1.4.0-typescript.0", | ||
"@types/jest": "^29.5.3", | ||
"@typescript-eslint/eslint-plugin": "^6.4.0", | ||
"@typescript-eslint/parser": "^6.4.0", | ||
"eslint": "^8.47.0", | ||
"eslint-plugin-jsdoc": "^46.4.6", | ||
"jest": "^29.5.0" | ||
@@ -24,2 +30,3 @@ }, | ||
"unpkg": "dist/util-logger.umd.js", | ||
"types": "dist/util-logger.d.ts", | ||
"scripts": { | ||
@@ -33,5 +40,12 @@ "prepublishOnly": "npm test && npm run build", | ||
"dependencies": { | ||
"@babel/runtime": "^7.18.6", | ||
"@onflow/config": "^1.2.0-alpha.0" | ||
"@babel/runtime": "^7.18.6" | ||
}, | ||
"peerDependencies": { | ||
"@onflow/util-config": ">1.1.1" | ||
}, | ||
"peerDependenciesMeta": { | ||
"@onflow/util-config": { | ||
"optional": true | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
14
57346
8
473
- Removed@onflow/config@^1.2.0-alpha.0
- Removed@es-joy/jsdoccomment@0.41.0(transitive)
- Removed@eslint-community/eslint-utils@4.4.1(transitive)
- Removed@eslint-community/regexpp@4.12.1(transitive)
- Removed@eslint/eslintrc@2.1.4(transitive)
- Removed@eslint/js@8.57.1(transitive)
- Removed@humanwhocodes/config-array@0.13.0(transitive)
- Removed@humanwhocodes/module-importer@1.0.1(transitive)
- Removed@humanwhocodes/object-schema@2.0.3(transitive)
- Removed@nodelib/fs.scandir@2.1.5(transitive)
- Removed@nodelib/fs.stat@2.0.5(transitive)
- Removed@nodelib/fs.walk@1.2.8(transitive)
- Removed@onflow/config@1.5.1(transitive)
- Removed@onflow/util-actor@1.3.4(transitive)
- Removed@onflow/util-invariant@1.2.4(transitive)
- Removed@onflow/util-logger@1.3.3(transitive)
- Removed@ungap/structured-clone@1.2.0(transitive)
- Removedacorn@8.14.0(transitive)
- Removedacorn-jsx@5.3.2(transitive)
- Removedajv@6.12.6(transitive)
- Removedansi-regex@5.0.1(transitive)
- Removedansi-styles@4.3.0(transitive)
- Removedare-docs-informative@0.0.2(transitive)
- Removedargparse@2.0.1(transitive)
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedbuiltin-modules@3.3.0(transitive)
- Removedcallsites@3.1.0(transitive)
- Removedchalk@4.1.2(transitive)
- Removedcolor-convert@2.0.1(transitive)
- Removedcolor-name@1.1.4(transitive)
- Removedcomment-parser@1.4.1(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedcross-spawn@7.0.6(transitive)
- Removeddebug@4.3.7(transitive)
- Removeddeep-is@0.1.4(transitive)
- Removeddoctrine@3.0.0(transitive)
- Removedescape-string-regexp@4.0.0(transitive)
- Removedeslint@8.57.1(transitive)
- Removedeslint-plugin-jsdoc@46.10.1(transitive)
- Removedeslint-scope@7.2.2(transitive)
- Removedeslint-visitor-keys@3.4.3(transitive)
- Removedespree@9.6.1(transitive)
- Removedesquery@1.6.0(transitive)
- Removedesrecurse@4.3.0(transitive)
- Removedestraverse@5.3.0(transitive)
- Removedesutils@2.0.3(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedfast-levenshtein@2.0.6(transitive)
- Removedfastq@1.17.1(transitive)
- Removedfile-entry-cache@6.0.1(transitive)
- Removedfind-up@5.0.0(transitive)
- Removedflat-cache@3.2.0(transitive)
- Removedflatted@3.3.2(transitive)
- Removedfs.realpath@1.0.0(transitive)
- Removedglob@7.2.3(transitive)
- Removedglob-parent@6.0.2(transitive)
- Removedglobals@13.24.0(transitive)
- Removedgraphemer@1.4.0(transitive)
- Removedhas-flag@4.0.0(transitive)
- Removedignore@5.3.2(transitive)
- Removedimport-fresh@3.3.0(transitive)
- Removedimurmurhash@0.1.4(transitive)
- Removedinflight@1.0.6(transitive)
- Removedinherits@2.0.4(transitive)
- Removedis-builtin-module@3.2.1(transitive)
- Removedis-extglob@2.1.1(transitive)
- Removedis-glob@4.0.3(transitive)
- Removedis-path-inside@3.0.3(transitive)
- Removedisexe@2.0.0(transitive)
- Removedjs-yaml@4.1.0(transitive)
- Removedjsdoc-type-pratt-parser@4.0.0(transitive)
- Removedjson-buffer@3.0.1(transitive)
- Removedjson-schema-traverse@0.4.1(transitive)
- Removedjson-stable-stringify-without-jsonify@1.0.1(transitive)
- Removedkeyv@4.5.4(transitive)
- Removedlevn@0.4.1(transitive)
- Removedlocate-path@6.0.0(transitive)
- Removedlodash.merge@4.6.2(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedms@2.1.3(transitive)
- Removednatural-compare@1.4.0(transitive)
- Removedonce@1.4.0(transitive)
- Removedoptionator@0.9.4(transitive)
- Removedp-limit@3.1.0(transitive)
- Removedp-locate@5.0.0(transitive)
- Removedparent-module@1.0.1(transitive)
- Removedpath-exists@4.0.0(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedpath-key@3.1.1(transitive)
- Removedprelude-ls@1.2.1(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedqueue-microtask@1.2.3(transitive)
- Removedresolve-from@4.0.0(transitive)
- Removedreusify@1.0.4(transitive)
- Removedrimraf@3.0.2(transitive)
- Removedrun-parallel@1.2.0(transitive)
- Removedsemver@7.6.3(transitive)
- Removedshebang-command@2.0.0(transitive)
- Removedshebang-regex@3.0.0(transitive)
- Removedspdx-exceptions@2.5.0(transitive)
- Removedspdx-expression-parse@4.0.0(transitive)
- Removedspdx-license-ids@3.0.20(transitive)
- Removedstrip-ansi@6.0.1(transitive)
- Removedstrip-json-comments@3.1.1(transitive)
- Removedsupports-color@7.2.0(transitive)
- Removedtext-table@0.2.0(transitive)
- Removedtype-check@0.4.0(transitive)
- Removedtype-fest@0.20.2(transitive)
- Removeduri-js@4.4.1(transitive)
- Removedwhich@2.0.2(transitive)
- Removedword-wrap@1.2.5(transitive)
- Removedwrappy@1.0.2(transitive)
- Removedyocto-queue@0.1.0(transitive)