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
, andWARN
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.
Installation:
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};
Catch-all Server's errors example: [Server]
1const bound = Meteor.bindEnvironment((callback) => {callback();}); 2process.on('uncaughtException', function (err) { 3 bound(() => { 4 log.error("Server Crashed!", err); 5 console.error(err.stack); 6 process.exit(7); 7 }); 8};
Catch-all Meteor's errors example: [Server]
1// store original Meteor error 2const originalMeteorDebug = Meteor._debug; 3Meteor._debug =(message, stack) => { 4 const error = new Error(message); 5 error.stack = stack; 6 log.error('Meteor Error!', error); 7 return originalMeteorDebug.apply(this, arguments); 8 }; 9};
Register new adapter [Isomorphic]
Mainly should be used by adapter developers, a.k.a. developer API.
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});
Support this project:
This project wouldn't be possible without ostr.io.
Using ostr.io you are not only protecting domain names, monitoring websites and servers, using Prerendering for better SEO of your JavaScript website, but support our Open Source activity, and great packages like this one could be available for free.