1.0.0 • Published 12 days ago

@asdfgertyjhnpm/excepturi-minima-atque v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
12 days ago

beaver-logger

build status code coverage npm version

Front-end logger, which will:

  • Buffer your front-end logs and periodically send them to the server side
  • Automatically flush logs for any errors or warnings

This is a great tool to use if you want to do logging on the client side in the same way you do on the server, without worrying about sending off a million beacons. You can quickly get an idea of what's going on on your client, including error cases, page transitions, or anything else you care to log!

Overview

Setup

var $logger = beaver.Logger({
  url: "/my/logger/url",
});

Basic logging

$logger.info(<event>, <payload>);

Queues a log. Options are debug, info, warn, error.

For example:

$logger.error('something_went_wrong', { error: err.toString() })

$logger.track(<payload>);

Call this to attach general tracking information to the current page. This is useful if the data is not associated with a specific event, and will be sent to the server the next time the logs are flushed.

$logger.metricCounter(<event>, <payload>);

Queues a counter metric, helper wrapping logger.metric

logger.metricCounter({
  namespace: "pp.team.product.feature",
  event: "button_click",
  dimensions: {
    type: "paypal"
  }
})

Using a namespace prefix

const logger = new Logger({...options, metricNamespacePrefix: "company.team.app"})

logger.metricCounter({
  namespace: "product.feature",
  event: "button_click",
})

// creates metric with namespace of
// company.team.app.product.feature

$logger.metricGauge(<event>, <payload>);

Queues a gauge metric, helper wrapping logger.metric

logger.metricGauge({
  namespace: "pp.team.product.feature",
  event: "request_latency",
  value: 100,
  dimensions: {
    method: "GET"
  }
})

Using a namespace prefix

const logger = new Logger({...options, metricNamespacePrefix: "company.team.app"})

logger.metricGauge({
  namespace: "product.feature",
  event: "request_latency",
  value: 100
})

// creates metric with namespace of
// company.team.app.product.feature

Deprecated - $logger.metric(<event>, <payload>);

Queues a metric. We suggest using the metricCount or metricGauge interface for better type safety and clearer intention in your code.

Advanced

$logger.addMetaBuilder(<function>);

Attach a method which is called and will attach general information to the logging payload whenever the logs are flushed

$logger.addMetaBuilder(function () {
  return {
    current_page: getMyCurrentPage(),
  };
});

$logger.addMetricDimensionBuilder(<function>);

Attach a method which is called and will attach values to each metric's dimensions whenever the logs are flushed

$logger.addMetricDimensionBuilder(() => ({
  token_used: true,
  type: "user_id_token",
}));

$logger.addPayloadBuilder(<function>);

Attach a method which is called and will attach values to each individual log's payload whenever the logs are flushed

$logger.addPayloadBuilder(function () {
  return {
    performance_ts: window.performance.now(),
  };
});

$logger.addTrackingBuilder(<function>);

Attach a method which is called and will attach values to each individual log's tracking whenever the logs are flushed

$logger.addTrackingBuilder(function () {
  return {
    pageLoadTime: getPageLoadTime(),
  };
});

$logger.addHeaderBuilder(<function>);

Attach a method which is called and will attach values to each individual log requests' headers whenever the logs are flushed

$logger.addHeaderBuilder(function () {
  return {
    "x-csrf-token": getCSRFToken(),
  };
});

$logger.flush();

Flushes the logs to the server side. Recommended you don't call this manually, as it will happen automatically after a configured interval.

Installing

  • Install via npm

npm install --save beaver-logger

  • Include in your project
<script src="/js/beaver-logger.min.js"></script>

or

let $logger = require("beaver-logger");

Configuration

Full configuration options:

var $logger = beaver.Logger({
  // Url to send logs to
  url: "/my/logger/url",

  // Prefix to prepend to all events
  prefix: "myapp",

  // Log level to display in the browser console
  logLevel: beaver.LOG_LEVEL.WARN,

  // Interval to flush logs to server
  flushInterval: 60 * 1000,

  // Use sendBeacon if supported rather than XHR to send logs; defaults to false
  enableSendBeacon: true,
});

Server Side

beaver-logger includes a small node endpoint which will automatically accept the logs sent from the client side. You can mount this really easily:

let beaverLogger = require("beaver-logger/server");

myapp.use(
  beaverLogger.expressEndpoint({
    // URI to recieve logs at
    uri: "/api/log",

    // Custom logger (optional, by default logs to console)
    logger: myLogger,

    // Enable cross-origin requests to your logging endpoint
    enableCors: false,
  })
);

Or if you're using kraken, you can add this in your config.json as a middleware:

      "beaver-logger": {
          "priority": 106,
          "module": {
              "name": "beaver-logger/server",
              "method": "expressEndpoint",
              "arguments": [
                  {
                      "uri": "/api/log",
                      "logger": "require:my-custom-logger-module"
                  }
              ]
          }
      }

Custom backend logger

Setting up a custom logger is really easy, if you need to transmit these logs to some backend logging service rather than just logging them to your server console:

module.exports = {
  log: function (req, level, event, payload) {
    logSocket.send(
      JSON.stringify({
        level: level,
        event: event,
        payload: payload,
      })
    );
  },
};

Data Flow

flowchart TD
    A[Client-Side Log statement] --> B[beaver-logger/client]
    B[beaver-logger/client] --> C[beaver-logger/server]
    C[beaver-logger/server] --> D[your-custom-logger]
    D[your-customer-logger] --> E[Backend 1]
    D[your-customer-logger] --> F[Backend 2]
    G[Server-Side Log statement] --> D[your-custom-logger]
ECMAScript 2022jsonpathclassnameserializerhooksString.prototype.trimflatpathlimitdiffassignregularrequireassertsforEachastautoprefixerrmdir__proto__eventDispatchersetImmediatebddsidedefinePropertystyleguideinterruptsES2015everystartersyntaxerrornodebindimmutableWebSocketES2016waitfast-cloneviewArrayReactiveXprotojson-schema-validatorreact-hook-formutilityInt16ArrayerrorsigintclimonorepomatchjsdiffObject.fromEntries@@toStringTagcode pointsframerwatching6to5throttlewrapoptimisthttpsremoveaccessorURLSearchParamsrgbpositiveArray.prototype.filterReactiveExtensionsopenslinewraputilstypeofreduxzodArrayBuffer.prototype.slicecacheclassnameswhatwgglobreact posechannelfastcolorStyleSheetStreamsbusywatchjQuerycallbackcolourtaskregexpchinesestatusvalidationstableopenutilpasswordstructuredClonewaapiObject.entrieslistenersurlsRFC-6455groupparsexssargvguidbootstrap cssanimationdatapicomatchfull-widthes2018testeshasOwntypeddirectorygraphqlspringtakeES2022rm -frES8higher-ordersearchlazyschemashimArrayBuffertoolkitHyBicmdfastifyfolderJSONES2023ECMAScript 2023$.extendcryptonested cssenumerableEShandlerscoerciblepackagesmruregular expressionslinuxserializedragfindReflect.getPrototypeOfl10ntc39groupBylimitedequalitysignalwarningargumentmulti-packagemimematchesimmerinspectvaluecircularunicodees8exit-codefunctionalString.prototype.matchAllcomparerandomcodestestingbrowserbuffermkdirpPromiseeditorefficienttoArrayAsyncIteratoriemomentobjectprocessarraybufferparentsemitArray.prototype.flattenfunctionconnectsharedUint16Arraytypesfphookformes2015TypeBoxframeworkurlfindLastIndexauthmobileextrairqreadablewidthoutputlibphonenumberbrowserslistFunction.prototype.nameiteratermchromiumextendvarnameshebangpropprettysyntaxsymlinkArray.prototype.flattoSortedcss nestingbyteLengthasciikarmanodejscontainsconfigfindLastdebuggernegativeprotobufescapeharmonycollectionexpressObject.assigninputdeepcopysanitizationbundlingpopmotionidfast-copyimportutilitiesboundStreamInt8ArrayuppolyfillSymbol.toStringTagmake dirclassesfile systemconcatpurearraysconsoleconcurrencycheckprototypecss variablebreakflagstrimiterationkeyslinkmochaexeSethashes-abstractwordbreakflatMapflattentrimRightECMAScript 6look-upmakenopedescriptionWebSocketstranspilerObject.valuesconsumegetoptstreamseast-asian-widthArray.prototype.findLastsafegradients css3colorsless mixinsoncevaluesFloat32Arrayfigletargparseweaksetcss lessES6corsawaitWeakSetcreatephoneintrinsicdatecommanderreactpackage managerreact animationrecursivechromebannertesterdeletewgetuuidmaptoobjectdescriptorsObservablespyyamlFloat64Arrayio-tsajaxless.jsenvironmentdescriptormacosuser-streamsjavascriptObject.definePropertyES2017serializationECMAScript 2019batchTypedArraytypedarrayequalenvsetPrototypeOfyupBigUint64ArraysettingscompilerwordwrapwatcherreadablestreambytemergeObject.keysdeepWeakMapcolumnsairbnbnpmECMAScript 2018eslint-pluginfast-deep-copyfastcopypnpm9apiquotecollection.es6stringifierobjES2018tsmimetypeslanguagegetterjsonschemaArrayBuffer#sliceparentrequestpushisConcatSpreadablees6tostringtaggetOwnPropertyDescriptorappfastclonesetcss-in-jstslibmodulees-shimssigtermexecbcryptxdg-openajvtyped arraymixinsstylesheetfilenameseslintsuperstructPushtapspinnerpromiseRegExp.prototype.flagsUnderscorecjktimegradients cssfantasy-landyamldom-testing-libraryIteratorquerystringqsresolvetypeshellfind-upthreecopyspinnersconcatMapcurrieddomopenernativebufferslintwebsitees2017columnECMAScript 2021ECMAScript 2015babel-corequeuereducespecfixed-widthutil.inspectES2021csstapereusedayjscore-jsramdadotenvECMAScript 7toStringTagreal-timecall-bindES2019jwttrimStarthardlinkswindowsclientstartscheme-validationparsercharacterlrumkdirformatvestdependency managerencryptionprunewebcomputed-typespersistentjsdomenderECMAScript 2016call-boundtelephoneclass-validatores-shim APIfsArray.prototype.includesfilterdeep-copyjson-schema-validationfindupsortedelectronwhichmiddlewarewalkingargsbluebirdinternal slotshrinkwrapfseventslessasynccurlvisualavaoffsetlogstyled-componentscommandRegExp#flagslookfetchponyfillsettertermECMAScript 2020performanceconfigurablespeedMicrosoftSymbolprogressstylespromisesless compiler3dArray.prototype.findLastIndexauthenticationminimalmatchAllsignalswalkredactMapArray.prototype.flatMapbabelcryptUint8ClampedArrayECMAScript 5packagereact-testing-libraryESnextjson-schemaES7jsxstringCSSdirshaminstall0trimLeftObservableinternaliteratorslot-0YAMLcallboundBigInt64Arrayfullstyleexecutablea11yjestgetxhrargumentspackage.jsoneslintconfigrm -rfcloneless cssidleoperating-system.envjsonpluginemojitddfullwidthObjectes2016ECMAScript 2017form-validationpropertiestouchwritablesomerfc4122arraygetPrototypeOfaccessibilitykoreansortchaiformattingformvalidrapidstringifycharactersregular expressionexpressiontoolsprefixcensorcompile lesswritetypanionomitxtermassertioncommand-lineloggingecmascriptestreeES5rangeerrordeepclonepoint-freereadregexvalidateeventsECMAScript 3keypostcss-pluginfast-deep-clonesanitizeRxcoreES2020ttyprivate databrowserlistfromlengthInt32ArraysharedarraybufferbyteOffsetchildjapanesesymlinksUint8ArrayrobustletapolloistextartdebugdefaultqueueMicrotaskspawnmetadataratelimitxdgstatelesslastvalidatordatastructureJSON-Schemadefinebundler
1.0.0

12 days ago