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

sloth-logger

v2.0.0

Published

A happy little logger with custom levels, formats, output options and more

Downloads

12,535

Readme

Logo

Sloth Logger

A happy little logger with custom levels, colors, formats, output options and more

Installation

npm install sloth-logger

Basic Usage

SlothLogger has 5 log types by default and the log level is set to show all logs. The default log types are: error, warn, info, inspect, debug.

By default, logs are output to the console.

const SlothLogger = require("sloth-logger");
const logger = new SlothLogger.Logger();

logger.error("Error Log Line");
logger.debug("Debug Log Line");
logger.info("Line with variable: %s", "stringVar");

Output

[ERROR][3/10/16 12:20:26 PM EST][log.js:47] Error Log Line
[DEBUG][3/10/16 12:20:26 PM EST][log.js:48] Debug Log Line
[INFO][3/10/16 12:59:26 PM EST][log.js:49] Line with variable: stringVar

Setting properties of each log level

Each Log Level contains the following properties. New levels are created with these defaults

'destination': process.stdout,
'dateFormat': 'm/dd/yy h:MM:ss TT Z',
'color': 'gray',
'sendEmail': false,
'inspect': false,
'inspectOptions': {},
'logLevelThreshold': 5,
'format': '${logLevelName}[${logDate}][${relativeFilePath}:${line}] ${logMessage}',
'aggregator': null  

destination: Takes an instance of an fs.WriteStream or a file path string
dateFormat: Takes a date format. Uses dateformat
color: Takes a color string. Uses colors
sendEmail: Takes a Boolean. States whether individual log entries should be emailed
inspect: Takes a Boolean. States whether to use util.inspect when printing log entry
inspectOptions: Takes an object to pass to the util.inspect function
logLevelThreshold: Takes an integer. Log will only be handled if logLevelThreshold is less than or equal to logger's logLevel
aggregator: Takes an Aggregator. If set, sendEmail option is ignored and emails are only sent when aggregotor.send() is called
format: Takes and ES2015 template literal style string.

Available options for format

fullFilePath: full path to file calling logger
relativeFilePath: path to file relative to the process.cwd
line: line of file calling logger
logDate: formatting text string of date/time. Uses dateformat
logLevelName: name of the log level (i.e. error, warn, etc) enclosed in []
logMessage: formatted message

These settings can be customized when a new SlotLogger is initialized or during runtime.

Set at initialization

let logger = new SlothLogger.Logger({
  levels: {
    info: {
      destination: "logs/info.log"
    },
    warn: {
      destination: "logs/warn.log"
    },
    crit: {
      destination: process.stderr,
      dateFormat: "m/dd/yy h:MM:ss TT Z",
      logLevelThreshold: 0
    }
  }
});

Note, that if you'd like to add a custom log level, simply add a new object keyed by the name of your new level. The above would create a new logger.crit() function with the specified adjustments to the default log level options.

Set logLevels on a per-file basis

If you're currently debugging a single file and wish to ignore your debug messages on other files, you can specify a custom log level on a per-file basis.
To do this, you will need to set your logger as a global function

const SlothLogger = require("../index.js");
global.logger = new SlothLogger.Logger({
  logLevel: 1
});

Then, at the top of the file that you are debugging, you will specify the log level to a higher number

logger.setLogLevelForThisFile(4);

You can also change the logLevel at runtime if you are debugging a specific section of synchronous code

const SlothLogger = require("../index.js");
global.logger = new SlothLogger.Logger({
  logLevel: 1
});

logger.debug("I will not print");
logger.logLevel = 5;
logger.debug("I will print now");
logger.logLevel = 1;
logger.debug("I will not print again");

Send an email notification for each log item

By providing your logger with email settings, you can set each log type to send an email every time the logger is called.

SlothLogger uses nodemailer version 4.6.7 for mailing. Options for nodemailer transport can be found in their documentation.

const SlothLogger = require("sloth-logger");
const logger = new SlothLogger.Logger({
  emailSettings: {
    from: "[email protected]",
    to: "[email protected]",
    transportConfig: {
      host: "smtp.mycompany.com",
      port: 25
    }
  }
});
logger.sendEmail("error", true);

Set Log Email Aggregator

You can also set a log aggregator to collect log entries and send them in bulk. sendEmail attributes of log types are ignored if an aggregator is assigned. Log entries will always be emailed and they will only be emailed once send() has been called on the Aggregator.

const SlothLogger = require("sloth-logger");
const logger = new SlothLogger.Logger({
  emailSettings: {
    from: "[email protected]",
    to: "[email protected]",
    subject: "Error logs from server",
    transportConfig: {
      host: "smtp.mycompany.com",
      port: 25
    }
  }
});

const aggregator = new SlothLogger.Aggregator({
  emailSettings: {
    from: "[email protected]",
    to: "[email protected]",
    subject: "Aggregated logs from server",
    transportConfig: {
      host: "smtp.mycompany.com",
      port: 25
    }
  }
});

logger.setLevelProps("error", { aggregator: aggregator });

logger.error("add this line to error log");
aggregator.send(function(err) {
  if (err) {
    console.error(err);
  }
});