Skip to content

πŸ”– Meteor Logging: Store application log messages into file (FS)

License

Notifications You must be signed in to change notification settings

veliovgroup/Meteor-logger-file

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

67 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

support support

Logging: To File

File adapter for logger driver. Store application log messages (from both Client and Server) in the file. By default log file is rotated daily, you can easily adjust it to hourly, monthly, or any other period, via fileNameFormat.

Whenever you log message(s) on Client or Sever, it goes directly to a log file on your Server.

Features:

  • πŸ€“ Support Windows, Linux, and MacOS environments;
  • πŸ‘·β€β™‚οΈ 100% tests coverage;
  • πŸ’ͺ Flexible log level filters;
  • πŸ‘¨β€πŸ’» userId is automatically passed and logged, data is associated with logged-in user;
  • πŸ“Ÿ Pass logs from Client to Server;
  • πŸ•· Catch all browser's errors and exceptions.

Installation:

meteor add ostrio:logger # If not yet installed
meteor add ostrio:loggerfile

ES6 Import:

import { Logger } from 'meteor/ostrio:logger';
import { LoggerFile } from 'meteor/ostrio:loggerfile';

FAQ:

  • Q: Where to find the log file?
    • A: On dev stage: /static/logs. On prod stage: /assets/app/logs. Change this behavior with options.path (see below)
  • Q: Log files are gone, why?
    • A: All logs will be removed as soon as your application rebuilds or you run meteor reset. To keep your logs persistent during development use an absolute path outside of your project folder, e.g. /logs directory. Make sure selected directory is writable by node/meteor's process owner

Usage

Initialize Logger instance and pass it into LoggerInstance constructor to enable logging into the log file.

Initialization [Isomorphic]

new LoggerFile(LoggerInstance, options)

  • LoggerInstance {Logger} - from new Logger()
  • options {Object}
  • options.path {String} - Log's storage path, absolute, or relative to NodeJS process, note: do not use '~' (path relative to user)
  • options.fileNameFormat {Function} - Log file name, use to adjust file creation frequency, arguments:
    • time {Date}
  • options.format {Function} - Log record format, arguments:
    • time {Date}
    • level {String} - 'ERROR', 'FATAL', 'WARN', 'DEBUG', 'INFO', 'TRACE'
    • message {String}
    • data {Object}
    • userId {String} - set if user is logged in and package accounts-base is installed
    • Note: Do not forget \r\n at the end of record-line

Example:

import { Logger } from 'meteor/ostrio:logger';
import { LoggerFile } from 'meteor/ostrio:loggerfile';
// Initialize Logger:
const log = new Logger();
// Initialize and enable LoggerFile with default settings:
(new LoggerFile(log)).enable();

Example 2:

import { Logger } from 'meteor/ostrio:logger';
import { LoggerFile } from 'meteor/ostrio:loggerfile';
// Initialize Logger:
const log = new Logger();

// Initialize LoggerFile:
const logFile = new LoggerFile(log, {
  fileNameFormat(time) {
    // Create/Rotate log-files hourly
    return `${time.getDate()}-${time.getMonth() + 1}-${time.getFullYear()}_${time.getHours()}.log`;
  },
  format(time, level, message, data, userId) {
    // Omit Date and hours from messages
    return `[${level}] | ${time.getMinutes()}:${time.getSeconds()} | ${message}' | User: ${userId}\n`;
  },
  path: '/data/logs/' // Use absolute storage path
});

// Enable LoggerFile with default settings
logFile.enable();

Activate and set adapter settings: [Isomorphic]

import { Logger } from 'meteor/ostrio:logger';
import { LoggerFile } from 'meteor/ostrio:loggerfile';

const log = new Logger();
(new LoggerFile(log)).enable({
  enable: true,
  filter: ['ERROR', 'FATAL', 'WARN'], // Filters: 'ERROR', 'FATAL', 'WARN', 'DEBUG', 'INFO', 'TRACE', '*'
  client: true, // Set to `false` to avoid Client to Server logs transfer
  server: true  // Allow logging on server
});

Log message: [Isomorphic]

import { Logger } from 'meteor/ostrio:logger';
import { LoggerFile } from 'meteor/ostrio:loggerfile';

const log = new Logger();
(new LoggerFile(log)).enable();

/*
  message {String} - 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); // Shortcut

// Use with throw
throw log.error(message, data, userId);

Catch-all Client's errors example: [Client]

/* 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);
  }
};

Catch-all Server's errors example: [Server]

const bound = Meteor.bindEnvironment((callback) => {callback();});
process.on('uncaughtException', (err) => {
  bound(() => {
    log.error('Server Crashed!', err);
    console.error(err.stack);
    process.exit(7);
  });
});

Catch-all Meteor's errors example: [Server]

// store original Meteor error
const originalMeteorDebug = Meteor._debug;
Meteor._debug = function (message, stack) {
  const error = new Error(message);
  error.stack = stack;
  log.error('Meteor Error!', error);
  return originalMeteorDebug.apply(this, arguments);
};

Use multiple logger(s) with different settings:

import { Logger } from 'meteor/ostrio:logger';
import { LoggerFile } from 'meteor/ostrio:loggerfile';

const log1 = new Logger();
const log2 = new Logger();

(new LoggerFile(log1)).enable();

(new LoggerFile(log2, {
  fileNameFormat(time) {
    return (time.getDate()) + '-' + (time.getMonth() + 1) + '-' + (time.getFullYear()) + '_' + (time.getHours()) + '.log';
  },
  format(time, level, message, data, userId) {
    return `[${level}] | ${time.getMinutes()}:${time.getSeconds()} | ${message}' | User: ${userId}\n`;
  },
  path: '/data/logs/'
})).enable();

Running Tests

  1. Clone this package
  2. In Terminal (Console) go to directory where package is cloned
  3. Then run:

Meteor/Tinytest

meteor test-packages ./

Support this awesome package:

Support our open source contribution: