npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

col_pkg_log_util

v1.1.1

Published

Logger for AWS cloudwatch.

Downloads

146

Readme

to test package:

  1. run "npm link"
  2. go to external project to test from
  3. run "npm link col_pkg_log_util"
  4. external project must have a .env file with:
AWS_REGION=
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
LOGGROUPNAME=
CRITICALLOGSTREAMNAME=
ERRORLOGSTREAMNAME=
WARNINGLOGSTREAMNAME=
INFOLOGSTREAMNAME=
DEBUGLOGSTREAMNAME=
DEBUGMODE=

** debug mode is false by default

  1. import function logSend from module col_pkg_log_util
const { logSend } = require('col_pkg_log_util');
  1. log event:
await logSend({log level}, {logDetails});

log levels:

  • critical
  • error
  • warning
  • info
  • debug

logDetails:

  • json object with details to log

to test as middleware:

npm install col_pkg_log_util

add file loggingMiddleware.js


const { logSend } = require('col_pkg_log_util');


const loggingMiddleware = (req, res, next) => {
  const start = Date.now();

  const originalSend = res.send;
  res.send = function (body) {
    res.responseBody = body;
    originalSend.call(this, body);
  };

  res.on('finish', () => {
    const duration = Date.now() - start;

//     const logData = {
//         method: req.method, // HTTP method (GET, POST, etc.)
//         url: req.originalUrl, // Full URL requested
//         headers: req.headers, // Headers sent by the client
//         params: req.params, // Route parameters
//         query: req.query, // Query string parameters
//         body: req.body, // Body of the request (e.g., POST/PUT data)
//         cookies: req.cookies, // Cookies sent by the client (if cookie-parser is used)
//         ip: req.ip, // IP address of the client
//         protocol: req.protocol, // Protocol used (http/https)
//         hostname: req.hostname, // Hostname of the request
//         userAgent: req.get('User-Agent'), // User-Agent header for the client
//         responseBody: res.body, // Body of the response
//         statusCode: res.statusCode, // Status code of the response
//         statusMessage: res.statusMessage, // Status message (e.g., "OK")
//         responseTime: res.getHeaders()['x-response-time'], // Response time (if added by middleware)
//   };
  
  
    const logData = {
      method: req.method,
      url: req.originalUrl,
      headers: {
        userAgent: req.headers['user-agent'],
        referer: req.headers.referer
      },
      params: req.params,
      body: req.body,
      statusCode: res.statusCode,
      responseBody: (() => {
        try {
          return JSON.parse(res.responseBody);
        } catch (e) {
          return res.responseBody;
        }
      })(),
      duration: `${duration}ms`,
      authInfo: req.user || {}, // Auth info if available
    };


    // Conditionals for appropriate log stream
    if (res.statusCode >= 500) {
        logSend('error', logData);
    } else if (res.statusCode >= 400 && res.statusCode < 500) {
        logSend('warning', logData);
    } else {
        logSend('info', logData);
    }
    
    // To add:
        // logSend('debug', logData);
        // logSend('critical', logData);


  });

  next();
};

module.exports = loggingMiddleware;

in server.js add lines

const loggingMiddleware = require('./middleware/loggingMiddleware');

and

app.use(loggingMiddleware);

TODO

  • creating event log settings in env file (retention time...etc)
  • set pre-set log details for different levels (if needed)