ostrio:loggerfile

v2.0.4Published 7 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

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:

1import { Logger }     from 'meteor/ostrio:logger';
2import { 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)

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:

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

Example 2:

1import { Logger }     from 'meteor/ostrio:logger';
2import { LoggerFile } from 'meteor/ostrio:loggerfile';
3// Initialize Logger:
4const log = new Logger();
5
6// Initialize LoggerFile:
7const LogFile = new LoggerFile(log, {
8  fileNameFormat(time) {
9    // Create log-files hourly
10    return (time.getDate()) + "-" + (time.getMonth() + 1) + "-" + (time.getFullYear()) + "_" + (time.getHours()) + ".log";
11  },
12  format(time, level, message, data, userId) {
13    // Omit Date and hours from messages
14    return "[" + level + "] | " + (time.getMinutes()) + ":" + (time.getSeconds()) + " | \"" + message + "\" | User: " + userId + "\r\n";
15  },
16  path: '/data/logs/' // Use absolute storage path
17});
18
19// Enable LoggerFile with default settings
20LogFile.enable();

Activate and set adapter settings: [Isomorphic]

1import { Logger }     from 'meteor/ostrio:logger';
2import { LoggerFile } from 'meteor/ostrio:loggerfile';
3
4const log = new Logger();
5(new LoggerFile(log)).enable({
6  enable: true,
7  filter: ['ERROR', 'FATAL', 'WARN'], // Filters: 'ERROR', 'FATAL', 'WARN', 'DEBUG', 'INFO', 'TRACE', '*'
8  client: true, // Set to `false` to avoid Client to Server logs transfer
9  server: true  // Allow logging on server
10});

Log message: [Isomorphic]

1import { Logger }     from 'meteor/ostrio:logger';
2import { LoggerFile } from 'meteor/ostrio:loggerfile';
3
4const log = new Logger();
5(new LoggerFile(log)).enable();
6
7/*
8  message {String} - Any text message
9  data    {Object} - [optional] Any additional info as object
10  userId  {String} - [optional] Current user id
11 */
12log.info(message, data, userId);
13log.debug(message, data, userId);
14log.error(message, data, userId);
15log.fatal(message, data, userId);
16log.warn(message, data, userId);
17log.trace(message, data, userId);
18log._(message, data, userId); // Shortcut
19
20// Use with throw
21throw 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', (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};

Use multiple logger(s) with different settings:

1import { Logger }     from 'meteor/ostrio:logger';
2import { LoggerFile } from 'meteor/ostrio:loggerfile';
3
4const log1 = new Logger();
5const log2 = new Logger();
6
7(new LoggerFile(log1)).enable();
8
9(new LoggerFile(log2, {
10  fileNameFormat(time) {
11    return (time.getDate()) + "-" + (time.getMonth() + 1) + "-" + (time.getFullYear()) + "_" + (time.getHours()) + ".log";
12  },
13  format(time, level, message, data, userId) {
14    return "[" + level + "] | " + (time.getMinutes()) + ":" + (time.getSeconds()) + " | \"" + message + "\" | User: " + userId + "\r\n";
15  },
16  path: '/data/logs/'
17})).enable();

Support this awesome package:

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.