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

@exobase/use-init-logger

v1.0.0-rc.1

Published

Exobase init hook to proxy all console log calls to a logger

Downloads

4

Readme

An Exobase init hook that proxies calls to the global standard console functions (log, warn, error, debug) to a logger you specify.

Some engineers or teams may not be a fan of this. Thats ok. This is a hook I (@rayepps) have used a lot and now can't live without. Locally, we use a simple environment variable check to disable the customer logger which gives us beautiful/readable logs as engineers. In production, we have the assurance of knowing that no log can possibly escape our logger because we've proxied the console.log functions.

I highly recommend 👍

Install

Yarn

yarn add @exobase/use-init-logger

then

import { useLogger } from '@exobase/use-init-logger'

or, install with the hooks package

yarn add @exobase/hooks

then

import { useLogger, useServices, useJsonArgs, useCors } from '@exobase/hooks'

Usage

This is a hook you'll probably use on every endpoint in the same way so you'll want to compose it in one place with all your logging config. We'll do that below, here's a quick naive implementation of a file logger.

import { compose, isObject } from 'radash'
import type { Props } from '@exobase/core'
import { useLambda } from '@exobase/lambda'
import { useLogger } from '@exobase/hooks'
import fs from 'fs'

const endpoint = (props: Props) => {
  console.log('This will be sent to the endpoint.log file')
}

export default compose(
  useLogger({
    logger: FileLogger('endpoint.log')
  }),
  useLambda(),
  endpoint
)

Here's the file logger

const FileLogger = (file: string) => {
  const stream = fs.createWriteStream(file, { flags: 'a' })
  const log =
    (level: 'log' | 'warn' | 'error' | 'debug') =>
    (...args: any[]) => {
      const message = args.reduce(
        (acc, arg) =>
          isObject(arg)
            ? { ...acc, ...arg }
            : { ...acc, message: `${arg.message} ${arg}` },
        { message: '', level: level.toUpperCase() }
      )
      stream.write(JSON.stringify(message))
    }
  return {
    log: log('log'),
    warn: log('warn'),
    error: log('error'),
    debug: log('debug')
  }
}

LogTail Example

We have deep love for log tail. This is how we use LogTail with the useLogger hook. First, we compose the hook in a hook/useLogger file in our project so we don't have to do the setup in every endpoint.

// ~/hooks/useLogger.ts
import { useLogger as exobaseUseLogger } from '@exobase/use-init-logger'

export const useLogger = () =>
  exobaseUseLogger({
    logger: LogTailLogger(),
    awaitFlush: true,
    reuseLogger: true,
    passthrough: true,
    disable: process.env.ENV_NAME === 'local'
  })

Here's the LogTailLogger.

import { Logtail } from '@logtail/node'
import config from '~/config'

const LogTailLogger = () => {
  const tail = new Logtail(config.logtail.token, {
    batchSize: 2,
    batchInterval: 15
  })
  return {
    log: tail.log.bind(tail),
    warn: tail.warn.bind(tail),
    error: tail.error.bind(tail),
    debug: tail.debug.bind(tail)
  }
}

Lastly, in our endpoint, we import our own useLogger instead of the Exobase useLogger hook directly.

import { useLogger } from '~/hooks/useLogger'

export default compose(useLogger(), useLambda(), endpoint)