Thorin loggers

Logging is an important part of any application. It outputs in a readable form the things that happen inside your application. As Node.js has its default console logger, thorin provides the default log logging functionality and uses the console to output data.
You can always use loglet.io as the centralized logging storage, by using the loglet plugin.

Thorin enables you to create namespaced loggers for various parts of your application, and enable you to manually configure every one of them. As an example, you might want to create a payment logger for when you process payments, so that if you use centralized logging, it would be easier for you to search after the payment logger name.

The thorin logger can be viewed as a continuous stream of events, with a default handler that outputs to the console. If you want to pipe your log entries to a separate target, you can just use log.pipe() and receive the stream of log events.

Creating a namespaced logger

As previously said, you can create as many namespaced loggers as you wish, calling the function below.

thorin.logger(namespace) : loggerObj
Creates or returns the logger with the given namespace
  • namespacestring*the namespace of the logger we wish to access
Example
'use strict';
const thorin = require('thorin');

const pLogger = thorin.logger('payment');
pLogger.info(`This is my payment logger`);
// outputs [INFO] [2016-08-01 18:16:19.935] [payment] This is my payment logger
log.info(`This is the default logger`);
// outputs [INFO] [2016-08-01 18:16:19.935] [default] This is the default logger

Enabling, disabling and piping console logging

Console logging can be globally enabled or disabled by calling

thorin.logger.disableConsole()
Disables console logging for all namespaces
thorin.logger.enableConsole()
Enables console logging for all namespaces
thorin.logger.replaceConsole()
Replaces the default console.log logs with the default logger.

Logs can be piped either from the global logger or for specific namespaces by calling

thorin.logger.pipe(namespace, fn)
Calls the given function whenever a new log entry is generated.
  • namespacestringThis will only pipe generated log entries from the given namespace.
  • fnfunction*The callback function that will be called whenever a new log entry is generated
A log entry is an object that has the following structure:
  • ts - the timestamp of the log entry, eg: Date.now()
  • name - the namespace of the log entry, defaults to default
  • message - the string representation of the log entry
  • level - the log entry level (see below)
  • args - an array with all the arguments that were given when the log function was called.
Logger levels
  • log.trace
  • log.debug
  • log.info
  • log.warn
  • log.error
  • log.fatal
Do you have a question or is something missing?

You can always create a new issue on GitHub or contact one of the core founders by chat.