Closes #2471 - Add logging to a file

- Refactored the JSONStream to be a through stream
- Closes #2471
- Refactored how we handle JSONStreams. They now need to be piped to the
  destination
This commit is contained in:
Chris Cowan 2015-02-27 12:16:37 -07:00
parent d31be9aa73
commit 5d46991d8e
6 changed files with 43 additions and 21 deletions

View file

@ -56,7 +56,8 @@
"request": "^2.40.0",
"requirefrom": "^0.2.0",
"semver": "^4.2.0",
"serve-favicon": "~2.2.0"
"serve-favicon": "~2.2.0",
"through": "^2.3.6"
},
"devDependencies": {
"connect": "~2.19.5",

View file

@ -18,6 +18,7 @@ program.option('-c, --config <path>', 'Path to the config file');
program.option('-p, --port <port>', 'The port to bind to', parseInt);
program.option('-q, --quiet', 'Turns off logging');
program.option('-H, --host <host>', 'The host to bind to');
program.option('-l, --log-file <path>', 'The file to log to');
program.option('--plugins <path>', 'Path to scan for plugins');
program.parse(process.argv);
@ -49,6 +50,10 @@ if (program.quiet) {
config.quiet = program.quiet;
}
if (program.logFile) {
config.log_file = program.logFile;
}
if (program.host) {
config.host = program.host;
}

View file

@ -45,7 +45,8 @@ var config = module.exports = {
kibana : kibana,
package : require(packagePath),
htpasswd : htpasswdPath,
buildNum : '@@buildNum'
buildNum : '@@buildNum',
log_file : kibana.log_file || null
};
config.plugins = listPlugins(config);

View file

@ -49,3 +49,6 @@ verify_ssl: true
# Set the path to where you would like the process id file to be created.
# pid_file: /var/run/kibana.pid
# If you would like to send the log output to a file you can set the path below.
# This will also turn off the STDOUT log output.
# log_file: ./kibana.log

View file

@ -1,6 +1,5 @@
var _ = require('lodash');
var Writable = require('stream').Writable;
var util = require('util');
var through = require('through');
var levels = {
10: 'trace',
@ -11,14 +10,7 @@ var levels = {
60: 'fatal'
};
function JSONStream(options) {
options = options || {};
Writable.call(this, options);
}
util.inherits(JSONStream, Writable);
JSONStream.prototype._write = function (entry, encoding, callback) {
function write(entry) {
entry = JSON.parse(entry.toString('utf8'));
var env = process.env.NODE_ENV || 'development';
@ -36,8 +28,13 @@ JSONStream.prototype._write = function (entry, encoding, callback) {
if (!output.message) output.message = output.error.message;
}
process.stdout.write(JSON.stringify(output) + "\n");
callback();
};
this.queue(JSON.stringify(output) + '\n');
}
module.exports = JSONStream;
function end() {
this.queue(null);
}
module.exports = function () {
return through(write, end);
};

View file

@ -2,18 +2,33 @@ var _ = require('lodash');
var morgan = require('morgan');
var env = process.env.NODE_ENV || 'development';
var bunyan = require('bunyan');
var fs = require('fs');
var StdOutStream = require('./StdOutStream');
var JSONStream = require('./JSONStream');
var createJSONStream = require('./createJSONStream');
var config = require('../config');
var stream = { stream: new JSONStream() };
var streams = [];
// Set the default stream based on the enviroment. If we are on development then
// then we are going to create a pretty stream. Everytyhing else will get the
// JSON stream to stdout.
var defaultStream;
if (env === 'development') {
stream.stream = new StdOutStream();
defaultStream = new StdOutStream();
} else {
defaultStream = createJSONStream()
.pipe(process.stdout);
}
if (!config.quiet) {
streams.push(stream);
// If we are not being oppressed and we are not sending the output to a log file
// push the default stream to the list of streams
if (!config.quiet && !config.log_file) {
streams.push({ stream: defaultStream });
}
// Send the stream to a file using the json format.
if (config.log_file) {
var fileStream = fs.createWriteStream(config.log_file);
streams.push({ stream: createJSONStream().pipe(fileStream) });
}
var logger = module.exports = bunyan.createLogger({