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

@kmhgmbh/logger

v1.1.6

Published

Standardized JSON logger

Downloads

825

Readme

@kmhgmbh/logger

This package provides a standardized logger for all projects. It features error serialization, runtime log level evaluation and JSON output.

Installation

npm install @kmhgmbh/logger

Log Levels

Supported log levels are, from highest to lowest priority:

  • fatal - Fatal errors. The process will exit after logging the message.
  • error - Errors. The process will continue after logging the message.
  • warning - Warnings. The process will continue after logging the message.
  • info - Informational messages. The process will continue after logging the message.
  • debug - Debug messages. The process will continue after logging the message.

Log Purposes

Supported log purposes are:

  • general - normal log without any special purpose
  • performance - for performance measurements
  • network - for network requests
  • process - for process management
  • audit - for audit logs
  • security - for security logs
  • unknown - for unknown purposes or not yet implemented purposes

The initial log level is set from the environment variable LOG_LEVEL. If it's not found or cannot be parsed, the default log level setting is debug.

Example Usage

const logger = require('@kmhgmbh/logger').default;
const { LogLevel, LogPurpose } = require('@kmhgmbh/logger');

logger.appVersion = '[email protected]';
// set the log level default to 'info'
logger.defaultLogLevel = LogLevel.INFO;
// restrict log level to all above INFO
logger.currentLogLevel = LogLevel.INFO;
// set the log purpose default to 'general'
logger.defaultLogPurpose = LogPurpose.GENERAL;

// basic logging
logger.log({ message: 'Hello World!' });
// with purpose
logger.log({ message: 'Hello World!', purpose: LogPurpose.GENERAL });
// example for performance logging
logger.warning({ message: 'Average API request time has increased' , purpose: LogPurpose.PERFORMANCE });

// log content with context information
logger.info({
  message: 'Unauthorized access attempt',
  purpose: LogPurpose.AUDIT,
  context: {
    user: 'john.doe',
    ip: '127.0.0.1',
  },
});

// log errors
logger.error({
  message: 'Error while processing request',
  purpose: LogPurpose.NETWORK,
  context: new Error('Something went wrong'),
});

// log fatal errors
logger.fatal({
  message: 'Fatal error',
  purpose: LogPurpose.PROCESS,
  context: new Error('Something went wrong'),
});

Anonymization

The logger can be configured to anonymize the log output. This is useful for production environments where you don't want to log sensitive data. Sensitive data will be replaced with the string anonymized.

Nested objects and arrays are supported.

To enable anonymization, pass the anonymize option to the log function:

// set anonymousValue globally
logger.anonymousValue = 'anonymized'; // default

// set it globally
logger.anonymize = ['password'];

// or in the log function
logger.info({
  message: 'User authentication',
  purpose: LogPurpose.SECURITY,
  anonymize: ['password'],
  context: {
    username
    password,
  },
});

The console output will be:

{
  "logLevel": "info",
  "purpose": "security",
  "message": "User authentication",
  "appVersion": "unknown",
  "context": {
    "username": "john.doe",
    "password": "anonymized"
  }
}

Pretty Print

The logger can be configured to pretty print the log output. This is useful for development environments where you want to read the log output in a structured and colorful way. The Pretty Print option is disabled by default.

To enable pretty print, set the prettyPrint option to true

// set prettyPrint globally
logger.prettyPrint = true;

The console output will be:

alt text