Skip to content

Commit

Permalink
Closes elastic#2471 - Add logging to a file
Browse files Browse the repository at this point in the history
- Refactored the JSONStream to be a through stream
- Closes elastic#2471
- Refactored how we handle JSONStreams. They now need to be piped to the
  destination
  • Loading branch information
simianhacker committed Feb 27, 2015
1 parent d31be9a commit 5d46991
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 21 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions src/server/bin/kibana.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion src/server/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
3 changes: 3 additions & 0 deletions src/server/config/kibana.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 11 additions & 14 deletions src/server/lib/JSONStream.js → src/server/lib/createJSONStream.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var _ = require('lodash');
var Writable = require('stream').Writable;
var util = require('util');
var through = require('through');

var levels = {
10: 'trace',
Expand All @@ -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';

Expand All @@ -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');
}

function end() {
this.queue(null);
}

module.exports = JSONStream;
module.exports = function () {
return through(write, end);
};
25 changes: 20 additions & 5 deletions src/server/lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 5d46991

Please sign in to comment.