bunyan-arangodb
Version:
Bunyan stream for ArangoDB
73 lines (66 loc) • 2.31 kB
JavaScript
/**
* @fileOverview Bunyan stream for ArangoDB
* @author Kurt Kincaid
* @version 0.3.2
*/
var url = require( 'url' );
var debug = require( 'debug' )( 'bunyan-arangodb' );
/**
* Main module constructor
*
* Options/Defaults:
*
* opts = {
* 'collection': 'logs', // Optional. Default: logs
* 'username': '', // Required. No default.
* 'password': '', // Required. No default.
* 'server': 'http://localhost:8529', // Optional. Default: http://localhost:8529
* 'db': '_system', // Optional. Default: _system,
* 'port': 8529 // Optional. Default: 8529. Ignored if port is specified as part of 'server'
* }
*/
var collection;
function bunyanArangoDB( opts ) {
opts = opts || {};
this.server = opts.server || 'http://127.0.0.1';
var u = url.parse( opts.server || 'http://127.0.0.1:8529' );
this.server = u.hostname;
this.port = u.port || opts.port || 8529;
this.db = opts.db || '_system';
this.username = opts.username;
this.password = opts.password;
this.connectString = `${u.protocol}//${this.username}:${this.password}@${this.server}:${this.port}`;
var arangodb;
console.log( this.connectString );
try {
//arangodb = require( 'arangojs' )( this.connectString );
var arangojs = require( 'arangojs' );
arangodb = arr( this.connectString );
arangodb.useDatabase( this.db );
this.aqlQuery = arr.aqlQuery;
this.collection = arangodb.collection( opts.collection || 'logs' );
this.arangodb = arangodb;
console.log( 'Finished setup' );
}
catch( e ) {
return e;
}
}
/**
* Function for parsing and storing raw log data in ArangoDB
*
* @param {Object} entry Raw Bunyan log data
*/
bunyanArangoDB.prototype.write = function( entry ) {
console.log( `Raw log: ${entry}` );
this.arangodb.query( this.aqlQuery `INSERT ${JSON.parse( entry )} IN ${this.collection}`
).then( ( r ) => {
// Not doing anything with the results. Return them, maybe?
debug( `Write successful: ${JSON.stringify( r, null, 2 )}` );
return null;
} ).catch( ( e ) => {
debug( `ERRORzzz: ${e}` );
return e;
} );
};
module.exports = bunyanArangoDB;