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.
meteor add ostrio:logger # If not yet installed
meteor add ostrio:loggerfile
import { Logger } from 'meteor/ostrio:logger';
import { LoggerFile } from 'meteor/ostrio:loggerfile';
- 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
Initialize Logger
instance and pass it into LoggerInstance
constructor to enable logging into the log file.
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
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();
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();
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
});
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);
/* 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', (err) => {
bound(() => {
log.error('Server Crashed!', err);
console.error(err.stack);
process.exit(7);
});
});
// 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);
};
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();
- Clone this package
- In Terminal (Console) go to directory where package is cloned
- Then run:
meteor test-packages ./
- Star on GitHub
- Star on Atmosphere
- Tweet
- Share on Facebook
- Sponsor via GitHub
- Support via PayPal
- Use ostr.io β Monitoring, Analytics, WebSec, Web-CRON and Pre-rendering for a website