ostrio:logger

v1.1.2Published 8 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

Logger package to be used with any adapter, ex.: MongoDB, Log files, Server and/or Client console. With range of settings, like Server and/or Client execution, filters by log levels (types, like warn, info, etc.).

Install:

meteor add ostrio:logger

Usage

To use this package install an adapter separately:

  • File - Store application log messages into file (FS);
  • Mongo - Store application log messages into MongoDB;
  • Console - Print Client's application log messages to Server's console, messages colorized for better readability.
Logger [Isomorphic]
1this.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 log without level
23
24/* Use with throw */
25throw log.error(message, data, userId);
Catch-all Client's errors example: [CLIENT]
1/* Store original window.onerror */
2var _WoE = window.onerror;
3
4window.onerror = function(msg, url, line) {
5  log.error(msg, {file: url, onLine: line});
6  if (_WoE) {
7    _WoE.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 * Example: log.add(name, emitter, init, denyClient);
7 */
8
9var emitter = function(level, message, data, userId){
10  /* .. do something with a message .. */
11};
12
13var init = function(){
14  /* Initialization function */
15  /* For example create a collection */
16  log.collection = new Meteor.Collection("logs");
17};
18
19log.add('AdapterName', emitter, init, true);
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});