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

dataset-logger

v1.5.0

Published

DataSet NodeJS Logger

Downloads

217

Readme

DataSet NodeJS Logger

npm package Build Status Downloads Issues Code Coverage Commitizen Friendly Semantic Release

NodeJS DataSet Logger

Install

With NPM:

npm install dataset-logger

Or if you use Yarn:

yarn add dataset-logger

Usage

import { DataSetEventSeverity, DataSetLogger } from 'dataset-logger';

const options = {
  // API Key is required.
  apiKey: 'YOUR DATASET WRITE LOGS API KEY',
  // SessionInfo is optional, it can be used to specify fields associated with the uploading process and
  // are appended to all of your events. These fields can then be used when querying the uploaded events.
  sessionInfo: {
    // Should generally specify at least a `serverHost` field, containing the hostname or other server
    // identifier. DataSet uses this value to organize events from different servers / sources.
    serverHost: 'front-1',
    serverType: 'frontend',
    region: 'us-east-1',
    application: 'some application name',
  },
};

const logger = new DataSetLogger(options);

// Simple events can be sent by just passing a string message:
logger.log('record retrieved');

// Or more complex events can be sent like:
logger.log({
  // This refers to the severity of the event
  // Possible severities are `INFO`, `WARN`, `ERROR`, `DANGER`
  sev: DataSetEventSeverity.INFO,
  // These are the attributes of the event, and can include nested properties
  attrs: {
    message: 'record retrieved',
    recordId: 39217,
    latency: 19.4,
    length: 39207,
  },
});

// Once done, make sure to close the logger so any remaining events are flushed
await logger.close();

To upload unstructured logs

This package also exports a small util function that allows to upload unstructured, plain-text logs. It can be used for lightweight integrations, and to upload batches of data from stateless environments.

It uses the uploadLogs API, it's recommended to read more about before using it.

import { uploadLogs } from './upload-logs';

// Can receive the full string text as a parameter:

(async () => {
  await uploadLogs({
    apiKey: 'YOUR DATASET WRITE LOGS API KEY',
    body: '{test: 123, field1: "value", field2: "value2"}',
    logfile: 'some-json.log',
    parser: 'json',
    sessionInfo: {
      serverHost: 'test-host',
      region: 'us-east',
    },
  });
})();

// Or can also receive a file full path, which will read and send its content:

(async () => {
  await uploadLogs({
    apiKey: 'YOUR DATASET WRITE LOGS API KEY',
    filePath: '/Users/user/some-json.log',
    logfile: 'some-json.log',
    parser: 'json',
    sessionInfo: {
      serverHost: 'test-host',
      region: 'us-east',
    },
  });
})();

API

DataSetLogger(options?)

options

Type: object

options.apiKey (required)

Type: string

options.serverUrl (optional)

Type: `string'

options.sessionInfo (optional)

Type: object

options.shouldFlattenAttributes (optional)

Type: boolean

If nested attributes should be flatten with dot notation for easier handling when working with the events in DataSet.

The following event payload:

attrs: {
  message: 'some message',
  record: {
    id: 'some id',
    name: 'some name',
    user: {
      id: 'user id',
      name: 'user name',
    },
  },
},

Would be converted to the following before sending it to DataSet:

attrs: {
  message: 'some message',
  'record.id': 'some id',
  'record.name': 'some name',
  'record.user.id': 'user id',
  'record.user.name': 'user name',
},

See flatten-nested-object.spec.ts for more cases.

options.onErrorHandler (optional)

Type: function

(error: Error) => void

options.onSuccessHandler (optional)

Type: function

(response: unknown) => void;