@cstan/api-logger
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

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: "cstan@gmail.com", // 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: "info@mywebsite.org",
    },
    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: "info@mywebsite.org",
    },
    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: "info@mywebsite.org",
    },
    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`;

Readme

Keywords

none

Package Sidebar

Install

npm i @cstan/api-logger

Weekly Downloads

2

Version

2.0.1

License

ISC

Unpacked Size

8.95 kB

Total Files

4

Last publish

Collaborators

  • cstanton48