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:
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 withoptions.path
(see below)
- A: On dev stage:
- 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 absolutepath
outside of your project folder, e.g./logs
directory. Make sure selected directory is writable by node/meteor's process owner
- A: All logs will be removed as soon as your application rebuilds or you run
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} - fromnew 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 packageaccounts-base
is installed- Note: Do not forget
\r\n
at the end of record-line
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 = function (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 = function (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};
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();
Running Tests
- Clone this package
- In Terminal (Console) go to directory where package is cloned
- Then run:
Meteor/Tinytest
meteor test-packages ./
Support this awesome package:
- Star on GitHub
- Star on Atmosphere
- Tweet
- Share on Facebook
Support our open source contribution:
- Sponsor via GitHub
- Support via PayPal
- Use ostr.io — Monitoring, Analytics, WebSec, Web-CRON and Pre-rendering for a website