setup loggingedit
Every application needs to have some solution for logging, and there isn’t a standard in JavaScript, so instead of forcing you to rely on a specific logging module we created a bare bones logging solution and Using the default loggers will show you how to configure it. That said, our implementation of logging is very minimal and it is highly recommended that you use something like Bunyan once you move to production.
Using A Libraryedit
When the client receives a function for the log:
config value, it expects that the function is a constructor for a custom log class. This is the simplest way to integrate other logging libraries into the elasticsearch client at this time. The contract for this Log class is pretty straight-forward. See our implementation for additional details.
new Constructor(config)
edit
|
The object that was passed to the client constructor, use to determine the log level. |
error(error)
edit
|
|
warning(message)
edit
|
|
info(message)
edit
|
|
debug(message)
edit
|
|
trace(httpMethod, requestUrl, requestBody, responseBody, responseStatus)
edit
Called after every HTTP request.
|
|
|
|
|
|
|
|
Bunyan Exampleedit
In the future we may add loggers for some of the more common libraries, but for now this is an exercise for the user. Here is a hint to get you started implementing a Bunyan log class. Be sure to check out the Bunyan repo for more info about setting things up.
in log_to_bunyan.js.
module.exports = LogToBunyan; var bunyan = require('bunyan'); function LogToBunyan(config) { // config is the object passed to the client constructor. var bun = bunyan.createLogger({name: 'mylogger'}); this.error = bun.error.bind(bun); this.warning = bun.warn.bind(bun); this.info = bun.info.bind(bun); this.debug = bun.debug.bind(bun); this.trace = function (method, requestUrl, body, responseBody, responseStatus) { bun.trace({ method: method, requestUrl: requestUrl, body: body, responseBody: responseBody, responseStatus: responseStatus }); }; this.close = function () { /* bunyan's loggers do not need to be closed */ }; }
in model.js.
var elasticsearch = require('elasticsearch'); var LogClass = require('./log_to_bunyan'); // now just pass the log class to the client constructor using the "log" config option. var client = new elasticsearch.Client({ log: LogClass });
Using the default loggersedit
By default, the client creates a "warning"
level, Console or Stdio logger. To change this, specify the client’s log:
config value to either an array of logger config’s, a single logger config, a log level, an array of log levels, or a constructor for your own logger. That’s a lot of options, so here is an example of each.
Change the logging level to trace, so we get every log message.
var client = new elasticsearch.Client({ log: 'trace' });
Change the logging level, only listen for error and trace messages.
var client = new elasticsearch.Client({ log: ['error', 'trace'] });
Log every message to a file.
var client = new elasticsearch.Client({ log: { type: 'file', level: 'trace', path: '/var/log/elasticsearch.log' } });
Log everything to a file and errors to a socket.
var client = new elasticsearch.Client({ log: [ { type: 'stream', level: 'error', // config option specific to stream type loggers stream: mySocket }, { type: 'file', level: 'trace', // config options specific to file type loggers path: '/var/log/elasticsearch.log' } ] });
Logger Typesedit
|
The default logger for in Node, writes log messages for "info", "debug", and "trace" to stdout and "error" and "warning" to stderr. Options:
|
|
Append the log messages to a file. Options:
|
|
Logs in a format that can be executed with bash, where everything is commented except the trace commands which are formatted as curl calls. By default all of the urls are rewritten to protect production systems and to making the scripts easier to reuse/send to other people. In order to control the urls written specify the curlHost and curlPort configs. Options:
|
|
Send log messages to a <a href="http://nodejs.org/api/stream.html#stream_class_stream_writable">WriteableStream</a> Options:
|
|
Default logger for the browser build, logs to the console when one exists. |