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

@optimizely/js-sdk-logging

v0.3.1

Published

Optimizely Full Stack Core Logging

Downloads

802,639

Readme

Javascript SDK Logging

Provides a centralized LogManager and errorHandler for Javascript SDK packages.

Installation

npm install @optimizely/js-sdk-logging

Architecture

Logging Architecture

  • LogHandler - the component that determines where to write logs. Common log handlers are ConsoleLogHandler or NoopLogHandler
  • LogManager - returns Logger facade instances via LogManager.getLogger(name)
  • LoggerFacade the internal logging interface available to other packages via LogManager.getLogger(name)

Usage

Using the logger

import { getLogger } from '@optimizely/js-sdk-logging'

const logger = getLogger('myModule')
logger.log('warn', 'this is a warning')

logger.debug('string interpolation is easy and %s', 'lazily evaluated')

logger.info('info logging')
logger.warn('this is a warning')
logger.error('this is an error')

// `info` `warn` `debug` and `error` all support passing an Error as the last argument
// this will call the registered errorHandler
logger.error('an error occurred: %s', ex.message)

// also Error passes to errorHandler.handleError(ex)
logger.error('an error occurred: %s', ex.message, ex)

// if no message is passed will log `ex.message`
logger.error(ex)

Setting the log level

import { LogLevel, setLogLevel } from '@optimizely/js-sdk-logging'

// can use enum
setLogLevel(LogLevel.ERROR)

// can also use a string (lowercase or uppercase)
setLogLevel('debug')
setLogLevel('info')
setLogLevel('warn')
setLogLevel('error')

Setting a LogHandler

import { setLogHandler, ConsoleLogHandler } from '@optimizely/js-sdk-logging'

const handler = new ConsoleLogHandler({
  logLevel: 'error',
  prefix: '[My custom prefix]', // defaults to "[OPTIMIZELY]"
})

setLogHandler(handler)

Implementing a custom LogHandler

Perhaps you want to integrate Optimizely with your own logging system or use an existing library.

A valid LogHandler is anything that implements this interface

interface LogHandler {
  log(level: LogLevel, message: string): void
}

Example: integrating with Winston

import winston from 'winston'
import { setLogHandler, LogLevel } from '@optimizely/js-sdk-logging'

const winstonLogger = winston.createLogger({
  level: 'info',
  format: winston.format.json(),
  defaultMeta: { service: 'optimizely' },
  transports: [
    new winston.transports.File({ filename: 'combined.log' }),
  ],
})

/**
 * Convert from optimizely log levels to winston
 */
function convertLogLevels(level) {
  switch(level) {
    case LogLevel.DEBUG:
      return 'debug'
    case LogLevel.INFO:
      return 'info'
    case LogLevel.WARNING:
      return 'warning'
    case LogLevel.ERROR:
      return 'error'
    default:
      return 'silly'
  }
}

setLogHandler({
  log(level, message) {
    winstoLogger.log({
      level: convertLogLevels(level),
      message,
    })
  }
})

API Interfaces

interface LoggerFacade {
  log(level: LogLevel | string, message: string): void

  info(message: string | Error, ...splat: any[]): void

  debug(message: string | Error, ...splat: any[]): void

  warn(message: string | Error, ...splat: any[]): void

  error(message: string | Error, ...splat: any[]): void
}

interface LogManager {
  getLogger(name?: string): LoggerFacade
}

interface LogHandler {
  log(level: LogLevel, message: string): void
}