Logger driver for Meteor.js with different adapters. 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.
- ๐ทโโ๏ธ 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, data is associated with logged-in user; - ๐ Pass logs from Client to Server;
- ๐ท Catch all browser's errors.
meteor add ostrio:logger
import { Logger } from 'meteor/ostrio:logger';
Initiate a logger and pass it into the adapter constructor
const log = new Logger();
/* Activate adapters with default settings */
/* meteor add ostrio:loggerfile */
new LoggerFile(log).enable();
/* meteor add ostrio:loggermongo */
new LoggerMongo(log).enable();
/* meteor add ostrio:loggerconsole */
new LoggerConsole(log).enable();
/* Log message
* message {String|Number} - Any text message
* data {Object} - [optional] Any additional info as object
* userId {String} - [optional] Current user id
*/
log.info(message, data, userId);
log.debug(message, data, userId);
log.error(message, data, userId);
log.fatal(message, data, userId);
log.warn(message, data, userId);
log.trace(message, data, userId);
log._(message, data, userId); //--> Plain shortcut
/* Use with throw */
throw log.error(message, data, userId);
/* Store original window.onerror */
const _GlobalErrorHandler = window.onerror;
window.onerror = function (msg, url, line) {
log.error(msg, {file: url, onLine: line});
if (_GlobalErrorHandler) {
_GlobalErrorHandler.apply(this, arguments);
}
};
const bound = Meteor.bindEnvironment((callback) => {callback();});
process.on('uncaughtException', function (err) {
bound(() => {
log.error('Server Crashed!', err);
console.error(err.stack);
process.exit(7);
});
});
// store original Meteor error
const originalMeteorDebug = Meteor._debug;
Meteor._debug = (message, stack) => {
const error = new Error(message);
error.stack = stack;
log.error('Meteor Error!', error);
return originalMeteorDebug.apply(this, arguments);
};
Mainly should be used by adapter developers, a.k.a. developer API.
/**
* Logger#add() โ register new adapter
*
* Emitter function
* name {String} - Adapter name
* emitter {Function} - Function called on Meteor.log...
* init {Function} - Adapter initialization function
* denyClient {Boolean} - Strictly deny execution on client
* denyServer {Boolean} - Strictly deny execution on server
* Example: log.add(name, emitter, init, denyClient, denyServer);
*/
const emitter = (level, message, data, userId) => {
/* .. do something with a message .. */
};
const init = () => {
/* Initialization function */
/* For example create a collection */
log.collection = new Meteor.Collection('logs');
};
log.add('AdapterName', emitter, init, true, false);
/**
* Logger#rule() โ register adapter default rules
*
* name {String} - Adapter name
* options {Object} - Settings object, accepts next properties:
* options.enable {Boolean} - Enable/disable adapter
* options.filter {Array} - Array of strings, accepts:
* 'ERROR', 'FATAL', 'WARN', 'DEBUG', 'INFO', '*'
* in lowercase and uppercase
* default: ['*'] - Accept all
* options.client {Boolean} - Allow execution on Client
* options.server {Boolean} - Allow execution on Server
* Example: log.rule(name, options);
*/
/* Example: */
log.rule('AdapterName', {
enable: true,
filter: ['ERROR', 'FATAL', 'WARN'],
client: false, /* Allow to call, but not execute on Client */
server: true /* Calls from client will be executed on Server */
});
- Clone this package
- In Terminal (Console) go to directory where package is cloned
- Then run:
# Default
meteor test-packages ./
# With custom port
meteor test-packages ./ --port 8888
# With local MongoDB and custom port
MONGO_URL="mongodb://127.0.0.1:27017/logger-tests" meteor test-packages ./ --port 8888
- Sponsor via GitHub
- Support via PayPal
- Use ostr.io โ Monitoring, Analytics, WebSec, Web-CRON and Pre-rendering for a website