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

bluesky-logger

v1.0.1

Published

Yet another logger for NodeJS

Downloads

98

Readme

BlueSky Logger

BlueSky Logger

Yet another logger for Node JS. The logger is simple and easy to use.

Having implemented this library three times in different projects, here is a new additional logger library.

Content

Install

$ npm install --save bluesky-logger

Configuration

var
  factory = require('bluesky-logger');

factory
  .config({
    'root': 'info',
    'com.example': 'debug',
    'com.example.services': 'config',
    'com.example.view': 'none',            // don't print any message
    'com.example.view.login': 'all',       // print every message
    'com.example.view.home': 'warn'
  })
  .setSeparator('.')
  .setWriter(function (logLevel, message) {
      // save the output
  });

Log Level Hierarchy:

none < trace < debug < config < info < warn < all

Example: debug

  • none don't print the message
  • trace don't print the message
  • debug print the message
  • all other levels: print the message

Overview:

| level (string) | value (constant) | number | method | |----------------|------------------|--------|--------------------| | none | LEVEL_NONE | 1000 | - | | trace | LEVEL_TRACE | 5 | logger.trace(...) | | debug | LEVEL_DEBUG | 10 | logger.debug(...) | | config | LEVEL_CONFIG | 20 | logger.config(...) | | info | LEVEL_INFO | 50 | logger.info(...) | | warn | LEVEL_WARN | 99 | logger.warn(...) | | all | LEVEL_ALL | 0 | - |

The levels as string must be written in lowercase signs

Usage

Example 1: the log level for this logger is LEVEL_DEBUG

var
  logger = require('bluesky-logger').getLogger('com.example');

logger.trace('This is a short message');             // don't print the message
logger.debug('Hello', name, ', how are yout');       // print the message
logger.info('Important message');                    // print the message
logger.warn('Critical message', system.health);      // print the message
logger.config('Current User:', process.env['USER']); // print the message

Example 2: the log level for this logger is LEVEL_CONFIG

var
  logger = require('bluesky-logger').getLogger('com.example.services.DateService');

logger.trace('This is a short message');             // don't print the message
logger.debug('Hello', name, ', how are yout');       // don't print the message
logger.info('Important message');                    // print the message
logger.warn('Critical message', system.health);      // print the message
logger.config('Current User:', process.env['USER']); // print the message

Example 3: the log level for this logger is LEVEL_INFO

var
  logger = require('bluesky-logger').getLogger('com');

logger.trace('This is a short message');             // don't print the message
logger.debug('Hello', name, ', how are yout');       // don't print the message
logger.info('Important message');                    // print the message
logger.warn('Critical message', system.health);      // print the message
logger.config('Current User:', process.env['USER']); // don't print the message

Example 4: is debug enable? Or other levels

var
  logger = require('bluesky-logger').getLogger('com.example');

if (logger.isDebugEnabled()) {
  logger.debug('output a large object: ', JSON.stringify(largeObject));
}

FAQ

Replace the default writer function:

var factory = require('bluesky-logger');

factory.setWriter(function (logLevel, message) {
  var text = new Date().toString() + logLevel + message;
  fs.appendText('logger.txt', text);
});

Reset to the default writer function:

var factory = require('bluesky-logger');

factory.setWriter(null);

Separate the namespace

var factory = require('bluesky-logger');

factory.setSeparator(':');
// now: the namespace must be splited with ':'
// e.g. 'com:example:services:TestService'

LoggerFactory chaining

The logger factory is able to chain the configuration steps.

var factory = require('bluesky-logger');

factory
  .config({
    'root': 'info'
  })
  .setSeparator('-')
  .config({
    'example-service': 'debug',
    'example-util': 'config'
  })
  .setWriter(function (logLevel, message) {
    // ...
  });

var logger = factory.getLogger('example'); // log level is inherited from root "info"

File Appender

The file appender is for writing the log message into a file. Every day a new file is created.

var loggerFactory = require('bluesky-logger');
var fileAppender = require('bluesky-logger/file-appender');

var filer = fileAppender({
  path: 'path/to/the/logger',
  name: 'logger-filename',
  datePattern: 'YYYY-MM-DD',    // see node module "moment"
  timePattern: 'hh:mm:ss'       // see node module "moment"
});

loggerFactory
  .confif(...)
  .setSeparator(...)
  .setWriter(filer.appendMessage);

Logger

Description of the methods from Logger class.

| method | parameters | description |----------------------|------------|---------------------------- | isTraceEnable() | - | true if the LEVEL_TRACE is enabled. | isConfigEnabled() | - | true if the LEVEL_CONFIG is enabled. | isDebugEnabled() | - | true if the LEVEL_DEBUG is enabled. | isInfoEnabled() | - | true if the LEVEL_INFO is enabled. | isWarnEnabled() | - | true if the LEVEL_WARN is enabled. | trace(...) | any | print the trace message if the LEVEL_TRACE is enabled | debug(...) | any | print the trace message if the LEVEL_DEBUG is enabled | config(...) | any | print the trace message if the LEVEL_CONFIG is enabled | info(...) | any | print the trace message if the LEVEL_INFO is enabled | warn(...) | any | print the trace message if the LEVEL_WARN is enabled | getLevel() | - | return the current level of the logger. | getName() | - | return the last part of the namespace.

Change from Version

The API has change from the version 0.2 to 0.3. In order to get a logger instance must be called the function getLogger('name').

Version 0.2.x

var logger = require('bluesky-logger');

Version 0.3.x and above

var factory = require('bluesky-logger');
var logger = factory.getLogger('namespace');
// or call directly
var logger = require('bluesky-logger').getLogger('namespace');

History

  • 1.0.0 Remove lodash from dependencies. Convert into ES6 modules
  • 0.7.1 Update the dependencies node module. Add a typings definition file index.d.ts (BETA).
  • 0.7.0 unit test for cache, pump lodash to 4.11.1, rewrite the cache module
  • 0.6.3 fixed the lodash call "padStart".
  • 0.6.1 update modules and refactory the file-appender
  • 0.5.0 switch the dependency module from "strftime" to "dateformat". Improve the jsDoc.
  • 0.4.1 add a new line sign to the file appender message.
  • 0.4.0 add a file-appender to the libray.
  • 0.3.0 Separate the configuration from the log message
  • 0.2.2 improve the jsdoc
  • 0.2.1 fixed the readme file and change the "lodash" library to "3.10.1"
  • 0.2.0 add the "toLogLevel"
  • 0.1.1 add trace level
  • 0.0.1 first publishing

License

The MIT License (MIT)

Copyright (c) 2017 BlueSkyFish

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.