Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

logging-utils

Package Overview
Dependencies
Maintainers
1
Versions
54
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

logging-utils - npm Package Compare versions

Comparing version 2.0.4 to 3.0.0

111

logging.js

@@ -23,2 +23,4 @@ 'use strict';

const Objects = require('core-functions/objects');
function noop() {

@@ -42,4 +44,6 @@ }

isLoggingConfigured: isLoggingConfigured,
/** Configures a target object with logging functionality using given logging settings (if any) or using default logging settings partially overridden by given logging options (if any) */
configureLogging: configureLogging,
/** Configures a target object with logging functionality based on given logging settings */
configureLogging: configureLogging,
configureLoggingWithSettings: configureLoggingWithSettings,
/** Configures a target object with default logging functionality partially overridden with given logging options */

@@ -49,6 +53,2 @@ configureDefaultLogging: configureDefaultLogging,

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 */
configureLoggingIfNotConfigured: configureLoggingIfNotConfigured,

@@ -119,2 +119,35 @@ // Constants for Log Levels

/**
* 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 object.
* @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 configureLogging(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 loggingOptionsAvailable = options && typeof options === 'object';
const loggingSettings = loggingSettingsAvailable ?
loggingOptionsAvailable ? Objects.merge(options, settings, false, false) : settings :
getDefaultLoggingSettings(options, underlyingLogger);
// Configure logging with the given or derived logging settings
configureLoggingWithSettings(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;
}
/**
* Configures logging functionality on the given target object, but ONLY if forceConfiguration is true or if there is no

@@ -134,3 +167,3 @@ * logging functionality already configured on the target, based on the given logging settings and returns the target

* const context = {...}; // an existing object on which to configure logging
* configureLogging(context, {logLevel: 'info'});
* configureLoggingWithSettings(context, {logLevel: 'info'});
* let err = new Error('Some arbitrary error');

@@ -145,3 +178,3 @@ * context.error('Insert error message here', err.stack);

* const settings = {logLevel: 'info', useLevelPrefixes: true, underlyingLogger: console, useConsoleTrace: false};
* const log = configureLogging({}, settings, true);
* const log = configureLoggingWithSettings({}, settings, true);
* let err = new Error('Some arbitrary error');

@@ -160,3 +193,3 @@ * log.error('Insert error message here', err.stack);

*/
function configureLogging(target, settings, forceConfiguration) {
function configureLoggingWithSettings(target, settings, forceConfiguration) {
// If forceConfiguration is false check if the given target already has logging functionality configured on it

@@ -233,3 +266,3 @@ // and, if so, do nothing more and simply return the target as is (to prevent overriding an earlier configuration)

const settings = getDefaultLoggingSettings(options, underlyingLogger);
return configureLogging(target, settings, forceConfiguration);
return configureLoggingWithSettings(target, settings, forceConfiguration);
}

@@ -241,5 +274,5 @@

*
* This function is used internally by both {@linkcode configureLogging} and {@linkcode configureDefaultLogging}, but
* could also be used in custom configurations to get the default settings as a base to be overridden with your custom
* settings before calling {@linkcode configureLogging}.
* This function is used internally by {@linkcode configureLogging}, {@linkcode configureLoggingWithSettings} and
* {@linkcode configureDefaultLogging}, but could also be used in custom configurations to get the default settings as a
* base to be overridden with your custom settings before calling {@linkcode configureLogging}.
*

@@ -361,56 +394,2 @@ * @param {LoggingOptions|undefined} [options] - optional logging options to use to override the default options

return logWithPrefix;
}
/**
* 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 logging is not
* already configured on the given context.
* @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 {string|undefined} [caller] - optional arbitrary text to identify the caller of this function
* @returns {Object} the given target object
*/
function configureLoggingIfNotConfigured(target, settings, options, underlyingLogger, caller) {
if (!isLoggingConfigured(target)) {
if (settings && typeof settings === 'object') {
configureLogging(target, settings, true);
target.warn(`Logging was not configured${caller ? ` before calling ${caller}` : ''} - used logging settings (${stringify(settings)})`);
} else {
configureDefaultLogging(target, options, underlyingLogger, true);
target.warn(`Logging was not configured${caller ? ` before calling ${caller}` : ''} - used default logging configuration with options (${stringify(options)})`);
}
}
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.4",
"version": "3.0.0",
"author": "Byron du Preez",

@@ -16,4 +16,4 @@ "license": "Apache-2.0",

"dependencies": {
"core-functions": "^2.0.3"
"core-functions": "^2.0.5"
}
}

@@ -1,2 +0,2 @@

# logging-utils v2.0.4
# logging-utils v3.0.0
Utilities for configuring simple log level based logging functionality on an object.

@@ -34,6 +34,6 @@

const isLoggingConfigured = logging.isLoggingConfigured;
const configureLogging = logging.configureLogging;
const configureLoggingWithSettings = logging.configureLoggingWithSettings;
const getDefaultLoggingSettings = logging.getDefaultLoggingSettings;
const configureDefaultLogging = logging.configureDefaultLogging;
const configureLoggingWithSettingsOrOptions = logging.configureLoggingWithSettingsOrOptions;
const configureLogging = logging.configureLogging;

@@ -59,3 +59,3 @@ // Log level constants

```js
configureLogging(context, {logLevel: WARN});
configureLoggingWithSettings(context, {logLevel: WARN});
```

@@ -65,7 +65,7 @@ * To configure specific logging (WITHOUT overriding any existing logging on context)

const settings = {logLevel: DEBUG, useLevelPrefixes: false, useConsoleTrace: false, underlyingLogger: console};
configureLogging(context, settings, false);
configureLoggingWithSettings(context, settings, false);
```
* To configure specific logging (OVERRIDING any existing logging on context!)
```js
configureLogging(context, settings, true);
configureLoggingWithSettings(context, settings, true);
```

@@ -91,3 +91,3 @@

const loggingSettings = getDefaultLoggingSettings(options.loggingOptions);
configureLogging(context, loggingSettings);
configureLoggingWithSettings(context, loggingSettings);
// or as an alternative to the above 2 lines, just use the following:

@@ -97,4 +97,4 @@ configureDefaultLogging(context, config.loggingOptions);

// Alternatives specifying only optional underlying logger or forceConfiguration
configureLogging(context, loggingSettings, console);
configureLogging(context, loggingSettings, undefined, true);
configureLoggingWithSettings(context, loggingSettings, console);
configureLoggingWithSettings(context, loggingSettings, undefined, true);
```

@@ -106,3 +106,3 @@

const loggingSettings = getDefaultLoggingSettings(options);
configureLogging(context, loggingSettings);
configureLoggingWithSettings(context, loggingSettings);
// or as an alternative to the above 2 lines, just use the following:

@@ -114,3 +114,3 @@ configureDefaultLogging(context, options);

```js
configureLoggingWithSettingsOrOptions(context, loggingSettings, loggingOptions, underlyingLogger, false);
configureLogging(context, loggingSettings, loggingOptions, underlyingLogger, false);
```

@@ -120,3 +120,3 @@

```js
configureLoggingWithSettingsOrOptions(context, loggingSettings, loggingOptions, underlyingLogger, true);
configureLogging(context, loggingSettings, loggingOptions, underlyingLogger, true);
```

@@ -174,2 +174,9 @@

### 3.0.0
- Changes to `logging.js` module:
- Renamed `configureLogging` function to `configureLoggingWithSettings`
- Renamed `configureLoggingWithSettingsOrOptions` function to `configureLogging`
- Removed `configureLoggingIfNotConfigured` function
- Updated `core-functions` dependency to version 2.0.5
### 2.0.4

@@ -176,0 +183,0 @@ - Changes to `logging.js` module:

@@ -20,10 +20,9 @@ 'use strict';

const isLoggingConfigured = logging.isLoggingConfigured;
const configureLogging = logging.configureLogging;
const configureLoggingWithSettings = logging.configureLoggingWithSettings;
const configureDefaultLogging = logging.configureDefaultLogging;
const getDefaultLoggingSettings = logging.getDefaultLoggingSettings;
const configureLoggingIfNotConfigured = logging.configureLoggingIfNotConfigured;
const configureLoggingWithSettingsOrOptions = logging.configureLoggingWithSettingsOrOptions;
const configureLogging = logging.configureLogging;
const booleans = require('core-functions/booleans');
const isBoolean = booleans.isBoolean;
//const isBoolean = booleans.isBoolean;

@@ -120,3 +119,3 @@ const defaultSettings = logging.getDefaultLoggingSettings();

};
const log = configureLogging({}, settings, true);
const log = configureLoggingWithSettings({}, settings, true);

@@ -128,6 +127,6 @@ t.equal(isLoggingConfigured(log), true, 'logging must be configured');

// =====================================================================================================================
// Test configureLogging on existing object with test logger to validate methods
// Test configureLoggingWithSettings on existing object with test logger to validate methods
// =====================================================================================================================
test('configureLogging on existing object with TRACE level with test logger & prefixes', t => {
test('configureLoggingWithSettings on existing object with TRACE level with test logger & prefixes', t => {
const logLevel = TRACE;

@@ -146,3 +145,3 @@ const useLevelPrefixes = true;

};
configureLogging(context, settings, false);
configureLoggingWithSettings(context, settings, false);
t.equal(context.abc, 123, 'context must still be intact');

@@ -157,3 +156,3 @@

test('configureLogging on existing object with DEBUG level with test logger & prefixes', t => {
test('configureLoggingWithSettings on existing object with DEBUG level with test logger & prefixes', t => {
const logLevel = DEBUG;

@@ -172,3 +171,3 @@ const useLevelPrefixes = true;

};
configureLogging(context, settings, false);
configureLoggingWithSettings(context, settings, false);
t.equal(context.abc, 123, 'context must still be intact');

@@ -183,3 +182,3 @@

test('configureLogging on existing object with INFO level with test logger & prefixes', t => {
test('configureLoggingWithSettings on existing object with INFO level with test logger & prefixes', t => {
const logLevel = INFO;

@@ -198,3 +197,3 @@ const useLevelPrefixes = true;

};
configureLogging(context, settings, false);
configureLoggingWithSettings(context, settings, false);
t.equal(context.abc, 123, 'context must still be intact');

@@ -209,3 +208,3 @@

test('configureLogging on existing object with WARN level with test logger & prefixes', t => {
test('configureLoggingWithSettings on existing object with WARN level with test logger & prefixes', t => {
const logLevel = WARN;

@@ -224,3 +223,3 @@ const useLevelPrefixes = true;

};
configureLogging(context, settings, false);
configureLoggingWithSettings(context, settings, false);
t.equal(context.abc, 123, 'context must still be intact');

@@ -235,3 +234,3 @@

test('configureLogging on existing object with ERROR level with test logger & prefixes', t => {
test('configureLoggingWithSettings on existing object with ERROR level with test logger & prefixes', t => {
const logLevel = ERROR;

@@ -250,3 +249,3 @@ const useLevelPrefixes = true;

};
configureLogging(context, settings, false);
configureLoggingWithSettings(context, settings, false);
t.equal(context.abc, 123, 'context must still be intact');

@@ -262,6 +261,6 @@

// =====================================================================================================================
// Test configureLogging on new object with test logger to validate methods
// Test configureLoggingWithSettings on new object with test logger to validate methods
// =====================================================================================================================
test('configureLogging on new object with TRACE level with test logger & prefixes', t => {
test('configureLoggingWithSettings on new object with TRACE level with test logger & prefixes', t => {
const logLevel = TRACE;

@@ -278,3 +277,3 @@ const useLevelPrefixes = false;

};
const log = configureLogging({}, settings, true);
const log = configureLoggingWithSettings({}, settings, true);

@@ -288,3 +287,3 @@ checkEnabledsBasedOnLogLevel(t, log, TRACE);

test('configureLogging on new object with DEBUG level with test logger & prefixes', t => {
test('configureLoggingWithSettings on new object with DEBUG level with test logger & prefixes', t => {
const logLevel = DEBUG;

@@ -301,3 +300,3 @@ const useLevelPrefixes = true;

};
const log = configureLogging({}, settings, true);
const log = configureLoggingWithSettings({}, settings, true);

@@ -311,3 +310,3 @@ checkEnabledsBasedOnLogLevel(t, log, DEBUG);

test('configureLogging on new object with INFO level with test logger & prefixes', t => {
test('configureLoggingWithSettings on new object with INFO level with test logger & prefixes', t => {
const logLevel = INFO;

@@ -324,3 +323,3 @@ const useLevelPrefixes = true;

};
const log = configureLogging({}, settings, true);
const log = configureLoggingWithSettings({}, settings, true);

@@ -334,3 +333,3 @@ checkEnabledsBasedOnLogLevel(t, log, INFO);

test('configureLogging on new object with WARN level with test logger & prefixes', t => {
test('configureLoggingWithSettings on new object with WARN level with test logger & prefixes', t => {
const logLevel = WARN;

@@ -347,3 +346,3 @@ const useLevelPrefixes = true;

};
const log = configureLogging({}, settings, true);
const log = configureLoggingWithSettings({}, settings, true);

@@ -357,3 +356,3 @@ checkEnabledsBasedOnLogLevel(t, log, WARN);

test('configureLogging on new object with ERROR level with test logger & prefixes', t => {
test('configureLoggingWithSettings on new object with ERROR level with test logger & prefixes', t => {
const logLevel = ERROR;

@@ -370,3 +369,3 @@ const useLevelPrefixes = true;

};
const log = configureLogging({}, settings, true);
const log = configureLoggingWithSettings({}, settings, true);

@@ -381,7 +380,7 @@ checkEnabledsBasedOnLogLevel(t, log, ERROR);

// =====================================================================================================================
// Test configureLogging on new object with console (i.e. test real usage for Lambdas) and useLevelPrefixes true
// Test configureLoggingWithSettings on new object with console (i.e. test real usage for Lambdas) and useLevelPrefixes true
// =====================================================================================================================
const useConsoleTrace = false;
test('configureLogging on new object with TRACE level with console & prefixes', t => {
test('configureLoggingWithSettings on new object with TRACE level with console & prefixes', t => {
const logLevel = TRACE;

@@ -398,3 +397,3 @@ const useLevelPrefixes = true;

};
const log = configureLogging({}, settings, true);
const log = configureLoggingWithSettings({}, settings, true);

@@ -408,3 +407,3 @@ checkEnabledsBasedOnLogLevel(t, log, TRACE);

test('configureLogging on new object with DEBUG level with console & prefixes', t => {
test('configureLoggingWithSettings on new object with DEBUG level with console & prefixes', t => {
const logLevel = DEBUG;

@@ -421,3 +420,3 @@ const useLevelPrefixes = true;

};
const log = configureLogging({}, settings, true);
const log = configureLoggingWithSettings({}, settings, true);

@@ -431,3 +430,3 @@ checkEnabledsBasedOnLogLevel(t, log, DEBUG);

test('configureLogging on new object with INFO level with console & prefixes', t => {
test('configureLoggingWithSettings on new object with INFO level with console & prefixes', t => {
const logLevel = INFO;

@@ -444,3 +443,3 @@ const useLevelPrefixes = true;

};
const log = configureLogging({}, settings, true);
const log = configureLoggingWithSettings({}, settings, true);

@@ -454,3 +453,3 @@ checkEnabledsBasedOnLogLevel(t, log, INFO);

test('configureLogging on new object with WARN level with console & prefixes', t => {
test('configureLoggingWithSettings on new object with WARN level with console & prefixes', t => {
const logLevel = WARN;

@@ -467,3 +466,3 @@ const useLevelPrefixes = true;

};
const log = configureLogging({}, settings, true);
const log = configureLoggingWithSettings({}, settings, true);

@@ -477,3 +476,3 @@ checkEnabledsBasedOnLogLevel(t, log, WARN);

test('configureLogging on new object with ERROR level with console & prefixes', t => {
test('configureLoggingWithSettings on new object with ERROR level with console & prefixes', t => {
const logLevel = ERROR;

@@ -490,3 +489,3 @@ const useLevelPrefixes = true;

};
const log = configureLogging({}, settings, true);
const log = configureLoggingWithSettings({}, settings, true);

@@ -502,6 +501,6 @@ checkEnabledsBasedOnLogLevel(t, log, ERROR);

// =====================================================================================================================
// Test configureLogging on new object with console (i.e. test real usage for Lambdas) and useLevelPrefixes false
// Test configureLoggingWithSettings on new object with console (i.e. test real usage for Lambdas) and useLevelPrefixes false
// =====================================================================================================================
test('configureLogging on new object with TRACE level with console & no prefixes', t => {
test('configureLoggingWithSettings on new object with TRACE level with console & no prefixes', t => {
const logLevel = TRACE;

@@ -518,3 +517,3 @@ const useLevelPrefixes = false;

};
const log = configureLogging({}, settings, true);
const log = configureLoggingWithSettings({}, settings, true);

@@ -528,3 +527,3 @@ checkEnabledsBasedOnLogLevel(t, log, TRACE);

test('configureLogging on new object with DEBUG level with console & no prefixes', t => {
test('configureLoggingWithSettings on new object with DEBUG level with console & no prefixes', t => {
const logLevel = DEBUG;

@@ -541,3 +540,3 @@ const useLevelPrefixes = false;

};
const log = configureLogging({}, settings, true);
const log = configureLoggingWithSettings({}, settings, true);

@@ -551,3 +550,3 @@ checkEnabledsBasedOnLogLevel(t, log, DEBUG);

test('configureLogging on new object with INFO level with console & no prefixes', t => {
test('configureLoggingWithSettings on new object with INFO level with console & no prefixes', t => {
const logLevel = INFO;

@@ -564,3 +563,3 @@ const useLevelPrefixes = false;

};
const log = configureLogging({}, settings, true);
const log = configureLoggingWithSettings({}, settings, true);

@@ -574,3 +573,3 @@ checkEnabledsBasedOnLogLevel(t, log, INFO);

test('configureLogging on new object with WARN level with console & no prefixes', t => {
test('configureLoggingWithSettings on new object with WARN level with console & no prefixes', t => {
const logLevel = WARN;

@@ -587,3 +586,3 @@ const useLevelPrefixes = false;

};
const log = configureLogging({}, settings, true);
const log = configureLoggingWithSettings({}, settings, true);

@@ -597,3 +596,3 @@ checkEnabledsBasedOnLogLevel(t, log, WARN);

test('configureLogging on new object with ERROR level with console & no prefixes', t => {
test('configureLoggingWithSettings on new object with ERROR level with console & no prefixes', t => {
const logLevel = ERROR;

@@ -610,3 +609,3 @@ const useLevelPrefixes = false;

};
const log = configureLogging({}, settings, true);
const log = configureLoggingWithSettings({}, settings, true);

@@ -621,6 +620,6 @@ checkEnabledsBasedOnLogLevel(t, log, ERROR);

// =====================================================================================================================
// Ensure that configureLogging on existing object does NOT override previously configured logging if not forced
// Ensure that configureLoggingWithSettings on existing object does NOT override previously configured logging if not forced
// =====================================================================================================================
test('configureLogging on existing object MUST NOT override previously configured logging if not forced', t => {
test('configureLoggingWithSettings on existing object MUST NOT override previously configured logging if not forced', t => {
const useLevelPrefixes = true;

@@ -635,3 +634,3 @@ const useConsole = false;

};
const context = configureLogging({}, settings, true);
const context = configureLoggingWithSettings({}, settings, true);
context.abc = 123;

@@ -649,3 +648,3 @@

};
configureLogging(context, overrideSettings, false);
configureLoggingWithSettings(context, overrideSettings, false);
t.equal(context.abc, 123, 'context must still be intact');

@@ -662,6 +661,6 @@

// =====================================================================================================================
// Ensure that configureLogging on existing object does override previously configured logging if forced
// Ensure that configureLoggingWithSettings on existing object does override previously configured logging if forced
// =====================================================================================================================
test('configureLogging on existing object MUST override previously configured logging if forced', t => {
test('configureLoggingWithSettings on existing object MUST override previously configured logging if forced', t => {
const useLevelPrefixes = true;

@@ -677,3 +676,3 @@ const useConsole = false;

};
const context = configureLogging({}, settings, true);
const context = configureLoggingWithSettings({}, settings, true);
context.abc = 123;

@@ -691,3 +690,3 @@

};
configureLogging(context, overrideSettings, true);
configureLoggingWithSettings(context, overrideSettings, true);
t.equal(context.abc, 123, 'context must still be intact');

@@ -728,3 +727,3 @@

};
configureLogging(context, overrideSettings, true);
configureLoggingWithSettings(context, overrideSettings, true);

@@ -774,3 +773,3 @@ // Now do NOT override with default logging configuration when NOT using force

};
configureLogging(context, overrideSettings, true);
configureLoggingWithSettings(context, overrideSettings, true);

@@ -794,6 +793,6 @@ // Now do NOT override with default logging configuration when NOT using force

// =====================================================================================================================
// Test configureLogging on existing object with test logger to validate methods
// Test configureLoggingWithSettings on existing object with test logger to validate methods
// =====================================================================================================================
test('configureLogging on existing object with test logger & config.logLevel etc.', t => {
test('configureLoggingWithSettings on existing object with test logger & config.logLevel etc.', t => {
const logLevel = defaultSettings.logLevel !== TRACE ? TRACE : ERROR;

@@ -810,3 +809,3 @@ const logger = testLogger(logLevel, false, t);

};
configureLogging(context, settings, false);
configureLoggingWithSettings(context, settings, false);

@@ -825,6 +824,6 @@ t.equal(context.abc, 123, 'context must still be intact');

};
configureLogging(context, overrideSettings, true);
configureLoggingWithSettings(context, overrideSettings, true);
// Now do NOT override with logging configuration again when NOT using force
configureLogging(context, settings, false);
configureLoggingWithSettings(context, settings, false);

@@ -835,3 +834,3 @@ checkEnabledsBasedOnLogLevel(t, context, overrideLogLevel);

// Now override with logging configuration using force
configureLogging(context, settings, true);
configureLoggingWithSettings(context, settings, true);

@@ -846,6 +845,6 @@ t.equal(context.abc, 123, 'context must still be intact');

// =====================================================================================================================
// Test configureLogging with default getDefaultLoggingSettings
// Test configureLoggingWithSettings with default getDefaultLoggingSettings
// =====================================================================================================================
test('configureLogging with default getDefaultLoggingSettings on existing object with test logger & defaults', t => {
test('configureLoggingWithSettings with default getDefaultLoggingSettings on existing object with test logger & defaults', t => {
const logLevel = defaultSettings.logLevel;

@@ -857,3 +856,3 @@ const logger = testLogger(logLevel, false, t);

const defaults = getDefaultLoggingSettings(undefined, logger);
configureLogging(context, defaults, false);
configureLoggingWithSettings(context, defaults, false);

@@ -872,6 +871,6 @@ t.equal(context.abc, 123, 'context must still be intact');

};
configureLogging(context, overrideSettings, true);
configureLoggingWithSettings(context, overrideSettings, true);
// Now do NOT override with default logging configuration when NOT using force
configureLogging(context, defaults, false);
configureLoggingWithSettings(context, defaults, false);

@@ -882,3 +881,3 @@ checkEnabledsBasedOnLogLevel(t, context, overrideLogLevel);

// Now override with default logging configuration using force
configureLogging(context, defaults, true);
configureLoggingWithSettings(context, defaults, true);

@@ -893,6 +892,6 @@ t.equal(context.abc, 123, 'context must still be intact');

// =====================================================================================================================
// Test configureLogging with non-default getDefaultLoggingSettings
// Test configureLoggingWithSettings with non-default getDefaultLoggingSettings
// =====================================================================================================================
test('configureLogging with getDefaultLoggingSettings on existing object with test logger & config.logLevel etc.', t => {
test('configureLoggingWithSettings with getDefaultLoggingSettings on existing object with test logger & config.logLevel etc.', t => {
const logLevel = defaultSettings.logLevel !== TRACE ? TRACE : ERROR;

@@ -909,3 +908,3 @@ const logger = testLogger(logLevel, false, t);

const settings = getDefaultLoggingSettings(options, logger);
configureLogging(context, settings, false);
configureLoggingWithSettings(context, settings, false);

@@ -924,6 +923,6 @@ t.equal(context.abc, 123, 'context must still be intact');

const overrideSettings = getDefaultLoggingSettings(overrideOptions, testLogger(overrideLogLevel, false, t));
configureLogging(context, overrideSettings, true);
configureLoggingWithSettings(context, overrideSettings, true);
// Now do NOT override with logging configuration again when NOT using force
configureLogging(context, settings, false);
configureLoggingWithSettings(context, settings, false);

@@ -934,3 +933,3 @@ checkEnabledsBasedOnLogLevel(t, context, overrideLogLevel);

// Now override with logging configuration using force
configureLogging(context, settings, true);
configureLoggingWithSettings(context, settings, true);

@@ -945,6 +944,6 @@ t.equal(context.abc, 123, 'context must still be intact');

// =====================================================================================================================
// Test configureLoggingIfNotConfigured with settings
// Test configureLogging with settings
// =====================================================================================================================
test('configureLoggingIfNotConfigured with settings', t => {
test('configureLogging with settings', t => {
const logLevel = defaultSettings.logLevel;

@@ -957,3 +956,3 @@ const logger = testLogger(logLevel, false, t);

const options = undefined;
configureLoggingIfNotConfigured(context, settings, options, logger, 'test1');
configureLogging(context, settings, options, logger, false);

@@ -972,6 +971,6 @@ t.equal(context.abc, 123, 'context must still be intact');

};
configureLogging(context, overrideSettings, true);
configureLoggingWithSettings(context, overrideSettings, true);
// Now do NOT override with default logging configuration (since already configured)
configureLoggingIfNotConfigured(context, settings, options, logger, 'test2');
// Now do NOT override with default logging configuration
configureLogging(context, settings, options, logger, false);

@@ -981,5 +980,4 @@ checkEnabledsBasedOnLogLevel(t, context, overrideLogLevel);

// Now override with default logging configuration (by first partially clearing logging from the context)
context.error = undefined;
configureLoggingIfNotConfigured(context, settings, options, logger, 'test3');
// Now override with default logging configuration
configureLogging(context, settings, options, logger, true);

@@ -994,3 +992,3 @@ t.equal(context.abc, 123, 'context must still be intact');

// =====================================================================================================================
// Test configureLoggingIfNotConfigured with options
// Test configureLogging with options
// =====================================================================================================================

@@ -1004,3 +1002,3 @@

const context = {abc: 123};
const settings = undefined; //getDefaultLoggingSettings(options, logger);
const settings = undefined;
const options = {

@@ -1011,3 +1009,3 @@ logLevel: logLevel,

};
configureLoggingIfNotConfigured(context, settings, options, logger, 'test1');
configureLogging(context, settings, options, logger, false);

@@ -1026,6 +1024,6 @@ t.equal(context.abc, 123, 'context must still be intact');

const overrideSettings = getDefaultLoggingSettings(overrideOptions, testLogger(overrideLogLevel, false, t));
configureLogging(context, overrideSettings, true);
configureLoggingWithSettings(context, overrideSettings, true);
// Now do NOT override with logging configuration again (since already configured)
configureLoggingIfNotConfigured(context, settings, options, logger, 'test2');
// Now do NOT override with logging configuration again
configureLogging(context, settings, options, logger, false);

@@ -1035,5 +1033,4 @@ checkEnabledsBasedOnLogLevel(t, context, overrideLogLevel);

// Now override with logging configuration using force (by first partially clearing logging from the context)
context.error = undefined;
configureLoggingIfNotConfigured(context, settings, options, logger, 'test3');
// Now override with logging configuration using force
configureLogging(context, settings, options, logger, true);

@@ -1048,7 +1045,7 @@ t.equal(context.abc, 123, 'context must still be intact');

// =====================================================================================================================
// Test configureLoggingWithSettingsOrOptions with settings
// Test configureLogging with settings AND options
// =====================================================================================================================
test('configureLoggingWithSettingsOrOptions with settings', t => {
const logLevel = defaultSettings.logLevel;
test('configureLogging with settings AND options', t => {
const logLevel = TRACE;
const logger = testLogger(logLevel, false, t);

@@ -1058,6 +1055,8 @@

const context = {abc: 123};
const settings = getDefaultLoggingSettings(undefined, logger);
const options = undefined;
configureLoggingWithSettingsOrOptions(context, settings, options, logger, false);
const settings = {logLevel: TRACE, underlyingLogger: logger};
const options = getDefaultLoggingSettings(undefined, undefined);
configureLogging(context, settings, options, undefined, false);
t.equal(context.abc, 123, 'context must still be intact');

@@ -1075,6 +1074,6 @@ checkEnabledsBasedOnLogLevel(t, context, logLevel);

};
configureLogging(context, overrideSettings, true);
configureLoggingWithSettings(context, overrideSettings, true);
// Now do NOT override with default logging configuration
configureLoggingWithSettingsOrOptions(context, settings, options, logger, false);
configureLogging(context, settings, options, logger, false);

@@ -1085,3 +1084,3 @@ checkEnabledsBasedOnLogLevel(t, context, overrideLogLevel);

// Now override with default logging configuration
configureLoggingWithSettingsOrOptions(context, settings, options, logger, true);
configureLogging(context, settings, options, logger, true);

@@ -1094,50 +1093,1 @@ t.equal(context.abc, 123, 'context must still be intact');

});
// =====================================================================================================================
// 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.4",
"version": "3.0.0",
"author": "Byron du Preez",

@@ -6,0 +6,0 @@ "license": "Apache-2.0",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc