-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: updated sdk dependency to use v3 of aws-javascript
- Loading branch information
biggaji
committed
Nov 15, 2023
1 parent
8942f87
commit c6286f5
Showing
13 changed files
with
2,405 additions
and
600 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,3 +102,5 @@ dist | |
|
||
# TernJS port file | ||
.tern-port | ||
|
||
tests/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"tabWidth": 2, | ||
"printWidth": 80, | ||
"singleQuote": true, | ||
"semi": true, | ||
"trailingComma": "all", | ||
"proseWrap": "always" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,120 @@ | ||
# send-cloudwatch-log | ||
This sdk allows you to easily send logs from your nodeJs application to AWSCloudWatchLogs service, it is easy and simple to implement. | ||
|
||
This sdk allows you to easily send logs from your Node.js application to | ||
AWSCloudWatchLogs service. It is easy and very simple to use. | ||
|
||
## Requirement | ||
|
||
This sdk requires a node version 8, 10 or higher to work effectively. | ||
|
||
## Installation | ||
## Installation | ||
|
||
```bash | ||
npm install send-cloudwatch-log | ||
or | ||
yarn add send-cloudwatch-log | ||
``` | ||
Using Npm? | ||
|
||
## Usage | ||
```sh | ||
npm install --save send-cloudwatch-log | ||
``` | ||
|
||
Using Yarn? | ||
|
||
```js | ||
const cwlogger = require("send-cloudwatch-log"); | ||
```sh | ||
yarn add send-cloudwatch-log | ||
``` | ||
|
||
//create an instance of the cwlogger , you can create as many instance as you want. | ||
## Usage | ||
|
||
// For each instance, you just need to pass your aws_credentials and provide the logGroupName and logStreamName. See the example below: | ||
```js | ||
const cwlogger = require('send-cloudwatch-log'); | ||
|
||
const logger = new cwlogger({ | ||
accessKeyId: "AWS_ACCESS_KEY_ID", | ||
secretAccessKey: "AWS_SECRET_ACCESS_KEY", | ||
region: "AWS_REGION", | ||
// Create an instance of the cwlogger , you can create as many instance as you want. | ||
// For each instance, you just need to pass the required configs details as seen in the example below: | ||
const logger = new cwlogger( | ||
{ | ||
accessKeyId: 'AWS_ACCESS_KEY_ID', | ||
secretAccessKey: 'AWS_SECRET_ACCESS_KEY', | ||
region: 'AWS_REGION', | ||
}, | ||
{ | ||
logGroupName: "LOG_GROUP_NAME", | ||
logStreamName: "LOG_STREAM_NAME", | ||
logGroupName: 'LOG_GROUP_NAME', | ||
logStreamName: 'LOG_STREAM_NAME', | ||
}, | ||
); | ||
|
||
/** | ||
* Create a log object | ||
* Each log object must contain only the 'message' and 'timestamp' properties. | ||
* { message: string, timestamp: number} | ||
* Since message must be a string, so you can also use the JSON.stringify() method to wrap objects as a string | ||
* timestamp must be in milliseconds, so you can use either Date.now() or new Date().getTime(); | ||
*/ | ||
|
||
let log = { | ||
message: 'Hello World from cwLogger', | ||
timestamp: Date.now(), | ||
}; | ||
|
||
|
||
// Send log to AWSCloudWatchLogs | ||
// The sendLog() method accepts either a single log object or an array of log objects. | ||
|
||
// Sending the a single log object | ||
logger | ||
.sendLog(log) | ||
.then((resp) => { | ||
// resp -> the response from AWSCloudWatchLogs | ||
console.log(`Logs sent successfully`, resp); | ||
}) | ||
.catch((err) => { | ||
console.log(`An error occured`, err); | ||
}); | ||
|
||
//create log a log | ||
//Each log must be an object containing only message and timestamp e.g {message: "STRING", timestamp: number}. | ||
|
||
//timestamp must be in milliseconds | ||
|
||
let log = { | ||
message: "Hello World from cwLogger", | ||
timestamp: Date.now() | ||
let logs = [ | ||
{ | ||
message: 'Hello World from cwLogger', | ||
timestamp: Date.now(), | ||
}; | ||
{ | ||
message: JSON.stringify({ error: 'TypeError', message: 'timestanps must be a number'}), | ||
timestamp: Date.now(), | ||
}; | ||
]; | ||
|
||
//send log to AWSCloudWatchLogs | ||
|
||
logger.sendLog(log) | ||
.then(resp => { | ||
// Sending the an array | list of log object | ||
logger | ||
.sendLog(logs) | ||
.then((resp) => { | ||
// resp -> the response from AWSCloudWatchLogs | ||
console.log(`Logs sent successfully`, resp); | ||
}) | ||
.catch(err => { | ||
.catch((err) => { | ||
console.log(`An error occured`, err); | ||
}); | ||
|
||
|
||
|
||
``` | ||
|
||
The `sendLog()` method takes the log as a parameter and sends it to the AWSCloudWatchLogs. It returns a promise which when successful, returns an object which contains a nextSequenceToken and a customised successful message `Logs sent successfully`, or an error with a message showing the reason it failed. | ||
The `sendLog()` method takes the log object or an array of log objects as an | ||
argument and sends it to the AWSCloudWatchLogs. | ||
|
||
It returns a promise which when successful, returns an object which contains a | ||
nextSequenceToken and $metadata from AWSCloudWatchLogs or an error with a | ||
message showing the reason it failed. | ||
|
||
![Image Screenshot of AwsCloudWatchLogs stream event](https://res.cloudinary.com/dahn8uiyc/image/upload/v1642677223/logger_baqit9.png) | ||
|
||
## How can I thank you? | ||
|
||
Why not star the github repo? I'd love the attention! Why not share the link for this repository on Twitter or HackerNews? Spread the word! | ||
Why not star the Github repository? I'd love the attention! | ||
|
||
Why not share the link for this repository on X or HackerNews? | ||
|
||
Don't forget to [follow me on twitter](https://twitter.com/bigg_aji)! | ||
Spread the word! | ||
|
||
Don't forget to [follow me on X](https://x.com/oxwware)! | ||
|
||
Thanks! | ||
|
||
Tobiloba Ajibade. | ||
|
||
## License | ||
|
||
The MIT License [(MIT)]("https://mit-license.org/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
const { SendCloudWatchLogs } = require('./lib/src/logger'); | ||
|
||
const { SendCloudWatchLogs } = require("./lib/src/logger") | ||
|
||
module.exports = SendCloudWatchLogs; | ||
module.exports = SendCloudWatchLogs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.validateRequiredKeys = void 0; | ||
function validateRequiredKeys(obj) { | ||
for (let key in obj) { | ||
if (obj[key] === undefined) { | ||
throw new Error(`${key} is required to initialize the logger class instance`); | ||
} | ||
} | ||
} | ||
exports.validateRequiredKeys = validateRequiredKeys; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); |
Oops, something went wrong.