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

@cstan/api-logger

v2.0.1

Published

A CommonJs module called to record errors and users logging in and out. This module requires one other "module". It is built into the Node package and does not need to be installed by npm or yarn. Also the logger needs to know where to write the log files

Downloads

3

Readme

api-logger

A CommonJs module called to record errors and users logging in and out. This module requires one other "module". It is built into the Node package and does not need to be installed by npm or yarn. Also the logger needs to know where to write the log files so use the method log.setConfig. You must pass the site's config.js object down through any @cstan/api-module-name modules which cannot access the config file directly. All of these files have a setConfig method which they automatically pass on to any modules they require. The config object must have a top level attribute named 'logPath' as shown in the example config file below.

The name of the file written to by the api-logger is a string of digits for the year and month. Every month a new file name is created and all logs are appended to that file until another new month, ie. '202206.log'.

Error logs are written in the errors folder and user logs are written in the users folder both within the folder specified by the config object.

Installation

npm install @cstan/api-logger

Use

const log = require("@cstan/api-logger");
log.setConfig(myConfig);

/*
 * User information would usually be provided by auth or some other source of user info.
 * Here we are providing as declared variable 'user'.
*/

const user = {
  first_name: "Charlie",
  last_name: "Stanton",
  email: "[email protected]", // not real
}

// assume username is user email
log.user(`User ${username} is attempting to log in.`);

log.user(`User: ${username}, Name: ${user.first_name} ${user.last_name} logged in successfuly.)

try{
  //something
} catch(err) {
  const details = {
    something: "went wrong"
  }
  log.error(err, details); // See note below regarding second argument
}

In the log.error method the second argument is for any details you may want to include, likely for debugging purposes, when you log the error. The function expects an object with attributes and their values.

The config.js file

The following is an example config.js file that we use for configuring our API. Critical information has been changed but the layout reveals a methodology that you can use or adapt to your needs. The 'env' is set by hard coding but you could use environment variables to assign its value for added convenience. Our API uses node, hapi, and mysql and the config.js addresses the setup for them.

config.js:

const env = "dev"; // suggested values: 'dev', 'prod', 'test', 'train'

const config = {
  dev: {
    server: {
      port: 3201,
      host: "localhost",
      routes: {
        cors: {
          origin: ["*"],
          additionalHeaders: ["x-requested-with"],
          credentials: true,
        },
      },
    },
    logPath: "C:/Users/cstan/logs/",
    // on Linux: logPath: "/home/cstan/logs/"
    // NOTE: use the absolute path (full path from root)
    mysql: {
      connectionLimit: 10,
      host: "localhost",
      user: "api_user",
      password: "superSecret",
      database: "api",
    },
    cookie: {
      name: "api_sess",
      password: "$t8s9vlH6SQ39bD8m%FT72cj49K",
      isSecure: false,
    },
    emailService: {
      serverToken: "4866cd4a-b21f-4916-ba3c-1dd3b84b8a67",
      sender: "[email protected]",
    },
    roles: ["guest", "clerk", "manager", "admin"],
  },
  prod: {
    server: {
      port: 3201,
      host: "127.0.0.1",
      routes: {
        cors: {
          origin: ["*"],
          additionalHeaders: ["x-requested-with"],
          credentials: true,
        },
      },
    },
    logPath: "/home/cstan/api/logs/",
    mysql: {
      connectionLimit: 10,
      host: "localhost",
      user: "api_user",
      password: "xtraSecret",
      database: "api",
    },
    cookie: {
      name: "api_sess",
      password: "$t8s9vlH6SQ39bD8m%FT72cj49K",
      isSecure: false,
    },
    emailService: {
      serverToken: "4866cd4a-b21f-4916-ba3c-1dd3b84b8a67",
      sender: "[email protected]",
    },
    roles: ["guest", "clerk", "manager", "admin"],
  },
  test: {
    server: {
      port: 3201,
      host: "127.0.0.1",
      routes: {
        cors: {
          origin: ["*"],
          additionalHeaders: ["x-requested-with"],
          credentials: true,
        },
      },
    },
    logPath: "/home/cstan/api/test-logs/",
    mysql: {
      connectionLimit: 10,
      host: "localhost",
      user: "tst_user",
      password: "nowayJose",
      database: "tst",
    },
    cookie: {
      name: "api_sess",
      password: "$t8s9vlH6SQ39bD8m%FT72cj49K",
      isSecure: false,
    },
    emailService: {
      serverToken: "4866cd4a-b21f-4916-ba3c-1dd3b84b8a67",
      sender: "[email protected]",
    },
    roles: ["guest", "clerk", "manager", "admin"],
  },
};

module.exports = config[env];

Within @cstan/api-logger are relevant statements such as :

const Site = require("../config.js");

//and

const filePath = Site.logPath + `users/${fileDate()}.log`;