- Notifications
You must be signed in to change notification settings - Fork2
log4js simple appender for writing logs to AWS CloudWatch
License
regevbr/log4js-cloudwatch-appender
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Simple appender for log4js to submit logs to AWS cloudwatch based on thelawgs module.
This module is installed via npm:
npm install --save log4js-cloudwatch-appender
Add aws appender to the log4js config file:
constconfig={appenders:{aws:{type:"log4js-cloudwatch-appender",accessKeyId:'<accessKeyId>',secretAccessKey:'<secretAccessKey>',region:'eu-central-1',logGroup:'prod',logStream:'apps',layout:'<custom layout object>',lawgsConfig:'<optional lawgs config object>'}},categories:{default:{appenders:['aws'],level:'info'}}}
This will cause logs to be sent to AWS CloudWatch with the specified group and stream.
If you are using roles, you will need the following roles:
- logs:DescribeLogGroups
- logs:DescribeLogStreams
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
region
- The CloudWatch regionlogGroup
- The log group to send the metrics tologStream
- The log stream of the group to send the metrics to
accessKeyId
- Optional if credentials are set in~/.aws/credentials
secretAccessKey
- Optional if credentials are set in~/.aws/credentials
layout
- Custom layout. Seesuggested layoutlawgsConfig
- Optional config 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.
Logs are easier to query when they are formatted as json.Following is a suggested json layout to set for this appender.The logging style should be:
constuuid=require('node-uuid');constcorr=uuid.v4();constlogger=logFactory.getLogger('category');logger.info(corr,'methodName()','part1','part2');
Which will output:
{"timestamp":"2017-06-10T11:55:38.251Z","corr":"2e2c99aa-7eee-4fd2-ae36-cd9dc9533816","app":"<appName>","host":"<ip>","pid":24532,"level":"INFO","category":"category","method":"methodName()","message":"part1 part2"}
The layout:
constutil=require('util');const_=require('underscore');letprocessName=path.basename(process.argv[1]);processName=processName.substring(0,processName.length-3);constpublicIp=require('public-ip').v4;letip='';publicIp().then(function(_ip){ip=_ip;}).catch(function(e){console.log(e);ip='unknown';});constjsonLayout={"type":"pattern","pattern":'{"timestamp": "%d{yyyy-MM-ddThh:mm:ss.SSSZ}", "app": "'+processName+'", "ip": "%x{my_ip}", "host": "%h", "pid": %z, "level": "%p", "category": "%c"%x{corr}%x{method}, "message": "%x{message}"}',"tokens":{"my_ip":function(){returnip;},"corr":function(logEvent){logEvent.__data__=_.map(logEvent.data,_.clone);if(logEvent.__data__){letcorr=logEvent.__data__[0];if(Array.isArray(corr)&&corr.length===2){corr=corr[0];if(typeofcorr==='string'&&corr.length===36&&corr.split("-").length===5){logEvent.__data__[0]=logEvent.__data__[0][1];return', "corr": "'+corr+'"';}}if(logEvent.__data__.length>1&&corr&&typeofcorr==='string'&&corr.length===36&&corr.split("-").length===5){logEvent.__data__.shift();return', "corr": "'+corr+'"';}}return'';},"method":function(logEvent){if(logEvent.__data__){constmethod=logEvent.__data__[0];if(logEvent.__data__.length>1&&method&&typeofmethod==='string'&&method.indexOf("()",method.length-2)!==-1){logEvent.__data__.shift();return', "method": "'+method+'"';}}return'';},"message":function(logEvent){if(logEvent.__data__){letdata=logEvent.__data__;data=util.format.apply(util,wrapErrorsWithInspect(data));data=escapedStringify(data);logEvent.__data__=undefined;returndata;}return'';}}};functionwrapErrorsWithInspect(items){returnitems.map(function(item){if((iteminstanceofError)&&item.stack){return{inspect:function(){returnutil.format(item)+'\n'+item.stack;}};}else{returnitem;}});}functionescapedStringify(json){returnjson.replace(/[\\]/g,'\\\\').replace(/[\"]/g,'\\\"').replace(/[\/]/g,'\\/').replace(/[\b]/g,'\\b').replace(/[\f]/g,'\\f').replace(/[\n]/g,'\\n').replace(/[\r]/g,'\\r').replace(/[\t]/g,'\\t');}
Please make all pull requests to themaster
branch and ensure tests passlocally.
About
log4js simple appender for writing logs to AWS CloudWatch
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.