/Meteor-logger-console

Meteor Logging: Print Client's log messages into Server's console with nice highlighting

Primary LanguageJavaScriptBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

Logging: To Console

Console adapter for logger driver. Print Client's log messages to Server's console package. All client messages is enhanced with colors and extra styles for better readability.

This package is not limited to transferring Client log messages to Server. It can be used on Client or Server only, or for printing colorized messages in Client.

Features:

  • Flexible log level filters;
  • userId is automatically passed and logged if logs is associated with logged-in user;
  • Pass logs from Client to Server;
  • Catch all browser's errors.

Client example:

client example

Installation:

meteor add ostrio:logger # If not yet installed
meteor add workcapital1:loggerconsole

ES6 Import:

import { Logger }        from 'meteor/ostrio:logger';
import { LoggerConsole } from 'meteor/workcapital1:loggerconsole';

Usage

Initialization [Isomorphic]

new LoggerConsole(LoggerInstance, settings)

  • LoggerInstance {Logger} - from new Logger()
  • settings {Object}
  • settings.format {Function} - This function must return String. Arguments:
    • opts {Object}
    • opts.userId {String}
    • opts.time {Date} - Report date
    • opts.level {String} - Message level, one of: ERROR, FATAL, WARN, DEBUG, INFO, TRACE, LOG, *
    • opts.message {String} - Report message
    • opts.data {Object} - Additional info passed as object

Example: [Isomorphic]

import { Logger }        from 'meteor/ostrio:logger';
import { LoggerConsole } from 'meteor/workcapital1:loggerconsole';

// Initialize Logger:
const log = new Logger();
// Initialize and enable LoggerConsole with default settings:
(new LoggerConsole(log)).enable();

// Initialize and enable LoggerConsole with custom formatting:
(new LoggerConsole(log, {
  format(opts) {
    return ((Meteor.isServer) ? '[SERVER]' : "[CLIENT]") + ' [' + opts.level + '] - ' + opts.message;
  }
})).enable();

Activate with custom adapter settings: [Isomorphic]

import { Logger }        from 'meteor/ostrio:logger';
import { LoggerConsole } from 'meteor/workcapital1:loggerconsole';

const log = new Logger();
(new LoggerConsole(log)).enable({
  enable: true,
  filter: ['ERROR', 'FATAL', 'WARN'], /* Filters: 'ERROR', 'FATAL', 'WARN', 'DEBUG', 'INFO', 'TRACE', '*' */
  client: true, // Set to `false` to avoid log transfer from Client to Server
  server: true  // Set to `false` to disallow execution on Server
});
Log message: [Isomorphic]
import { Logger }        from 'meteor/ostrio:logger';
import { LoggerConsole } from 'meteor/workcapital1:loggerconsole';

const log = new Logger();
(new LoggerConsole(log)).enable();

/*
  message {String} - Any text message
  data    {Object} - [optional] Any additional info as object
  userId  {String} - [optional] Current user id
 */
log.info(message, data, userId);
log.debug(message, data, userId);
log.error(message, data, userId);
log.fatal(message, data, userId);
log.warn(message, data, userId);
log.trace(message, data, userId);
log._(message, data, userId); // Shortcut

// Use with throw
throw log.error(message, data, usmerId);

Catch-all Client's errors example: [Client]

/* Store original window.onerror */
const _GlobalErrorHandler = window.onerror;

window.onerror = (msg, url, line) => {
  log.error(msg, {file: url, onLine: line});
  if (_GlobalErrorHandler) {
    _GlobalErrorHandler.apply(this, arguments);
  }
};

Use multiple logger(s) with different settings: [Isomorphic]

const log1 = new Logger();
const log2 = new Logger();

(new LoggerConsole(log1)).enable({
  filter: ['*'],
  client: true,
  server: true
});

(new LoggerConsole(log2)).enable({
  filter: ['ERROR', 'FATAL'],
  client: true,
  server: true
});