2012-06-20 23:04:23 +00:00
|
|
|
// Example handling an fs error for a Bunyan-created
|
|
|
|
// stream: we create a logger to a file that is read-only.
|
2012-06-05 06:13:50 +00:00
|
|
|
|
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
2014-07-25 06:36:50 +00:00
|
|
|
var bunyan = require('../lib/bunyan');
|
2012-06-05 06:13:50 +00:00
|
|
|
|
|
|
|
var FILENAME = 'handle-fs-error.log';
|
|
|
|
var S_IWUSR = 00200; // mask for owner write permission in stat mode
|
|
|
|
|
|
|
|
console.warn('- Log file is "%s".', FILENAME);
|
|
|
|
if (!path.existsSync(FILENAME)) {
|
2013-03-29 00:42:32 +00:00
|
|
|
console.warn('- Touch log file.');
|
|
|
|
fs.writeFileSync(FILENAME, 'touch\n');
|
2012-06-05 06:13:50 +00:00
|
|
|
}
|
|
|
|
if (fs.statSync(FILENAME).mode & S_IWUSR) {
|
2013-03-29 00:42:32 +00:00
|
|
|
console.warn('- Make log file read-only.');
|
|
|
|
fs.chmodSync(FILENAME, 0444);
|
2012-06-05 06:13:50 +00:00
|
|
|
}
|
|
|
|
|
2014-08-01 01:49:01 +00:00
|
|
|
console.warn('- Create logger.');
|
|
|
|
var log = bunyan.createLogger({
|
|
|
|
name: 'handle-fs-error',
|
|
|
|
streams: [
|
|
|
|
{path: FILENAME}
|
|
|
|
]
|
|
|
|
});
|
2012-06-05 06:13:50 +00:00
|
|
|
|
|
|
|
log.on('error', function (err) {
|
2013-03-29 00:42:32 +00:00
|
|
|
console.warn('- The logger emitted an error:', err);
|
2012-06-05 06:13:50 +00:00
|
|
|
});
|
|
|
|
|
2014-08-01 01:49:01 +00:00
|
|
|
console.warn('- Call log.info(...).');
|
2012-06-05 06:13:50 +00:00
|
|
|
log.info('info log message');
|
2014-08-01 01:49:01 +00:00
|
|
|
console.warn('- Called log.info(...).');
|
2012-06-05 06:13:50 +00:00
|
|
|
|
|
|
|
setTimeout(function () {
|
2014-08-01 01:49:01 +00:00
|
|
|
console.warn('- Call log.warn(...).');
|
2013-03-29 00:42:32 +00:00
|
|
|
log.warn('warn log message');
|
2014-08-01 01:49:01 +00:00
|
|
|
console.warn('- Called log.warn(...).');
|
2012-06-05 06:13:50 +00:00
|
|
|
}, 1000);
|