# aws-logging

> Logs activity across all services under a single AWS account. Optional features can be enabled to trigger Email notifications based on the logs severity level

Latest version **1.2.6** (published 2022-03-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install aws-logging
pnpm add aws-logging
yarn add aws-logging
bun add aws-logging
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.6 |
| Published | 2022-03-02 |
| First published | 2022-02-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 64.2 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Fadel Sobh fsobh15@icloud.com |
| Maintainers | fsobh |
| Keywords | aws logger, log alerts, AWS, Utility, save logs |

## Links

- npm: https://www.npmjs.com/package/aws-logging
- Repository: https://github.com/fsobh/aws-logging
- Homepage: https://github.com/fsobh/aws-logging#readme
- Issues: https://github.com/fsobh/aws-logging/issues
- npm.io page: https://npm.io/package/aws-logging

## Dependencies (2)

- [aws-sdk](https://npm.io/package/aws-sdk.md) ^2.1079.0
- [short-unique-id](https://npm.io/package/short-unique-id.md) ^4.4.4

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 1.2.6 (latest) — 2022-03-02
- 1.2.5 — 2022-03-02
- 1.2.4 — 2022-02-28
- 1.2.3 — 2022-02-28
- 1.2.2 — 2022-02-28
- 1.2.1 — 2022-02-28
- 1.2.0 — 2022-02-28
- 1.1.9 — 2022-02-28
- 1.1.8 — 2022-02-28
- 1.1.7 — 2022-02-28
- 1.1.6 — 2022-02-28
- 1.1.5 — 2022-02-28
- 1.1.4 — 2022-02-28
- 1.1.3 — 2022-02-28
- 1.1.2 — 2022-02-28
- … 12 more at https://npm.io/package/aws-logging/versions

## README

<p align="center">
    <a href="https://aws.amazon.com" title="AWS">
        <img  height=230px src="https://pbs.twimg.com/profile_images/1473756532827246593/KRgw2UkV_400x400.jpg" alt="AWS">
    </a>
</p>

<div align="center">

# aws-logging
### Logs activity across all services under a single AWS account. Optional features can be enabled to trigger Email notifications based on the logs severity level.


[![JavaScript](https://img.shields.io/badge/JavaScript-%23FFFF00)](https://img.shields.io/badge/JavaScript-%23FFFF00)    [![Node](https://img.shields.io/badge/NodeJS-v14.0.1-brightgreen)](https://img.shields.io/badge/NodeJS-v14.x.x-brightgreen) 
 [![AWS](https://img.shields.io/badge/AWS-Cloud%20provider-orange)](https://img.shields.io/badge/AWS-Cloud%20provider-orange)
[![Dynamo](https://img.shields.io/badge/DynamoDB-Database-blue)](https://img.shields.io/badge/DynamoDB-Database-blue) [![Lambda](https://img.shields.io/badge/Lambda-service-%20%09%23a26c2f)](https://img.shields.io/badge/Lambda-service-%20%09%23a26c2f)  [![SES](https://img.shields.io/badge/SES-aws--sdk-yellowgreen)](https://img.shields.io/badge/SES-aws--sdk-yellowgreen)
</div>


## Installation
####  Install the Package

```bash
  npm install aws-logging
```

## Import the module

```javascript
 let Logger = require('aws-logging');
```

## Configure your logger

```javascript
 Logger.config.update(
    {
        tableName : "SERVICE-LOGS",  
        mailList : ["example@icloud.com"],
        stage : "Dev",
        mailSubject : "New AWS Log",
        sourceEmail : "example@yourVerifiedEmailOrDomain.com",
        notifyOnSeverityLevel : 10,
        serviceName : "sample-service",
        enableNotifications : false,
        region : "us-east-2",
        accessKeyId : "xxxx",
        secretAccessKey : "xxxx"

    });
```
# IMPORTANT :

### Make sure the IAM role has the following permissions enabled on AWS AND in your app.
(The role you generated your  Access Key  and Secret from)

```yaml
  iamRoleStatements:
    - Effect: "Allow"
      Action:
        - "logs:*"
      Resource: "*" 
    - Effect: "Allow"
      Action:
        - "dynamodb:*"
      Resource: "*"
    - Effect: "Allow"
      Action: "ses:ListIdentities"
      Resource: "*" 
    - Effect: "Allow"
      Action: "ses:SendEmail"
      Resource: "*"
```

| Parameter | Type     | Description                       |
| :-------- | :------- | :-------------------------------- |
| `tableName`      | `String`| **Required**. The name of the table that will be automatically created to store your logs. Defaults to `"SERVICE-LOGS"`. Must be unique from other table names. |
| `mailList`       | `Array` | **Optional**. A list of recipient emails that will recieve log alerts |
| `mailSubject`     | `String` | **Optional**. The email subject to be displayed for recipients when they recieve a log alert. Defaults to `"New AWS Log"`  |
| `sourceEmail`     | `String` | **Optional**. The sender email used to send the logs.  **Must be a verified email in your AWS account or under a verified Domain** |
| `notifyOnSeverityLevel`     | `Integer` | **Required**. The severity level a log must have in order to trigger an email alert. Defaults to `10`. (max 10 - min 0) |
| `serviceName`     | `String` | **Required**. The name of the service you added this package to. This will be used to identify which service the log belongs to in the `SERVICE-LOGS` table |
| `enableNotifications`     | `Boolean` | **Optional**. Specify if you want email alerts enabled. **Note : If set to `true`, The following fields will be required : `mailList`, and `sourceEmail`** . |
| `region`     | `String` | **Required** . The AWS region you want this Logger configured for. **Note : Must be the same as the region that the `sourceEmail` is configured for in your AWS account**  |
| `accessKeyId`     | `String` | **Required** . Your AWS IAM access Key. Not required if you dont have to configure this before using `aws-sdk` in your service  |
| `secretAccessKey`     | `String` | **Required** . Your AWS IAM secret Key. Not required if you dont have to configure this before using `aws-sdk` in your service  |


## Usage
| Parameter | Type     | Description                       |
| :-------- | :------- | :-------------------------------- |
| `message`      | `String`| **Required**. The message you want to log |
| `severity`       | `Array` | **Optional**. Severity level (max 10 - min 0). **Defaults : `{Log : 1 , Warn : 2 , Error : 3}`** |
| `details`     | `JSON Object` | **Optional**. Any additional details you may want to add. **Defaults to false**  |

### Partial example from this [example app](https://github.com/fsobh/Chainlink-External-Initiator-Template/blob/Dev/app.js)

```javascript
var express = require('express');
const serverless = require('serverless-http');
var app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
const Logger = require("aws-logging");

Logger.config.update({
  
    mailList : [process.env.RECIEVER_EMAIL_1],
    sourceEmail : process.env.SOURCE_EMAIL,
    notifyOnSeverityLevel : 5,
    serviceName : process.env.SERVICE_NAME,
    enableNotifications : true,
    region:"us-east-2",
    accessKeyId : process.env.KEY,
    secretAccessKey : process.env.SECRET

})
app.get('/', async function (req, res) {

    try {

        await Logger.log("Health Check ran", 1, {request : {body : JSON.parse(JSON.stringify(req.body))}});
        res.sendStatus(200);
        
    } catch (error) {
        
        await Logger.error(error.message,3,
            {
                stack : error.stack,
                error : String(error)
            });
        res.send({'error' : error}).sendStatus(500);
    }

})

module.exports.handler = serverless(app);
```



####  Log
```javascript
 await Logger.log("Logging data");
 //or
 await Logger.log("Logging data", 1);
 //or
 await Logger.log("Logging data", 1 , {attribute1 : "1", attribute1 : "2", }); 
```

####  Warn
```javascript
 await Logger.warn("Warning data");
 //or
 await Logger.warn("Warning data", 2);
 //or
 await Logger.warn("Warning data", 2 , {attribute1 : "1", attribute1 : "2", }); 
```

####  Error
```javascript
 await Logger.error("Error data");
 //or
 await Logger.error("Error data", 3);
 //or
 await Logger.error("Error data", 3 , {attribute1 : "1", attribute1 : "2", });  
```

### Short Methods
These shorter methods just swap the argument order for details and severity.

####  Log
```javascript
 await Logger.l("Log data");
 //or
 await Logger.l("Log data with object", {attribute1 : "1", attribute1 : "2", });
 //or
 await Logger.l("Log data with object and sev", {attribute1 : "1", attribute1 : "2", }, 1);  
```
####  Warn
```javascript
 await Logger.w("Warn data");
 //or
 await Logger.w("Warn data with object", {attribute1 : "1", attribute1 : "2", });
 //or
 await Logger.w("Warn data with object and sev", {attribute1 : "1", attribute1 : "2", }, 2);  
```
####  Error
```javascript
 await Logger.e("Error data");
 //or
 await Logger.e("Error data with object", {attribute1 : "1", attribute1 : "2", });
 //or
 await Logger.e("Error data with object and sev", {attribute1 : "1", attribute1 : "2", }, 3);  
```

---
_Source: https://npm.io/package/aws-logging · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
