4.0.36 • Published 5 months ago

beaver-logger v4.0.36

Weekly downloads
30,998
License
-
Repository
github
Last release
5 months ago

beaver-logger

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.

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.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

Flow

4.0.36

5 months ago

4.0.34

2 years ago

4.0.33

2 years ago

4.0.35

2 years ago

4.0.32

3 years ago

4.0.31

3 years ago

4.0.30

3 years ago

4.0.29

3 years ago

4.0.27

3 years ago

4.0.28

3 years ago

4.0.25

3 years ago

4.0.24

3 years ago

4.0.23

3 years ago

4.0.22

3 years ago

4.0.21

4 years ago

4.0.20

4 years ago

4.0.18

4 years ago

4.0.17

4 years ago

4.0.16

4 years ago

4.0.15

4 years ago

4.0.14

4 years ago

4.0.13

4 years ago

4.0.12

5 years ago

3.0.19

5 years ago

3.0.18

5 years ago

4.0.11

5 years ago

4.0.10

5 years ago

4.0.9

5 years ago

4.0.8

5 years ago

4.0.7

5 years ago

4.0.6

6 years ago

4.0.5

6 years ago

4.0.4

6 years ago

4.0.3

6 years ago

4.0.2

6 years ago

4.0.1

6 years ago

4.0.0

6 years ago

3.0.17

6 years ago

3.0.16

6 years ago

3.0.15

6 years ago

3.0.14

6 years ago

3.0.13

6 years ago

3.0.12

6 years ago

3.0.11

6 years ago

3.0.10

7 years ago

3.0.9

7 years ago

3.0.8

7 years ago

3.0.7

7 years ago

3.0.6

7 years ago

3.0.5

7 years ago

3.0.4

7 years ago

3.0.3

7 years ago

3.0.2

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.0.8

7 years ago

2.0.7

7 years ago

2.0.6

7 years ago

2.0.5

7 years ago

2.0.4

7 years ago

2.0.3

7 years ago

1.0.17

8 years ago

2.0.2

8 years ago

2.0.1

8 years ago

1.0.16

8 years ago

1.0.15

8 years ago

1.0.13

8 years ago

1.0.12

8 years ago

1.0.11

8 years ago

1.0.10

8 years ago

1.0.9

8 years ago

1.0.8

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.0

8 years ago