logging-utils
Advanced tools
Comparing version 2.0.3 to 2.0.4
@@ -47,2 +47,4 @@ 'use strict'; | ||
getDefaultLoggingSettings: getDefaultLoggingSettings, | ||
/** A convenience function to configure a target object with logging functionality based on either settings or options, if forced or not already configured */ | ||
configureLoggingWithSettingsOrOptions: configureLoggingWithSettingsOrOptions, | ||
/** A convenience function to configure a target object with logging functionality based on either settings or options, if not already configured */ | ||
@@ -375,2 +377,32 @@ configureLoggingIfNotConfigured: configureLoggingIfNotConfigured, | ||
return target; | ||
} | ||
/** | ||
* Configures the given target object with logging functionality using the given logging settings (if any) or using | ||
* default logging settings partially overridden by the given logging options (if any), but ONLY if forceConfiguration | ||
* is true or if there is no logging functionality already configured on the target, | ||
* @param {Object} target - the context to configure with logging | ||
* @param {LoggingSettings|undefined} [settings] - optional logging settings to use | ||
* @param {LoggingOptions|undefined} [options] - optional logging options to use if no logging settings provided | ||
* @param {Object|undefined} [underlyingLogger] - the optional underlying logger to use to do the actual logging | ||
* @param {boolean|undefined} [forceConfiguration] - whether or not to force configuration of the logging functionality, | ||
* which will override any previously configured logging functionality on the target object | ||
* @returns {Object} the given target object | ||
*/ | ||
function configureLoggingWithSettingsOrOptions(target, settings, options, underlyingLogger, forceConfiguration) { | ||
// Check if logging was already configured | ||
const loggingWasConfigured = isLoggingConfigured(target); | ||
// Determine the logging settings to be used | ||
const loggingSettingsAvailable = settings && typeof settings === 'object'; | ||
const loggingSettings = loggingSettingsAvailable ? settings : getDefaultLoggingSettings(options, underlyingLogger); | ||
// Configure logging with the given or derived logging settings | ||
configureLogging(target, loggingSettings, forceConfiguration); | ||
// Log a warning if no settings and no options were provided and the default settings were applied | ||
if (!loggingSettingsAvailable && (!options || typeof options !== 'object') && (forceConfiguration || !loggingWasConfigured)) { | ||
target.warn(`Logging was configured without settings or options - used default logging configuration (${stringify(loggingSettings)})`); | ||
} | ||
return target; | ||
} |
{ | ||
"name": "logging-utils", | ||
"description": "Utilities for configuring simple log level based logging functionality on an object", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"author": "Byron du Preez", | ||
@@ -6,0 +6,0 @@ "license": "Apache-2.0", |
@@ -1,2 +0,2 @@ | ||
# logging-utils v2.0.3 | ||
# logging-utils v2.0.4 | ||
Utilities for configuring simple log level based logging functionality on an object. | ||
@@ -33,6 +33,7 @@ | ||
// Logging configuration functions | ||
const isLoggingConfigured = logging.isLoggingConfigured; | ||
const configureLogging = logging.configureLogging; | ||
const getDefaultLoggingSettings = logging.getDefaultLoggingSettings; | ||
const configureDefaultLogging = logging.configureDefaultLogging; | ||
const isLoggingConfigured = logging.isLoggingConfigured; | ||
const getDefaultLoggingSettings = logging.getDefaultLoggingSettings; | ||
const configureLoggingWithSettingsOrOptions = logging.configureLoggingWithSettingsOrOptions; | ||
@@ -86,4 +87,4 @@ // Log level constants | ||
```js | ||
const config = { loggingOptions: { logLevel: DEBUG, useLevelPrefixes: true, useConsoleTrace: false } }; // replace with your own config object | ||
const loggingSettings = getDefaultLoggingSettings(config.loggingOptions); | ||
const options = { loggingOptions: { logLevel: DEBUG, useLevelPrefixes: true, useConsoleTrace: false } }; // replace with your own config object | ||
const loggingSettings = getDefaultLoggingSettings(options.loggingOptions); | ||
configureLogging(context, loggingSettings); | ||
@@ -107,2 +108,12 @@ // or as an alternative to the above 2 lines, just use the following: | ||
* To configure logging from EITHER logging settings OR logging options (OR defaults if neither) - WITHOUT overriding any existing logging on context | ||
```js | ||
configureLoggingWithSettingsOrOptions(context, loggingSettings, loggingOptions, underlyingLogger, false); | ||
``` | ||
* To configure logging from EITHER logging settings OR logging options (OR defaults if neither) - OVERRIDING any existing logging on context! | ||
```js | ||
configureLoggingWithSettingsOrOptions(context, loggingSettings, loggingOptions, underlyingLogger, true); | ||
``` | ||
### 2. Log messages | ||
@@ -158,2 +169,6 @@ | ||
### 2.0.4 | ||
- Changes to `logging.js` module: | ||
- Added new `configureLoggingWithSettingsOrOptions` function to simplify programmatic configuration | ||
### 2.0.3 | ||
@@ -160,0 +175,0 @@ - Changes to `logging.js` module: |
@@ -24,2 +24,3 @@ 'use strict'; | ||
const configureLoggingIfNotConfigured = logging.configureLoggingIfNotConfigured; | ||
const configureLoggingWithSettingsOrOptions = logging.configureLoggingWithSettingsOrOptions; | ||
@@ -976,1 +977,93 @@ const booleans = require('core-functions/booleans'); | ||
// ===================================================================================================================== | ||
// Test configureLoggingWithSettingsOrOptions with settings | ||
// ===================================================================================================================== | ||
test('configureLoggingWithSettingsOrOptions with settings', t => { | ||
const logLevel = defaultSettings.logLevel; | ||
const logger = testLogger(logLevel, false, t); | ||
// Configure default logging when no logging configured yet (without force) | ||
const context = {abc: 123}; | ||
const settings = getDefaultLoggingSettings(undefined, logger); | ||
const options = undefined; | ||
configureLoggingWithSettingsOrOptions(context, settings, options, logger, false); | ||
t.equal(context.abc, 123, 'context must still be intact'); | ||
checkEnabledsBasedOnLogLevel(t, context, logLevel); | ||
logOneOfEach(context, logLevel); | ||
// Now override with something else entirely using force (to change away from previous config settings) | ||
const overrideLogLevel = logLevel !== ERROR ? ERROR : TRACE; | ||
const overrideSettings = { | ||
logLevel: overrideLogLevel, | ||
useLevelPrefixes: !defaultSettings.useLevelPrefixes, | ||
useConsoleTrace: !defaultSettings.useConsoleTrace, | ||
underlyingLogger: testLogger(overrideLogLevel, false, t) | ||
}; | ||
configureLogging(context, overrideSettings, true); | ||
// Now do NOT override with default logging configuration | ||
configureLoggingWithSettingsOrOptions(context, settings, options, logger, false); | ||
checkEnabledsBasedOnLogLevel(t, context, overrideLogLevel); | ||
logOneOfEach(context, overrideLogLevel); | ||
// Now override with default logging configuration | ||
configureLoggingWithSettingsOrOptions(context, settings, options, logger, true); | ||
t.equal(context.abc, 123, 'context must still be intact'); | ||
checkEnabledsBasedOnLogLevel(t, context, logLevel); | ||
logOneOfEach(context, logLevel); | ||
t.end(); | ||
}); | ||
// ===================================================================================================================== | ||
// Test configureLoggingWithSettingsOrOptions with options | ||
// ===================================================================================================================== | ||
test('configureLogging with getDefaultLoggingSettings on existing object with test logger & config.logLevel etc.', t => { | ||
const logLevel = defaultSettings.logLevel !== TRACE ? TRACE : ERROR; | ||
const logger = testLogger(logLevel, false, t); | ||
// Configure logging from config when no logging configured yet (without force) | ||
const context = {abc: 123}; | ||
const settings = undefined; | ||
const options = { | ||
logLevel: logLevel, | ||
useLevelPrefixes: !defaultSettings.useLevelPrefixes, | ||
useConsoleTrace: !defaultSettings.useConsoleTrace | ||
}; | ||
configureLoggingWithSettingsOrOptions(context, settings, options, logger, false); | ||
t.equal(context.abc, 123, 'context must still be intact'); | ||
checkEnabledsBasedOnLogLevel(t, context, logLevel); | ||
logOneOfEach(context, logLevel); | ||
// Now override with something else entirely using force (to change away from previous config settings) | ||
const overrideLogLevel = logLevel !== ERROR ? ERROR : TRACE; | ||
const overrideOptions = { | ||
logLevel: overrideLogLevel, | ||
useLevelPrefixes: defaultSettings.useLevelPrefixes, | ||
useConsoleTrace: defaultSettings.useConsoleTrace, | ||
}; | ||
const overrideSettings = getDefaultLoggingSettings(overrideOptions, testLogger(overrideLogLevel, false, t)); | ||
configureLogging(context, overrideSettings, true); | ||
// Now do NOT override with logging configuration again | ||
configureLoggingWithSettingsOrOptions(context, settings, options, logger, false); | ||
checkEnabledsBasedOnLogLevel(t, context, overrideLogLevel); | ||
logOneOfEach(context, overrideLogLevel); | ||
// Now override with logging configuration using force | ||
configureLoggingWithSettingsOrOptions(context, settings, options, logger, true); | ||
t.equal(context.abc, 123, 'context must still be intact'); | ||
checkEnabledsBasedOnLogLevel(t, context, logLevel); | ||
logOneOfEach(context, logLevel); | ||
t.end(); | ||
}); | ||
{ | ||
"name": "logging-utils-tests", | ||
"description": "Unit tests for logging-utils", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"author": "Byron du Preez", | ||
@@ -6,0 +6,0 @@ "license": "Apache-2.0", |
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
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
82396
1207
220