1.2.6 • Published 3 years ago

aws-logging v1.2.6

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

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 Node AWS Dynamo Lambda SES

Installation

Install the Package

  npm install aws-logging

Import the module

 let Logger = require('aws-logging');

Configure your logger

 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)

  iamRoleStatements:
    - Effect: "Allow"
      Action:
        - "logs:*"
      Resource: "*" 
    - Effect: "Allow"
      Action:
        - "dynamodb:*"
      Resource: "*"
    - Effect: "Allow"
      Action: "ses:ListIdentities"
      Resource: "*" 
    - Effect: "Allow"
      Action: "ses:SendEmail"
      Resource: "*"
ParameterTypeDescription
tableNameStringRequired. 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.
mailListArrayOptional. A list of recipient emails that will recieve log alerts
mailSubjectStringOptional. The email subject to be displayed for recipients when they recieve a log alert. Defaults to "New AWS Log"
sourceEmailStringOptional. The sender email used to send the logs. Must be a verified email in your AWS account or under a verified Domain
notifyOnSeverityLevelIntegerRequired. The severity level a log must have in order to trigger an email alert. Defaults to 10. (max 10 - min 0)
serviceNameStringRequired. 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
enableNotificationsBooleanOptional. Specify if you want email alerts enabled. Note : If set to true, The following fields will be required : mailList, and sourceEmail .
regionStringRequired . 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
accessKeyIdStringRequired . Your AWS IAM access Key. Not required if you dont have to configure this before using aws-sdk in your service
secretAccessKeyStringRequired . Your AWS IAM secret Key. Not required if you dont have to configure this before using aws-sdk in your service

Usage

ParameterTypeDescription
messageStringRequired. The message you want to log
severityArrayOptional. Severity level (max 10 - min 0). Defaults : {Log : 1 , Warn : 2 , Error : 3}
detailsJSON ObjectOptional. Any additional details you may want to add. Defaults to false

Partial example from this example app

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

 await Logger.log("Logging data");
 //or
 await Logger.log("Logging data", 1);
 //or
 await Logger.log("Logging data", 1 , {attribute1 : "1", attribute1 : "2", }); 

Warn

 await Logger.warn("Warning data");
 //or
 await Logger.warn("Warning data", 2);
 //or
 await Logger.warn("Warning data", 2 , {attribute1 : "1", attribute1 : "2", }); 

Error

 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

 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

 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

 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);  
1.2.6

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago