1.1.0 • Published 4 years ago

ney-log v1.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
4 years ago

ney-log

A logger package (built with backend APIs in mind) designed to handle logs and put together relevant metadata. The package itself does not actually log anywhere, but requires you to add delegate methods that it can call upon when a new log has been received.

Adding to project

  • Install: npm install @neylion/log
  • Initialize (before any calls to log):
    • Importing: const { init } = require("@neylion/log");
    • Initializing: init(logDelegates);
  • Log:
    • Importing: const { log } = require("@neylion/log");
    • Logging: log.error("Message", errorObject, { metadata1: "Useful context", metadata2: "useful context" })

Log Levels

LevelMethodGeneral guidelines when to use
fatallog.fatal(...)Something critical went wrong that is likely to warrent immediate attention.
errorlog.error(...)Unexpected error was encountered that is likely something you should look into fixing.
warnlog.warn(...)Something unexpected/suboptimal happened that is likely something you should look into but does not require immediate attention.
infolog.info(...)Something happened that is likely to be good to know.
debuglog.debug(...)Something happend that is likely not of much interest but might be useful when debugging the application more thoroughly.

Log Delegates

When initializing the logger, you are required to provide an array of methods (delegates) that will do the actual logging. These delegates will be called, when a new log has been processed, with all the data relevant to that specific log.

Here are two small examples on how you can set up your own log delegate method:

Simply logging the whole logData object as json

function consoleLogger(logData){
  console.log(logData);
}

Logging message after customizing it

function consoleLogger(logData){
  // Customize message the way you want it
  let message = `${logData.direction} ${logData.message} | CorrId: ${logData.correlationId}`;
  // Add direction if it exists
  message = logData.direction ? `${logData.direction} ${message}` : message;
  console.log(`${logData.level} ${message}`);
}

If you use typescript you can import the interface "ILogData" to be able to more easily handle the logData parameter. If using plain javascript, see the following section for a list of what the object contains

Log Data

The packages processes a lot of useful information and makes them available to the log delegates through the parameter sent to them. Here are all the properties available on the object:

PropertyTypeExplanation
levelstringLog level (see section about log levels)
directionstring or undefinedundefined when using the regular "log" object but is automatically set if you use the "logInbound" or "logOutbound" objects instead. Useful for being able to tell which logs that relate to the pipe in to the internal code, and which relate to the pipe between internal code and external calls.
messagestringThe message passed to the log method.
logDetailsobjectObject passed to log method containing useful context to the specific log entry null (guarded to make sure it always returns empty object even if it is omitted in log).
errorobject or undefinedError
timestampstringTimestamp of log in format: hh:mm:ss.sss
requestContextobjectIf you use the package @neylion/request-context, useful request context is available on this object. See the following table for more details on this object.

requestContext properties

PropertyTypeExplanation
msSinceStartnumber or undefinedMilliseconds since request start.
startTimestampstring or undefinedRequest start timestamp in format: hh:mm:ss.sss
correlationIdstring or undefinedSee explanation
callingClientstring or undefinedSee explanation
methodstring or undefinedSee explanation
pathstring or undefinedSee explanation
1.1.0

4 years ago

1.0.0

4 years ago