Skip to content

Commit

Permalink
Refactor + example
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylvain Perron committed Feb 3, 2015
1 parent 1227ae5 commit bdd4b67
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
31 changes: 31 additions & 0 deletions example.js
@@ -0,0 +1,31 @@
var lawgs = require('./index');

// Lawgs configuration is mandatory
lawgs.config({
aws: {
accessKeyId: '********', /* Optional is credentials are set in ~/.aws/credentials */
secretAccessKey: '******', /* Optional */
region: 'us-east-1' /* Required */
}
});

var logger = lawgs.getOrCreate('SuperbowlLogs'); /* LogGroup */
var logger2 = lawgs.getOrCreate('SuperbowlLogs'); /* Returns the same instance (caching factory) */

// Logger configuration is optional
logger.config({
// Shows the debugging messages
showDebugLogs: true, /* Default to false */
// Change the frequency of log upload, regardless of the batch size
uploadMaxTimer: 1000, /* Defaults to 5000ms */
// Max batch size. An upload will be triggered if this limit is reached within the max upload time
uploadBatchSize: 10 /* Defaults to 500 */
});

/* 'error' is the log stream name */
logger.log('error', 'Argggg'); // Takes a string

logger.log('touchdown', { // or any serializable object
team: 'Patriots',
weight: 7
});
29 changes: 21 additions & 8 deletions index.js
Expand Up @@ -25,7 +25,6 @@ function CloudWatchLogger(logGroupName) {
this.showDebugLogs = false;
this.uploadMaxTimer = 5000;
this.uploadBatchSize = 500;
this.appendType = true;

// Private members
var logGroupName = logGroupName;
Expand Down Expand Up @@ -107,8 +106,13 @@ function CloudWatchLogger(logGroupName) {
};

// Public API
this.configure = function(config) {
extend(this, config);
this.config = function(conf) {
extend(this, conf);

if(conf.settings) {
settings = conf.settings;
}

AWS.config.update(settings.aws);
cw = new AWS.CloudWatchLogs({ apiVersion: '2015-01-28' });
initializeStream();
Expand Down Expand Up @@ -234,12 +238,21 @@ function CloudWatchLogger(logGroupName) {
|| DEFAULT_TIMEOUT, 'Could not communicate with AWS in a timely fashion');
}

this.configure({ });
this.config({ });
};

module.exports = function(logGroupName) {
if(!loggers.hasOwnProperty(logGroupName)) {
loggers[logGroupName] = new CloudWatchLogger(logGroupName);
module.exports = {

getOrCreate: function(logGroupName) {
if(!loggers.hasOwnProperty(logGroupName)) {
loggers[logGroupName] = new CloudWatchLogger(logGroupName);
}

return loggers[logGroupName];
},

config: function(s) {
extend(true, settings, s);
}
return loggers[logGroupName];

};

0 comments on commit bdd4b67

Please sign in to comment.