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

@3fv/logger-proxy

v1.0.70

Published

A logger proxy to enable hot swapping backends

Downloads

1,687

Readme

@3fv/logger-proxy


Overview

With so many logging libraries out there, and the fact that I felt I was creating the same boilerplate configs with different libs depending on target platforms as well as logging backends, etc. I finally decided to implement a simple logging framework that's meant to be used with other frameworks & loggers; Tracer, Morgan, Winston.

Works across browser & node & deno

Now, that is a fairly common statement from those that write logging frameworks including the aforementioned; but this is truly meant to be a proxy & with that it enables things like backend async context stacks. i.e. default config goes to a file appender, but your job's also have an additional append via an async context (Think ThreadLocal in the java or std::thread_local in c++ ).

@3fv/logger-proxy hotswap logging backends

Install

yarn add @3fv/logger-proxy

Usage

Basic

import {
  getLogger,
  LevelNames, getLoggingManager
} from "@3fv/logger-proxy"


getLoggingManager().configure({
  // Default appenders list is [ConsoleAppender], 
  // so the following is not needed and only 
  // remains as an example:
  //
  // appenders: [new ConsoleAppender()],
  rootLevel: "trace"
})

const log = getLogger(__filename)

LevelNames.forEach((name) =>
  log[name].call(log, `example %s`, name)
)

Context Stacks (the coolest bit)

For verboseness as well as the fact I'm lazy, here's a complete unit test illustrating the capabilities (in jest)

import {
  Appender,
  getLoggingManager,
  LogContext,
  Logger,
  getLogger
} from "@3fv/logger-proxy"

type Jest = typeof jest
type MockAppender = Appender & {
  append: Appender["append"] & ReturnType<Jest["fn"]>
}

function newMockAppender(): MockAppender {
  const fn = jest.fn((record: any) => {
    console.log(`record`, record)
  })
  return {
    append: fn
  }
}

describe("NodeContextProvider", () => {
  jest.setTimeout(10000)
  
  const manager = getLoggingManager()
  let baseAppender: MockAppender
  let contextAppender1: MockAppender
  let contextAppender2: MockAppender
  let context1: LogContext
  let context2: LogContext
  let log1: Logger
  let log2: Logger
  
  beforeEach(() => {
    baseAppender = newMockAppender()
    
    contextAppender1 = newMockAppender()
    contextAppender2 = newMockAppender()
    
    context1 = LogContext.with([contextAppender1])
    context2 = LogContext.with([contextAppender2])
    
    manager.setAppenders(baseAppender).setRootLevel("debug")
    
    log1 = getLogger("log1")
    log2 = getLogger("log2")
  })
  
  it("works with no contexts", async () => {
    log1.info("test1")
    log2.info("test2")
    
    expect(baseAppender.append).toBeCalledTimes(2)
  })
  
  it("works with no context provider", async () => {
    log1.info("test1")
    await context1.use(async () => {
      log1.info("test2")
    })
    
    expect(baseAppender.append).toBeCalledTimes(2)
    expect(contextAppender1.append).toBeCalledTimes(0)
  })
  
  it("works with 1 contexts", async () => {
    
    // You must explicitly `install` the context provider to use contexts
    await import("@3fv/logger-proxy/context/providers/node")
    
    log1.info("test1")
    await context1.use(async () => {
      log1.info("test2")
    })
    
    expect(baseAppender.append).toBeCalledTimes(2)
    expect(contextAppender1.append).toBeCalledTimes(1)
  })
  
  it("works with n contexts", async () => {
    
    // You must explicitly `install` the context provider to use contexts
    await import("@3fv/logger-proxy/context/providers/node")
    
    log1.info("test1")
    await context1.use(async () => {
      log1.info("test2")
      await context2.use(async () => {
        log1.info("test3")
      })
    })
    
    expect(baseAppender.append).toBeCalledTimes(3)
    expect(contextAppender1.append).toBeCalledTimes(2)
    expect(contextAppender2.append).toBeCalledTimes(1)
  })
})