ostrio:logger

v2.0.1Published 7 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

Isomorphic logging driver

To use this package install an adapter separately:

  • File - Store application log messages into the file (FS), log rotation included;
  • Mongo - Store application log messages into MongoDB;
  • Console - Print Client's application log messages to Server's console, messages colorized for better readability.

Features:

  • 100% tests coverage;
  • Flexible log level filters, ex: write FATAL, ERROR, and WARN to file, DEBUG to console, and all other to MongoDB;
  • userId is automatically passed and logged if logged data is associated with logged-in user;
  • Pass logs from Client to Server;
  • Catch all browser's errors.

Install:

meteor add ostrio:logger

ES6 Import:

1import { Logger } from 'meteor/ostrio:logger';

Usage

Logger [Isomorphic]

1const log = new Logger();
2
3/* Activate adapters with default settings */
4/* meteor add ostrio:loggerfile */
5new LoggerFile(log).enable();
6/* meteor add ostrio:loggermongo */
7new LoggerMongo(log).enable();
8/* meteor add ostrio:loggerconsole */
9new LoggerConsole(log).enable();
10
11/* Log message
12 * message {String|Number} - Any text message
13 * data    {Object} - [optional] Any additional info as object
14 * userId  {String} - [optional] Current user id
15 */
16log.info(message, data, userId);
17log.debug(message, data, userId);
18log.error(message, data, userId);
19log.fatal(message, data, userId);
20log.warn(message, data, userId);
21log.trace(message, data, userId);
22log._(message, data, userId); //--> Plain shortcut
23
24/* Use with throw */
25throw log.error(message, data, userId);

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

1/* Store original window.onerror */
2const _GlobalErrorHandler = window.onerror;
3
4window.onerror = (msg, url, line) => {
5  log.error(msg, {file: url, onLine: line});
6  if (_GlobalErrorHandler) {
7    _GlobalErrorHandler.apply(this, arguments);
8  }
9};

Register new adapter [Isomorphic]

1/* Emitter function
2 * name        {String}    - Adapter name
3 * emitter     {Function}  - Function called on Meteor.log...
4 * init        {Function}  - Adapter initialization function
5 * denyClient  {Boolean}   - Strictly deny execution on client
6 * denyServer  {Boolean}   - Strictly deny execution on server
7 * Example: log.add(name, emitter, init, denyClient, denyServer);
8 */
9
10const emitter = (level, message, data, userId) => {
11  /* .. do something with a message .. */
12};
13
14const init = () => {
15  /* Initialization function */
16  /* For example create a collection */
17  log.collection = new Meteor.Collection("logs");
18};
19
20log.add('AdapterName', emitter, init, true, false);

Enable/disable adapter and set its settings [Isomorphic]

1/*
2 * name    {String} - Adapter name
3 * options {Object} - Settings object, accepts next properties:
4 * options.enable {Boolean} - Enable/disable adapter
5 * options.filter {Array}   - Array of strings, accepts: 
6 *                            'ERROR', 'FATAL', 'WARN', 'DEBUG', 'INFO', '*'
7 *                            in lowercase and uppercase
8 *                            default: ['*'] - Accept all
9 * options.client {Boolean} - Allow execution on Client
10 * options.server {Boolean} - Allow execution on Server
11 * Example: log.rule(name, options);
12 */
13
14/* Example: */
15log.rule('AdapterName', {
16  enable: true,
17  filter: ['ERROR', 'FATAL', 'WARN'],
18  client: false, /* Allow to call, but not execute on Client */
19  server: true   /* Calls from client will be executed on Server */
20});