Logging: To MongoDB
Store application logs into MongoDB within ostrio:logger package.
Whenever you log message(s) on client or sever, it goes directly to MongoDB.
Installation:
meteor add ostrio:logger # If not yet installed meteor add ostrio:loggermongo
Usage
Initialization [Isomorphic]
new LoggerMongo(LoggerInstance, options)
LoggerInstance
{Logger} - fromnew Logger()
options
{Object}options.collectionName
{String} - MongoDB collection name, default:ostrioMongoLogger
Example:
1this.Log = new Logger(); 2var LogMongo = new LoggerMongo(Log, { 3 collectionName: 'AppLogs' /* Use custom collection name */ 4});
Activate and set adapter settings [Isomorphic]
1this.Log = new Logger(); 2new 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:
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 [Isomorphic]
1this.Log = new Logger(); 2new 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); //--> Shortcut for logging without message, e.g.: simple plain log 16 17/* Use with throw */ 18throw Log.error(message, data, userId);
Use multiple logger(s) with different settings:
1this.Log1 = new Logger(); 2this.Log2 = new Logger(); 3 4/* 5 * Separate settings and collection 6 * for info, debug and other messages 7 */ 8new 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 */ 20new LoggerMongo(Log2, { 21 collectionName: 'AppErrors' 22}).enable({ 23 filter: ['ERROR', 'FATAL', 'WARN'], 24 client: false, 25 server: true 26});