ostrio:logger

v1.1.0Published 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 you need to install an adapter:

  • File - Simply store application logs into file within ostrio:logger package;
  • Mongo - Simply store application logs into MongoDB within ostrio:logger package;
  • Console - Simply output Client application logs into Server's console within ostrio:logger package.
Logger [Isomorphic]
1this.Log = new Logger();
2/*
3  message {String|Number} - Any text message
4  data    {Object} - [optional] Any additional info as object
5  userId  {String} - [optional] Current user id
6 */
7Log.info(message, data, userId);
8Log.debug(message, data, userId);
9Log.error(message, data, userId);
10Log.fatal(message, data, userId);
11Log.warn(message, data, userId);
12Log.trace(message, data, userId);
13Log._(message, data, userId); //--> Shortcut for logging without message, e.g.: simple plain log
14
15/* Use with throw */
16throw Log.error(message, data, userId);
Catch-all client's errors: [CLIENT]
1/* Store original window.onerror */
2this.Log = new Logger();
3/* Log to file: */
4/* https://github.com/VeliovGroup/Meteor-logger-file */
5new LoggerFile(Log).enable();
6var _WoE = window.onerror;
7
8window.onerror = function(msg, url, line) {
9  Log.error(msg, {file: url, onLine: line});
10  if (_WoE) {
11    _WoE.apply(this, arguments);
12  }
13};
Register new adapter [Isomorphic]
1/*
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, only pass via Meteor.methods
6  Example: Log.add(name, emitter, init, denyClient);
7 */
8
9var emitter = function(level, message, data, userId){
10  Log.collection.insert({
11    userId: userId,
12    level: level,
13    message: message,
14    additional: data
15  });
16};
17
18var init = function(){
19  Log.collection = new Meteor.Collection("logs");
20};
21
22Log.add('Mongo', emitter, init, true);
Enable/disable adapter and set it's 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/* Examples: */
15Log.rule('File', {
16  enable: true,
17  filter: ['ERROR', 'FATAL', 'WARN'],
18  client: false, /* This allows to call, but not execute on Client */
19  server: true   /* Calls from client will be executed on Server */
20});
21
22Log.rule('Console', {
23  enable: true,
24  filter: ['*'],
25  client: true,
26  server: true
27});
28
29Log.rule('Mongo',{
30  enable: true
31});