Skip to content

Commit

Permalink
add support for custom config of lawgs
Browse files Browse the repository at this point in the history
  • Loading branch information
regevbr committed Dec 3, 2017
1 parent f4f8b93 commit db3830c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
7 changes: 6 additions & 1 deletion README.md
Expand Up @@ -34,7 +34,8 @@ const config = {
region: 'eu-central-1',
logGroup: 'prod',
logStream: 'apps',
layout: '<custom layout object>'
layout: '<custom layout object>',
lawgsConfig: '<optional alwgs config object>'
}
},
categories: {
Expand Down Expand Up @@ -66,6 +67,10 @@ If you are using roles, you will need the following roles:
- `accessKeyId` - Optional if credentials are set in `~/.aws/credentials`
- `secretAccessKey` - Optional if credentials are set in `~/.aws/credentials`
- `layout` - Custom layout. See [suggested layout](#suggested-json-layout)
- `lawgsConfig` - Optional vonfig object for lawgs:
- `showDebugLogs` - Show debug logs. Default: false.
- `uploadMaxTimer` - After this ms timeout, flush to server. Default: 5000.
- `uploadBatchSize` - After this amount of logs, flush to server. Default: 500.

## Suggested json layout

Expand Down
8 changes: 6 additions & 2 deletions lib/index.js
Expand Up @@ -2,7 +2,7 @@

const lawgs = require('lawgs');

function awsAppender(accessKeyId, secretAccessKey, region, logGroup, logStream, layout, timezoneOffset) {
function awsAppender(accessKeyId, secretAccessKey, region, logGroup, logStream, layout, timezoneOffset, lawgsConfig) {
lawgs.config({
aws: {
accessKeyId: accessKeyId,
Expand All @@ -11,6 +11,9 @@ function awsAppender(accessKeyId, secretAccessKey, region, logGroup, logStream,
}
});
const logger = lawgs.getOrCreate(logGroup);
if (lawgsConfig) {
logger.config(lawgsConfig);
}
return function (loggingEvent) {
logger.log(logStream, layout(loggingEvent, timezoneOffset));
};
Expand All @@ -29,7 +32,8 @@ function configure(config, layouts) {
config.logGroup,
config.logStream,
layout,
config.timezoneOffset
config.timezoneOffset,
config.lawgsConfig
);
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "log4js-cloudwatch-appender",
"version": "1.0.2",
"version": "1.1.0",
"description": "log4js aws cloudwatch appender",
"main": "lib/index.js",
"scripts": {
Expand Down
27 changes: 26 additions & 1 deletion test/test.spec.js
Expand Up @@ -16,12 +16,17 @@ const CUSTOM_LAYOUT_TYPE = 'CUSTOM_LAYOUT_TYPE';
const CUSTOM_LAYOUT = {
type: CUSTOM_LAYOUT_TYPE
};
const CUSTOM_LAWGS_CONFIG = {
showDebugLogs: true,
uploadMaxTimer: 6000,
uploadBatchSize: 600
};

describe('log4js-cloudwatch-appender', function () {

sandbox.registerBuiltInSourceTransformer('istanbul');

let config, group, stream, data, appender, layout, type, loggingEvent, timezoneOffset;
let config, group, stream, data, appender, layout, type, loggingEvent, timezoneOffset,customConfig;

function basicLayout(_loggingEvent, _timezoneOffset) {
loggingEvent = _loggingEvent;
Expand All @@ -33,6 +38,9 @@ describe('log4js-cloudwatch-appender', function () {
log: function (_stream, _data) {
stream = _stream;
data = _data;
},
config: function(config) {
customConfig = config;
}
};

Expand Down Expand Up @@ -81,6 +89,23 @@ describe('log4js-cloudwatch-appender', function () {
expect(config.aws.secretAccessKey).to.be(SECRET_KEY);
expect(config.aws.region).to.be(REGION);
expect(group).to.be(LOG_GROUP);
expect(customConfig).to.be(undefined);
done();
});

it('should configure lawgs properly with proper config', function (done) {
appender.configure({
accessKeyId: ACCESS_KEY,
secretAccessKey: SECRET_KEY,
region: REGION,
logGroup: LOG_GROUP,
lawgsConfig: CUSTOM_LAWGS_CONFIG
}, layouts);
expect(config.aws.accessKeyId).to.be(ACCESS_KEY);
expect(config.aws.secretAccessKey).to.be(SECRET_KEY);
expect(config.aws.region).to.be(REGION);
expect(group).to.be(LOG_GROUP);
expect(customConfig).to.be(CUSTOM_LAWGS_CONFIG);
done();
});

Expand Down

0 comments on commit db3830c

Please sign in to comment.