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

@felixpy/logger

v0.2.0

Published

A simple javascript logger for web developers.

Downloads

8

Readme

logger

A simple javascript logger for web developers.

CircleCI Codecov Version License Dependencies

Installation

Using NPM

npm i @felixpy/logger --save

Using CDN

<script src="https://unpkg.com/@felixpy/logger"></script>

Quick Start

const logger = new Logger({
  name: 'LoggerName',
  config: {
    separator: '>>>',
    dateFormatter: function (date) {
      return date.toISOString()
    }
  }
})

logger.log('MethodName', 'Hello Logger!')
// [2018-02-25T14:36:49.287Z] [LOG] [LoggerName->MethodName] >>> Hello Logger!

Constructor

Using new Logger(options) to create an instance.

options.name

Type: String

Optional logger name, by default it will be Logger#{id}.

options.config

Type: Object

Custom configuration of logger, these are available keys, none is required:

{
  // The minimum log level to show,
  // Available values: ALL, DEBUG, LOG, INFO, WARN, ERROR, OFF.
  level: 'LOG',

  // Prefix pattern:
  //   %t: date
  //   %p: priority
  //   %c: logger name
  //   %m: method name
  prefix: '[%t] [%p] [%c->%m]',

  // The separator symbol between prefix and messages.
  separator: '-',

  // Callback function to format date.
  dateFormatter (d) {
    return d.toLocaleString()
  },

  // Appenders, it can be built-in appender name or your own appender definition.
  appenders: ['console']
}

options.config.appenders

Type: String | Function | Object | Array<String | Function | Object>

Normally, you shold specify an object appender option like this:

{
  // built-in appender name
  type: 'console'
}
// or
{
  /**
   * custom appender callback
   * @param {string} level - log level
   * @param {string} args - message arguments
   */
  handler: function (level, args) {}
}

Also you can simply specify a string as built-in appender name or a function as appender callback.

When you want use multiple appenders, you just need put them in an array.

Instance Methods

Each instance of logger will have these methods:

logger.debug(methodName, [...args])

Log with DEBUG priority. See logger.log.

logger.log(methodName, [...args])

Log with LOG priority, example:

logger.log('Save', 'Parameters: ', '{"name":"foobar"}')
// [2018/2/26 下午11:09:54] [LOG] [ExampleLogger->Save] - Parameters:  {"name":"foobar"}

Note: When only passing one argument, the method name will be ignored. Example:

logger.log('Some magic messages')
// [2018/2/26 下午11:08:44] [LOG] [ExampleLogger->?] - Some magic messages

logger.info(methodName, [...args])

Log with INFO priority. See logger.log.

logger.warn(methodName, [...args])

Log with WARN priority. See logger.log.

logger.error(methodName, [...args])

Log with ERROR priority. See logger.log.

logger.setLevel(level)

Set minimum level to show logs, example:

logger.setLevel('INFO')

logger.setPrefix(prefix)

Set prefix pattern of logger, example:

logger.setPrefix('[Date->%t] [Priority->%p] [%c->%m]')

logger.setSeparator(separator)

Set separator of logger, example:

logger.setSeparator('>>>')

logger.setDateFormatter(dateFormatter)

Set date formatter of logger, example:

logger.setDateFormatter(function(date) {
  return date.toGMTString()
})

logger.setAppender(appenders)

Set appenders of logger, example:

const myAppender = {
  handler: function (level, args) { /* ... */ }
}

logger.setAppender(['console', myAppender])

Global API

Logger.get(options)

Return an single instance by specified options.

License

MIT

Copyright (c) 2018, Felix Yang