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

prhone-log

v3.0.1

Published

Simple JavaScript logger

Downloads

11

Readme

PRHONE Log

npm version Build Status prhone

Simple JavaScript logger.

A simple JavaScript logger for client side applications but it can be used in other environments. Supports CommonJS, AMD and standalone.

See example.html and example.js for browser and node.js usage respectively.

Install

Webpack, Browserify and Node.js:

npm install prhone-log

NPM CDN:

<script src="https://unpkg.com/prhone-log"></script>

In browser it is found as window.prhone.Log.

Use

const Log = require('prhone-log');

const log1 = new Log('app1');

log1.info('Initializing app');
log1.debug('Loading global configuration');
log1.debug('Loading user configuration');

...will output:

18:31:55.987 INFO [app1] Initializing app
18:31:55.987 DEBUG [app1] Loading global configuration
18:31:55.988 DEBUG [app1] Loading user configuration

API

Log

logger Log(String namespace[, Object settings])

Creates a new logger instance.

  • String namespace - The logger namespace or logger name.
  • Object settings - Optional configuration to overwrite.
    • Number priority - Levels priority to display. Default to 3. This can be used to display only messages equal or below than certain priority. (ex: priority: 1 will only display warning and error messages.)
    • Boolean display - Display messages on console. Default to true.
    • Boolean displayTime - Display time (hh:mm:ss.SSS). Default to true.
    • Boolean displayLevel - Display level name. Default to true.
    • Boolean displayNamespace - Display namespace. Default to true.
    • Boolean throwErrors - Throw errors on levels priority equal to 0. Default to false.
    • Boolean history - Keep history. Default to true.
    • Boolean colors - Display messages with colors. Default to true.

| Level | Priority | | :---- | :------- | | error | 0 | | warn | 1 | | info | 2 | | debug | 3 |

Object Log.COLORS

List of colors to set up levels. Available colors: RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, BLACK and WHITE.

Object Log.getSettings()

Get a copy of the current global settings.

Log.update(Object settingsToUpdate)

Update the current global settings.

Log.addLevel(Object level)

Adds a new log level globally.

  • Object level - Level configuration.
    • String level.name - Level name.
    • Number [level.priority] - Priority value. Default to 3.
    • Object [level.color] - The log color definition. You can use the Log.COLORS references. Default null.
      • String browser - Browser styles. Ex: color:#2E7D32;
      • String node - Node.js color. Ex: \x1b[32m.

Example:

const Log = require('prhone-log');

Log.addLevel({
  name: 'fatal',
  priority: 0,
  color: Log.COLORS.RED
});

const log2 = new Log('app2');

const errMsg = 'settings file is corrupt';

log2.fatal('The application crashed, details:', errMsg);
// 20:02:50.753 FATAL [app2] The application crashed, details: settings file is corrupt

logger

logger.<<method>>(...parameters [, Object meta])

Call a log level method, be it built in (debug, info, warn, error) or custom with parameters with any type (except functions). If the last parameter is an object or array it will be saved as metadata in history.

const logger = new Log('app1');
logger.info('This is a warning', { details: 'here the details' });
// 18:20:15.985 INFO [app1] This is a warning

Array logger.getHistory()

This is an array with all messages recorded in order chronological, whether logged in console or not. Only if the setting history is enabled.

The format of each message is:

{

  • date: The datetime in number format.
  • level: The string level name.
  • text: The parsed text message.
  • meta: The metadata if provided.

}

Object logger.getSettings()

Get a copy of the current logger settings.

logger.update(Object settingsToUpdate)

Update the current logger settings.

logger.addLevel(Object level)

Works the same way as Log.addLevel() method, but adds the level only to this logger.

Changelog

Read CHANGELOG.md file to see changes.

License

MIT