Universal Logging for Typescript/JavaScript
Getting Started
Reference Manual
Plugins
FAQ's
v1.x
GitHub
Getting Started
Reference Manual
Plugins
FAQ's
v1.x
GitHub
  • Reference Manual
    • Introduction & Lifecycle
    • Log Class
    • Data Getters
    • Modifiers
    • Terminators
    • Global Store
    • Tools
    • Configuration
    • Middleware
    • Formatters
    • Unit Testing

Global Store

The Global Store is a global cache and control hub for your Adze logs. For a more in depth review of how it works, refer to the Global Store section of the Getting Started guide.

Setup Function

The primary way of configuring the global store is through the setup() function. This function accepts a UserConfiguration value and uses it as a global configuration override.

The setup function also returns a reference to the Global Store instance. You can use this reference to apply listeners among other things.

For a more in depth tutorial on using the setup function, refer to the using the setup function section of the Global Store page of the Getting Started guide.

Interface

function setup<Meta extends Record<string, any> = Record<string, any>>(
  cfg?: UserConfiguration<Meta>
): AdzeGlobal;

Example

import adze, { setup } from 'adze';

// Apply global configuration
const store = setup({
  format: 'json',
});

// Add a log listener
store.addListener('*', (log) => {
  // Do stuff with the logs that trigger this listener.
});

adze.log('This is a log.');
adze.log('This is another log.');

Teardown Function

If you have ran the setup function or have generated at least one Adze log, you will have an $adzeGlobal context value on your global context (window or global).

Sometimes, such as when running unit tests, it is beneficial to remove the global context between tests. Adze exports the teardown function which can be used to remove the global context.

Interface

function teardown(): void;

Example

import adze, { setup, teardown } from 'adze';

// Apply global configuration
const store = setup();

adze.label('counter').count.log('This is a log.');
adze.label('counter').count.log('This is a log.');

teardown();

adze.label('counter').count.log('This is a log.');

Browser Output

browser output teardown example

Server Output

server output teardown example

Public Getters

These are used for retrieving data and accessing the browser console tools.

cache


Retrieves the log cache which is an array of log instances in the order they were generated.

Interface

class AdzeGlobal {
  public get cache(): Log[];
}

Example

import adze, { setup } from 'adze';

const store = setup({
  cache: true,
});

adze.log('This is a log.');
adze.log('This is another log.');

// Get the log cache.
const cache = store.cache; // => [Log, Log]

configuration


Retrieves the global configuration overrides.

Interface

class AdzeGlobal {
  public get configuration(): UserConfiguration;
}

Example

import adze, { setup } from 'adze';

const store = setup({
  format: 'json',
});

// Get the global configuration overrides.
const config = store.configuration; // => { format: 'json' }

pid


Returns an auto-incremented process ID. This is used by some formatters to provide a pid value to the logs.

Interface

class AdzeGlobal {
  public get pid(): number;
}

Example

import adze, { setup } from 'adze';

const store = setup();

// Get the global configuration overrides.
const pid1 = store.pid; // => 1
const pid2 = store.pid; // => 2

tools


Returns a reference to the browser console tools for filtering and recalling logs.

Interface

class AdzeGlobal {
  public get tools(): Tools;
}

Example

import adze, { setup } from 'adze';

const store = setup({
  cache: true,
});

// Use the tools to reprint every cached error log.
store.tools.filterByLevel('error');

Public Methods

These are used for caching logs, setting up log listeners, and creating and modifying labels.

addLogToCache


Adds a log to the log cache.

This is done automatically when log caching is enabled.

Interface

class AdzeGlobal {
  public addLogToCache(log: Log): void;
}

Example

import adze, { setup } from 'adze';

const store = setup();

// If I wanted to manually add a new log to the cache for some reason...
// NOTE: Don't do this! This is for demo purposes only!
store.addLogToCache(new adze());

clearCache


Clears the log cache.

Interface

class AdzeGlobal {
  public clearCache(): void;
}

Example

import adze, { setup } from 'adze';

const store = setup({ cache: true });

adze.log('This is a test log.');

// If I want to clear the cached logs...
store.clearCache();

getLabel


Get a label object by name.

Interface

class AdzeGlobal {
  public getLabel(name: string): Label | undefined;
}

Example

import adze, { setup } from 'adze';

const store = setup({ cache: true });

adze.label('foo').log('This is a test log.');

// Get the label object named foo...
const label = store.getLabel('foo');

setLabel


Get a label object by name.

Interface

class AdzeGlobal {
  public setLabel(name: string, label: Label): void;
}

Example

import adze, { setup } from 'adze';

const store = setup({ cache: true });

adze.label('foo').log('This is a test log.');

// Updating the foo label by setting the count to 1.
// EXAMPLE ONLY: DO NOT DO THIS!
const label = store.setLabel('foo', { name: 'foo', count: 1 });

addListener


Adds a log listener that listens for logs that match the provided level selector and returns the ID of the listener. The ID can be used to remove the listener at a later time by using the removeListener method.

Interface

class AdzeGlobal {
  public addListener(levels: LevelSelector, listener: LogListener): number;
}

Example

import adze, { setup } from 'adze';

const store = setup();

store.addListener('log', (log) => {
  // Do something with the log...
});

adze.label('foo').log('This is a test log.');

removeListener


Removes an existing log listener by its ID. This ID is returned from the addListener method.

Interface

class AdzeGlobal {
  public removeListener(id: number): void;
}

Example

import adze, { setup } from 'adze';

const store = setup();

const id = store.addListener('log', (log) => {
  // Do something with the log...
});

adze.label('foo').log('This is a test log.');

store.removeListener(id);

getListeners


Returns an array of log listener callback functions by the level they target.

Interface

class AdzeGlobal {
  public getListeners(level: number): LogListener[];
}

Example

import adze, { setup } from 'adze';

const store = setup();

store.addListener('log', (log) => {
  // Do something with the log...
});

adze.label('foo').log('This is a test log.');

const logListeners = store.getListeners(6);
Edit this page
Last Updated:
Contributors: Andrew Stacy
Prev
Terminators
Next
Tools