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

@sift/logger

v2.0.0

Published

Sift Logger

Downloads

94

Readme

Logger

Log Levels

  • CRIT - A critical error has occured, and the process has crashed.
  • ERROR - An unexpected error has occured, and the process may have crashed.
  • WARNING - An issue has occurred that warrants notification, but the process was allowed to continue.
  • INFO - An informational message that should be displayed in production.
  • DEBUG - A message that should only be displayed during development.

Logs at INFO or greater severity will be logged by default.

Retrieving a Logger

get-logger will retrieve an existing logger if a logger has already been created for the given module name, otherwise, a new logger with default options will be created automatically and returned.

Example

import { getLogger } from '@sift/logger';


const logger = getLogger('modulea');

logger.info('Hello World!');

Building a Logger

build-logger will create a new logger if no logger exists for the given module name, otherwise the existing cached logger will be overridden. Currently, loggers that have already been retrieved from the cache will not be updated when build-logger is called for an existing module.

build-logger Configuration

  • moduleName (required) - The moduleName that will be used as the logger cache key. This value will also be included with all logs.
  • parent (optional) - A parent logger that the new logger will inherit options from.
  • options (optional) - Optional logger configuration. Any options specified will override options from the parent.
  • options.level - The maximum level to log at (see Log Levels).

Example

import {
  buildLogger,
  Level,
} from '@sift/logger';


const parentLogger = buildLogger({
  moduleName: 'parent',
  options: {
    level: Level.DEBUG,
  }
});

const logger = buildLogger({
  parent: parentLogger,
  moduleName: 'logger',
});

Building a Request Logger

build-request-logger will create a new logger middleware designed to be used with Express. This logger is primarily for logging HTTP traffic passing through the Express app.

build-request-logger Configuration

  • logger (required) - The logger to use when logging within the middleware
  • appName (required) - The name of the application, usually the name within package.json
  • logRequestImmediately (optional) - Whether or not a request should be logged immediately before the response is returned. This option makes sure requests are logged, even if the server crashes; however, the response will not be included in the log.

Structuring Loggers

It is recommended that a root logger is created for the entire application, and loggers are created for each major module in the application. The root logger will serve as the parent for each major module. Each file in the module will use the module logger.

Example

Root Logger

project/logger.ts

import { buildLogger } from '@sift/logger';


export default buildLogger({
  moduleName: 'project',
});

Module Logger

project/modulea/logger.ts

import { buildLogger } from '@sift/logger';
import rootLogger from '../logger';


export default buildLogger({
  parent: rootLogger,
  moduleName: 'project.modulea',
});

Module File

project/modulea/file.ts

import logger from './logger';


logger.info('hello world!');

Logging

Log

import {
  getLogger,
  Level,
} from '@sift/logger';


const logger = getLogger('module');
logger.log(Level.INFO, 'Hello world!');

{"timestamp":"2019-04-05T15:17:39.951Z","level":"INFO","module":"module","message":"Hello world!"}

Crit

import { getLogger } from '@sift/logger';


const logger = getLogger('module');
logger.crit('Hello world!');

{"timestamp":"2019-04-05T15:14:29.384Z","level":"CRIT","module":"module","message":"Hello world!"}

Error

import { getLogger } from '@sift/logger';


const logger = getLogger('module');
logger.error('Hello world!');

{"timestamp":"2019-04-05T15:14:11.545Z","level":"ERROR","module":"module","message":"Hello world!"}

Warning

import { getLogger } from '@sift/logger';


const logger = getLogger('module');
logger.warning('Hello world!');

{"timestamp":"2019-04-05T15:13:22.852Z","level":"WARNING","module":"module","message":"Hello world!"}

Info

import { getLogger } from '@sift/logger';


const logger = getLogger('module');
logger.info('Hello world!');

{"timestamp":"2019-04-05T15:13:03.391Z","level":"INFO","module":"module","message":"Hello world!"}

Debug

import { getLogger } from '@sift/logger';


const logger = getLogger('module');
logger.debug('Hello world!');

{"timestamp":"2019-04-05T15:12:36.264Z","level":"DEBUG","module":"module","message":"Hello world!"}

Exception Logging

When an Exception is logged, The message will be extracted from the Exception, and the Exception's stacktrace will be included as stack.

import { getLogger } from '@sift/logger';


const logger = getLogger('module');
logger.warning(new Error('Hello Error!'));

{"timestamp":"2019-04-05T15:07:48.291Z","level":"WARNING","module":"module","message":"Hello Error!","stack":"Error: Hello Error!\n at Object.<anonymous> (/home/tnewman/repos/Lib.Core/test.js:5:16)\n at Module._compile (module.js:653:30)\n at Object.Module._extensions..js (module.js:664:10)\n at Module.load (module.js:566:32)\n at tryModuleLoad (module.js:506:12)\n at Function.Module._load (module.js:498:3)\n at Function.Module.runMain (module.js:694:10)\n at startup (bootstrap_node.js:204:16)\n at bootstrap_node.js:625:3"}

Message Type

Includes the messageType field in the log to make it easy to identify standardized messages, such as requests.

import { getLogger } from '@sift/logger';


const logger = getLogger('module');
logger.info('Hello World!', { messageType: 'string' });

{"timestamp":"2019-04-05T15:06:34.642Z","level":"INFO","module":"module","messageType":"string","message":"Hello World!"}

LOG_LEVEL Environment Variable

Setting the LOG_LEVEL environment variable will override the log level for every logger.

LOG_LEVEL=debug node test.js