-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (68 loc) · 2.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* @fileOverview Bunyan stream for ArangoDB
* @author Kurt Kincaid
* @version 1.1.1
*/
var url = require( 'url' );
//var debug = require( 'debug' )( 'bunyan-arangodb' );
/**
* Main module constructor
*
* Options/Defaults:
*
* opts = {
* 'collection': 'logs', // Optional. Default: logs
* 'agentOptions': { 'ca': caCertArray }, // Optional.
* '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'
* }
*/
function bunyanArangoDB( opts ) {
//debug( `opts: ${JSON.stringify( opts, null, 2 )}` );
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;
var options = {
'url': this.connectString,
'databaseName': this.db
}
if ( opts[ 'agentOptions' ] ) {
options[ 'agentOptions' ] = opts[ 'agentOptions' ];
}
try {
//debug( `options: ${JSON.stringify( options, null, 2 )}` );
arangodb = require( 'arangojs' )( options );
this.aqlQuery = require( 'arangojs' ).aqlQuery;
this.collection = arangodb.collection( opts.collection || 'logs' );
this.arangodb = arangodb;
}
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 ) {
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( `ERROR: ${e}` );
return e;
} );
};
module.exports = bunyanArangoDB;