0.0.5 • Published 8 years ago

bunyan-dynamo v0.0.5

Weekly downloads
3
License
MIT
Repository
github
Last release
8 years ago

Bunyan-dynamo

A Bunyan stream to Amazon's DynamoDB, allowing you to store your application's logs in Amazon's document storage.

Getting Started

Install bunyan-dynamo using npm and save it as a dependency.

npm install bunyan-dynamo --save

Use bunyan-dynamo like any other Bunyan stream.

var bunyan = require('bunyan'),
	BunyanDynamo = require('bunyan-dynamo');

var bunyanDynamoOptions = {
	"aws": {										// Accepts all AWS SDK options.
		"accessKeyId": "MyAccessKeyId",				// AWS access key ID.
        "secretAccessKey": "MySecretAccessKey"		// AWS secret access key.
    },
    "tableName": "myAppLogs",						// Name of the database table.
    "tableHashKey": "id",							// Partial key for database.
    "tableHashType": BunyanDynamo.TYPE_STRING 		// Hash key data type.
}

var bunyanOptions = {
	name: "MyAppLogger",							// Name of the bunyan logger.
	serializers: bunyan.stdSerializers,				// Bunyan serializers.
	level: 'info',									// Bunyan log level.
	type: 'raw',									// Bunyan type.
    stream: new BunyanDynamo(bunyanDynamoOptions) 	// Bunyan-Dynamo stream.
}

var log = bunyan.createLogger(bunyanOptions);

log.info("Logging to DynamoDB.");

Here is what the record created in DynamoDB looks like.

Time

Time is stored as a number in milliseconds since January 1, 1970, 00:00:00 UTC (zulu time). Amazon's SDK may return this value as a string, so use the following to convert time into an object.

var time = "1466190023000";           // This is the time value returned from DynamoDB using amazon's SDK.

console.log(new Date(Number(time)));  // Fri Jun 17 2016 15:00:23 GMT-0400 (EDT)

Options

PropertyTypeDefaultDescription
awsObject{}Options passed directly into the aws-sdk for DynamoDB.
aws.accessKeyIdStringundefinedYour AWS access key ID.
aws.apiVersionString2012-08-10The AWS API version of DynamoDB to use.
aws.maxRetriesNumber15Maximum amount of retries to attempt with a request to AWS.
aws.profileStringdefaultAWS credential profile to use.
aws.regionNumberus-east-1AWS region to send service requests to.
aws.secretAccessKeyStringundefinedYour AWS secret access key.
batchSizeNumber25Number of log messages to send at a time to the DynamoDB service.
debugBooleanfalseWhen enabled, additional log messages will be displayed and configurations used to help debug the module.
enableHostnameBooleantrueWhen enabled, the hostname will be included as an attribute in each item saved to the DynamoDB table.
sendIntervalNumber5000How often, in milliseconds, to send log messages to the DynamoDB service. Default send interval in debug and trace mode is 1000.
tableNameString<APP_NAME>_<HOSTNAME>_<PORT>Name of the database table, which must be unique. If not defined the module will attempt to create a unique name using the application's name, hostname, and port. See environment variables.
tableHashKeyStringidName of the partial key for the database table.
tableHashTypeStringSData type of the hash key. (S stands for String)
tableRangeKeyStringtimeName of the sort key for the database table.
tableRangeTypeStringNData type of the range key. (N stands for Number)
tableReadCapacityNumber5AWS read capacity for the table.
tableWriteCapacityNumber5AWS write capacity for the table.
traceBooleanfalseWhen enabled, debug mode and trace messages will be displayed and additional configurations used to help debug the module.

Environment Variables

The module can take advantage of some optional environment variables.

PropertyTypeDefaultDescription
APP_NAMEStringundefinedName of the application. Used to uniquely generate a table name if one has not already been defined in the options object.
PORTNumberundefinedPort the application is listening on. Used to uniquely generate a table name if one has not already been defined in the options object.

AWS Credentials

Credentials can either be passed in as options or configured any other way allowed by the amazon sdk

Static Properties

MethodTypeDescription
DYNAMO_TYPE_ARRAY_BINARYStringAmazon's DynamoDB data type identifier for an array of binary data.
DYNAMO_TYPE_ARRAY_MAPStringAmazon's DynamoDB data type identifier for an array of map data.
DYNAMO_TYPE_ARRAY_NUMBERStringAmazon's DynamoDB data type identifier for an array of numbers.
DYNAMO_TYPE_ARRAY_STRINGStringAmazon's DynamoDB data type identifier for an array of strings.
DYNAMO_TYPE_BINARYStringAmazon's DynamoDB data type identifier for binary data.
DYNAMO_TYPE_BOOLEANStringAmazon's DynamoDB data type identifier for a boolean variable.
DYNAMO_TYPE_MAPStringAmazon's DynamoDB data type identifier for map data.
DYNAMO_TYPE_NULLStringAmazon's DynamoDB data type identifier for null data.
DYNAMO_TYPE_NUMBERStringAmazon's DynamoDB data type identifier for a number.
DYNAMO_TYPE_STRINGStringAmazon's DynamoDB data type identifier for a string.

Example

var BunyanDynamo = require('bunyan-dynamo');
console.log("The DynamoDB string type identifier is %s.", BunyanDynamo.DYNAMO_TYPE_STRING); // 

Example Output

The DynamoDB string type identifier is S.

Instance Methods

Get Config

Returns the current bunyan-dynamo configuration object.

Example

var BunyanDynamo = require('bunyan-dynamo'),
    bunyanDynamoStream = new BunyanDynamo();

console.log(bunyanDynamoStream.getConfig());

Example Output

{ 
    aws: { 
        apiVersion: '2012-08-10', 
        maxRetries: 15, 
        region: 
        'us-east-1' 
    },
    enableHostname: true,
    batchSize: 25,
    sendInterval: 5000,
    tableName: 'MyAppName_MyHostName_3000',
    tableHashKey: 'id',
    tableHashType: 'S',
    tableRangeKey: 'time',
    tableRangeType: 'N',
    tableReadCapacity: 5,
    tableWriteCapacity: 5
}

Set Config

Set or change the configuration options.

Example

var BunyanDynamo = require('bunyan-dynamo'),
    bunyanDynamoStream = new BunyanDynamo();

bunyanDynamoStream.setConfig({
    batchSize: 50,
    sendInterval: 60000,
    tableName: "MyTableName"
});

console.log(bunyanDynamoStream.getConfig());

Example Output

{ 
    aws: { 
        apiVersion: '2012-08-10', 
        maxRetries: 15, 
        region: 
        'us-east-1' 
    },
    enableHostname: true,
    batchSize: 50,
    sendInterval: 60000,
    tableName: 'MyTableName',
    tableHashKey: 'id',
    tableHashType: 'S',
    tableRangeKey: 'time',
    tableRangeType: 'N',
    tableReadCapacity: 5,
    tableWriteCapacity: 5
}