log4js-cloudwatch-appender
Simple appender for log4js to submit logs to AWS cloudwatch based on the lawgs module.
Installation
This module is installed via npm:
npm install --save log4js-cloudwatch-appender
Usage
Add aws appender to the log4js config file:
const config = appenders: aws: type: "log4js-cloudwatch-appender" accessKeyId: '<accessKeyId>' secretAccessKey: '<secretAccessKey>' region: 'eu-central-1' logGroup: 'prod' logStream: 'apps' layout: '<custom layout object>' lawgsConfig: '<optional alwgs config object>' categories: default: appenders: 'aws' level: 'info'
This will cause logs to be sent to AWS CloudWatch with the specified group and stream.
Configuration
If you are using roles, you will need the following roles:
- logs:DescribeLogGroups
- logs:DescribeLogStreams
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
mandatory
region
- The CloudWatch regionlogGroup
- The log group to send the metrics tologStream
- The log stream of the group to send the metrics to
optional
accessKeyId
- Optional if credentials are set in~/.aws/credentials
secretAccessKey
- Optional if credentials are set in~/.aws/credentials
layout
- Custom layout. See suggested layoutlawgsConfig
- 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
Logs are easier to query whn they are formatted as json. Following is a suggested json layout to set for this appender. The logging style should be:
const uuid = ;const corr = uuid;const logger = logFactory; logger;
Which will output:
The layout:
const util = ;const _ = ; let processName = path;processName = processName; const publicIp = v4;let ip = ''; ; const jsonLayout = "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": { return ip; } { logEvent__data__ = _; if logEvent__data__ let corr = logEvent__data__0; if Array && corrlength === 2 corr = corr0; if typeof corr === 'string' && corrlength === 36 && corrlength === 5 logEvent__data__0 = logEvent__data__01; return ', "corr": "' + corr + '"'; if logEvent__data__length > 1 && corr && typeof corr === 'string' && corrlength === 36 && corrlength === 5 logEvent__data__; return ', "corr": "' + corr + '"'; return ''; } { if logEvent__data__ const method = logEvent__data__0; if logEvent__data__length > 1 && method && typeof method === 'string' && method !== -1 logEvent__data__; return ', "method": "' + method + '"'; return ''; } { if logEvent__data__ let data = logEvent__data__; data = utilformat; data = ; logEvent__data__ = undefined; return data; return ''; } ; { return items;} { return json ;}
Contributing
Please make all pull requests to the master
branch and ensure tests pass
locally.