# beaver-logger

> Client side logger.

Latest version **4.0.36** (published 2023-12-13) · 0 weekly downloads

## Install

```sh
npm install beaver-logger
pnpm add beaver-logger
yarn add beaver-logger
bun add beaver-logger
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 4.0.36 |
| Published | 2023-12-13 |
| First published | 2016-05-28 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 982.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 256 |
| Maintainers | mstuart, gregjopa, bluepnume, wsbrunson, westeezy, mnicpt |
| Keywords | logging, logger, log, krakenjs, kraken |

## Links

- npm: https://www.npmjs.com/package/beaver-logger
- Repository: https://github.com/krakenjs/beaver-logger
- Homepage: https://github.com/krakenjs/beaver-logger#readme
- Issues: https://github.com/krakenjs/beaver-logger/issues
- npm.io page: https://npm.io/package/beaver-logger

## Dependencies (3)

- [belter](https://npm.io/package/belter.md) ^1.0.17
- [zalgo-promise](https://npm.io/package/zalgo-promise.md) ^1.0.26
- [cross-domain-utils](https://npm.io/package/cross-domain-utils.md) ^2.0.36

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 4.0.36 (latest) — 2023-12-13
- 3.0.20-alpha.metrics.1 (alpha) — 2022-10-14
- 4.0.34 — 2022-02-10
- 4.0.35 — 2022-02-10
- 4.0.33 — 2022-02-09
- 4.0.32 — 2021-09-29
- 4.0.31 — 2021-09-18
- 4.0.30 — 2021-09-08
- 4.0.29 — 2021-08-19
- 4.0.28 — 2021-03-16
- 4.0.27 — 2021-03-16
- 4.0.25 — 2021-02-23
- 4.0.24 — 2021-02-17
- 4.0.23 — 2021-01-25
- 4.0.22 — 2021-01-22
- … 61 more at https://npm.io/package/beaver-logger/versions

## README

beaver-logger
------------

[![npm version][version-badge]][package]

[version-badge]: https://img.shields.io/npm/v/beaver-logger.svg?style=flat-square
[package]: https://www.npmjs.com/package/beaver-logger

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

```javascript
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

```javascript
$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

```javascript
$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

```javascript
$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

```javascript
$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

```html
<script src="/js/beaver-logger.min.js"></script>
```

or

```javascript
let $logger = require('beaver-logger');
```


Configuration
-------------

Full configuration options:

```javascript
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:

```javascript
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:

```json
      "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:

```javascript
module.exports = {

    log: function(req, level, event, payload) {

        logSocket.send(JSON.stringify({
            level: level,
            event: event,
            payload: payload
        }));
    }
}
```


Data Flow
---------

![Flow](/flow.png?raw=true)

---
_Source: https://npm.io/package/beaver-logger · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
