Setting Up Adze
Although Adze can be used in a very simple, no-config manner by just importing it and using it, most often we want to centrally create shared loggers that can be imported and used throughout our application.
Create a Shared Logger
To create a shared logger, first create a new file somewhere that makes sense in your application (like ./src/logger.[ts|js]
).
After you have created your file, let's import our dependencies.
// ./src/logger.ts
import adze, { setup } from 'adze';
// ...more setup code will go here
Here you may have noticed the presence of the setup
function that we are importing. This function is used to create a global log store and to apply global configuration to all adze logs.
Now that we have imported our dependencies, let's create and export a shared logger that has emoji's enabled and outputs timestamps with every log. We'll also attach some meta data globally to all loggers using the setup function. We'll go more in-depth on why we we're doing this later.
// ./src/logger.ts
import adze, { setup } from 'adze';
setup({
meta: {
hello: 'world!',
},
});
const logger = adze.withEmoji.timestamp.seal();
export default logger;
The interesting thing about the code above is that we are calling the seal
method instead of a typical log terminator like info
or error
.
What seal
does is it returns a new Log class that inherits any modifiers and configuration from the parent log chain. In this case, it will inherit the modifiers withEmoji
and timestamp
.
Now, let's import and use our new shared logger.
// ./src/index.ts
import logger from './logger';
logger.log('Logging from the index.');
Example output from index.ts
Shared Logger Configuration
In our example above, we created a shared logger by using the seal()
method. Another feature of the seal method is that it allows you to provide an alternative setup configuration as a parameter.
For example, let's say we want to apply a middleware for transporting our logs to a remote API, but we only want to apply it to a specific child logger and not all loggers.
import adze, { setup } from 'adze';
import AdzeTransportFile from '@adze/transport-file';
// Setup our middleware to write logs to rotating log files
const fileTransport = new AdzeTransportFile({ directory: './logs' });
await fileTransport.load();
// Setup our middleware to write logs to rotating log files
const remoteTransport = new RemoteTransport();
await remoteTransport.load();
// Configure our logger globally.
const store = setup({
middleware: [fileTransport], // <-- This middleware applies to all loggers.
});
// Create our logger instance to use throughout our app
const logger = adze.timestamp.ns('MyApp').seal();
// Create another logger that is exclusively used for a remote transport of its logs.
export const remoteLogger = logger.seal({
middleware: [remoteTransport], // <-- This middleware will only apply to our remoteLogger.
});
export default logger;
Notice in the example above we are creating a remoteLogger that inherits its settings from our regular logger but extends the configuration by applying the remoteTransport middleware to it.
All subsequent logs that are generated by the remoteLogger
will not make use of the remoteTransport
middleware.
Keeping this in mind, you can overwrite any setup configuration when you call the seal()
terminator.