/Meteor-logger-file

Meteor Logging: Store application log messages into file (FS)

Primary LanguageJavaScriptBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

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 log file on your Server.

Features:

  • 100% tests coverage;
  • Flexible log level filters;
  • userId is automatically passed and logged if logs is associated with logged-in user;
  • Pass logs from Client to Server;
  • Catch all browser's errors.

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';

Support this awesome package:

Usage

Initialization [Isomorphic]

new LoggerFile(LoggerInstance, options)

  • LoggerInstance {Logger} - from new Logger()
  • options {Object}
  • 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
  • options.path {String} - Log's storage path, absolute, or relative to NodeJS process, note: do not use '~' (path relative to user)

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: function(time) {
    // Create log-files hourly
    return (time.getDate()) + "-" + (time.getMonth() + 1) + "-" + (time.getFullYear()) + "_" + (time.getHours()) + ".log";
  },
  format: function(time, level, message, data, userId) {
    // Omit Date and hours from messages
    return "[" + level + "] | " + (time.getMinutes()) + ":" + (time.getSeconds()) + " | \"" + message + "\" | User: " + userId + "\r\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 = (msg, url, line) => {
  log.error(msg, {file: url, onLine: line});
  if (_GlobalErrorHandler) {
    _GlobalErrorHandler.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: function(time) {
    return (time.getDate()) + "-" + (time.getMonth() + 1) + "-" + (time.getFullYear()) + "_" + (time.getHours()) + ".log";
  },
  format: function(time, level, message, data, userId) {
    return "[" + level + "] | " + (time.getMinutes()) + ":" + (time.getSeconds()) + " | \"" + message + "\" | User: " + userId + "\r\n";
  },
  path: '/data/logs/'
})).enable();

Support this project:

This project can't be possible without ostr.io.

By 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 are available for free.