base.Logger(loggerName)

Fancy-ish log messages (likely over engineered).

Console nested message groups can be started and ended using the special intro/outro method pairs, entered/leaving and starting/finished. By default, the former are opened and the latter collapsed (documented here as closed).

Individual Loggers can be enabled/disabled by setting the enabled boolean property.

Each Logger will have also have a collection of Groups associated with it. These groups can have one of three modes: opened, closed, silenced. The first two correspond to the browser console nested message groups. The intro and outro type of methods will handle the nesting. If a group is set as silenced, no messages will be sent to the console.

All Logger instances register a configuration with a singleton Map keyed by the instance name. If more than one instance is created with the same name, they all share the same configuration.

Configurations can be exported as a plain object and reimported using the configs property. The object could be saved via the userscript manager. Depending on which manager, it may have to be processed with the JSON.{stringify,parse} functions. Once exported, the object may be modified. This could be used to provide a UI to edit the object, though no schema is provided.

Some values may be of interest to users for help in debugging a script.

The callCount value is how many times a logger would have been used for messages, even if the logger is disabled. Similarly, each group associated with a logger also has a callCount. These values can be used to determine which loggers and groups generate a lot of messages and could be disabled or silenced.

The sequence value is a rough indicator of how recently a logger or group was actually used. It is purposely not a timestamp, but rather, more closely associated with how often configurations are restored, e.g. during web page reloads. A low sequence number, relative to the others, may indicate a logger was renamed, groups removed, or simply parts of an application that have not been visited recently. Depending on the situation, one could clean up old configs, or explore other parts of the script.

Handlers may be attached to listen for changes to the configuration singleton using the onConfig and offConfig static methods. Arrival of messages may controlled using configDispatcherEnabled (useful for disabling during testing).

new Logger(loggerName)

Parameters:
Name Type Description
loggerName string

Name for this logger.

Example
const log = new Logger('Bob');
foo(x) {
 const me = foo.name;
 log.entered(me, x);
 ... do stuff ...
 log.starting('loop');
 for (const item of items) {
   log.log(`Processing ${item}`);
   ...
 }
 log.finished('loop');
 log.leaving(me, y);
 return y;
}

Logger.config(log.name).enabled = true;
Logger.config(log.name).group('foo').mode = 'silenced';

GM.setValue('Logger', Logger.configs);
... restart browser ...
Logger.configs = GM.getValue('Logger');

Classes

Config

Group

GroupMode

Members

includeStackTrace :boolean

mq :module:base.MessageQueue

name :string

silenced :boolean

static configDispatcherEnabled :boolean

static configs :object

static loggers :Array.<string>

Methods

entered(group, …rest)

Indicate entered a specific group.

Parameters:
Name Type Attributes Description
group string

Group that was entered.

rest object <repeatable>

Arbitrary items to pass to console.debug.

finished(group, …rest)

Indicate finished with a specific collapsed group.

Parameters:
Name Type Attributes Description
group string

Group that was entered.

rest object <repeatable>

Arbitrary items to pass to console.debug.

leaving(group, …rest)

Indicate leaving a specific group.

Parameters:
Name Type Attributes Description
group string

Group leaving.

rest object <repeatable>

Arbitrary items to pass to console.debug.

log(msg, …rest)

Log a specific message.

Parameters:
Name Type Attributes Description
msg string

Message to send to console.debug.

rest object <repeatable>

Arbitrary items to pass to console.debug.

starting(group, …rest)

Indicate starting a specific collapsed group.

Parameters:
Name Type Attributes Description
group string

Group that is being started.

rest object <repeatable>

Arbitrary items to pass to console.debug.

static clear()

Clear the console.

static config(loggerName) → {module:base.Logger~Config}

Get configuration of a specific Logger.

Parameters:
Name Type Description
loggerName string

Logger configuration to get.

Returns:
module:base.Logger~Config -

Current config for that Logger.

static offConfig(callback)

Remove all instances of a function attached for config change events.

Parameters:
Name Type Description
callback module:base.Logger~ConfigMutationHandler

Function that receives events.

static onConfig(callback)

Attach a function for all config change events.

Parameters:
Name Type Description
callback module:base.Logger~ConfigMutationHandler

Function that receives events.

static resetConfigs()

Reset all configs to an empty state.

Type Definitions

ConfigMutationHandler(evt, record)

Parameters:
Name Type Description
evt string

Type of mutation.

record module:base.Logger~ConfigMutationRecord

Details about the mutation.

ConfigMutationRecord

Properties:
Name Type Attributes Description
logger string

Name of the affected Logger instance.

group string <nullable>

Name of a possibly affected group.