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

@healthmatch/cloudwatch-front-logger

v0.0.11

Published

Save your browser console logs to AWS CloudWatch (Inspired by agea/console-cloud-watch)

Downloads

5

Readme

CloudWatch Front Logger Build Status npm

Save your browser console logs to AWS CloudWatch (Inspired by agea/console-cloud-watch)

Install

npm i cloudwatch-front-logger

Preparation

1. Create Public Log Group

Go to CloudWatch console and create Log Group for this purpose.

2. Create Policy

Go to IAM Console and create restricted policy for CloudWatch Logs.

  • logs:CreateLogStream
  • logs:PutLogEvents
{
    "Version": "2019-12-08",
    "Statement": [
        {
            "Sid": "CloudWatchFrontLoggerSid",
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": [
                "arn:aws:logs:*:*:log-group:<LOG_GROUP_NAME>:*:*",
                "arn:aws:logs:*:*:log-group:<LOG_GROUP_NAME>"
            ]
        }
    ]
}

3. Create IAM user

Go to IAM Console and create user with the restricted policy.

Basic Usage

import { Logger } from 'cloudwatch-front-logger'

const accessKeyId = 'XXXXXXXXXXXXXXXXXXXX'
const secretAccessKey = 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY'
const region = 'ap-northeast-1'
const logGroupName = '<LOG_GROUP_NAME>'

const logger = new Logger(accessKeyId, secretAccessKey, region, logGroupName)
logger.install()

Logs are collected from the following sources.

  • Uncaught Error
  • console.error() call
  • Manual logger.onError() call(See integration examples)

Advanced Usage

Personalize LogStream

By default, "anonymous" is used for logStreamName value. If you wish allocating a unique stream for each user, you can use a method such as Canvas Fingerprint. Pass a resolver function as logStreamNameResolver option value on install() call.

import Fingerprint2 from 'fingerprintjs2'

logger.install({
  logStreamNameResolver() {
    return new Promise((resolve) => new Fingerprint2().get(resolve))
  },
})

Customize Formatted MessageFilter Errors

By default, messages are formatted into JSON string which has message and type.

{
  "message": "Err: Something went wrong",
  "type": "uncaught"
}
{
  "message": "Something went wrong",
  "type": "console",
  "level": "error"
}

If you wish formatting them by yourself, pass a formatter function as messageFormatter option value on install() call. Note that you can cancel by returning null from the fuunction.

import StackTrace from 'stacktrace-js'

logger.install({
  async messageFormatter(e, info = { type: 'unknown' }) {
    if (!e.message) {
      return null
    }

    let stack = null
    if (e.stack) {
      stack = e.stack
      try {
        stack = await StackTrace.fromError(e, { offline: true })
      } catch (_) {
      }
    }

    return JSON.stringify({
      message: e.message,
      timestamp: new Date().getTime(),
      userAgent: window.navigator.userAgent,
      stack,
      ...info,
    })
  },
})

Use Asynchronous Storage instead of localStorage

By default, localStorage is used for caching logStreamName and nextSequenceToken. Still localStorage has only synchronous API, asynchronous interfaces are also supported. If you need to change storage implementation from localStorage, pass an instance as storage option value on install() call.

import { AsyncStorage } from 'react-native'

logger.install({
  storage: AsyncStorage,
})

Integration Examples

React (Component)

class LoggerComponent extends React.component {

  componentDidCatch(e, info) {
    this.props.logger.onError(e, {
      ...info,
      type: 'react',
    })
  }

  render() {
    return this.props.children
  }
}
<LoggerComponent logger={logger}>
  <App />
</LoggerComponent>

Redux (Middleware)

const createLoggerMiddleware = (logger) => (store) => (next) => (action) => {
  try {
    return next(action)
  } catch (e) {
    logger.onError(e, {
      action,
      type: 'redux',
    })
  }
}
const store = createStore(
  combineReducers(reducers),
  applyMiddleware(createLoggerMiddleware(logger))
)