Logging: To MongoDB
Store application log messages in MongoDB.
Whenever you log message(s) on Client or Sever, it goes directly into MongoDB.
Installation:
meteor add ostrio:logger # If not yet installed meteor add ostrio:loggermongo
Support this awesome package:
- Star on GitHub
- Star on Atmosphere
- Tweet
- Share on Facebook
Usage
Initialization [Isomorphic]
new LoggerMongo(LoggerInstance, options)
LoggerInstance
{Logger} - fromnew Logger()
options
{Object}options.collection
{Mongo.Collection} - Use to pass your own MongoDB collection instance, {Mongo.Collection} returned fromnew Mongo.Collection()
options.collectionName
{String} - MongoDB collection name, default:ostrioMongoLogger
- Note: You can't pass both
collection
andcollectionName
simultaneously. Set only one of those options. If both options is presentedcollection
is more prioritized - Note: If
collectionName
or no arguments is passed,update
,remove
,insert
is disallowed on the Client
Example:
1// Initialize Logger: 2this.log = new Logger(); 3 4// Initialize LoggerMongo and enable with default settings: 5(new LoggerMongo(log)).enable();
Example 2:
1// Initialize Logger: 2this.log = new Logger(); 3var AppLogs = new Mongo.Collection('AppLogs'); 4 5// Initialize LoggerMongo with own collection instance: 6var LogMongo = new LoggerMongo(log, { 7 collection: AppLogs 8}); 9 10// Enable LoggerMongo with default settings: 11LogMongo.enable();
Example 3:
1// Initialize Logger: 2this.log = new Logger(); 3 4// Initialize LoggerMongo with custom collection name: 5var LogMongo = new LoggerMongo(log, { 6 collectionName: 'AppLogs' 7}); 8 9// Enable LoggerMongo with default settings: 10LogMongo.enable();
Activate with custom adapter settings: [Isomorphic]
1this.log = new Logger(); 2(new LoggerMongo(log, {})).enable({ 3 enable: true, 4 filter: ['ERROR', 'FATAL', 'WARN'], /* Filters: 'ERROR', 'FATAL', 'WARN', 'DEBUG', 'INFO', 'TRACE', '*' */ 5 client: false, /* This allows to call, but not execute on Client */ 6 server: true /* Calls from client will be executed on Server */ 7});
Logging Collection Schema:
1({ 2 userId: { 3 type: String 4 }, 5 date: { 6 type: Date 7 }, 8 timestamp: { 9 type: Number 10 }, 11 level: { 12 type: String 13 }, 14 message: { 15 type: String 16 }, 17 additional: { 18 type: Object 19 } 20});
Set custom indexes on collection: [Server]
Read more at: ensureIndex docs
1this.log = new Logger(); 2var LogMongo = new LoggerMongo(log, { 3 collectionName: 'AppLogs' /* Use custom collection name */ 4}); 5 6if (Meteor.isServer) { 7 LogMongo.collection._ensureIndex({level: 1}, {background: true}); 8 LogMongo.collection._ensureIndex({userId: 1}, {background: true}); 9 LogMongo.collection._ensureIndex({date: 1}, {background: true}); 10 LogMongo.collection._ensureIndex({timestamp: 1}, {background: true}); 11}
Log message: [Isomorphic]
1this.log = new Logger(); 2(new LoggerMongo(log)).enable(); 3 4/* 5 message {String} - Any text message 6 data {Object} - [optional] Any additional info as object 7 userId {String} - [optional] Current user id 8 */ 9log.info(message, data, userId); 10log.debug(message, data, userId); 11log.error(message, data, userId); 12log.fatal(message, data, userId); 13log.warn(message, data, userId); 14log.trace(message, data, userId); 15log._(message, data, userId); //--> Plain log without level 16 17/* Use with throw */ 18throw 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};
Use multiple logger(s) with different settings: [Isomorphic]
1this.log1 = new Logger(); 2this.log2 = new Logger(); 3 4/* 5 * Separate settings and collection 6 * for info, debug and other messages 7 */ 8(new LoggerMongo(log1, { 9 collectionName: 'AppLogs' 10})).enable({ 11 filter: ['DEBUG', 'INFO', 'LOG', 'TRACE'], 12 client: false, 13 server: true 14}); 15 16/* 17 * Separate settings and collection 18 * for errors, exceptions, warnings and etc. 19 */ 20(new LoggerMongo(log2, { 21 collectionName: 'AppErrors' 22})).enable({ 23 filter: ['ERROR', 'FATAL', 'WARN'], 24 client: false, 25 server: true 26});