# loglevel-local-storage

> NPM plugin for JS logger loglevel which will log all messages to localStorage

Latest version **1.1.3** (published 2017-05-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install loglevel-local-storage
pnpm add loglevel-local-storage
yarn add loglevel-local-storage
bun add loglevel-local-storage
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.3 |
| Published | 2017-05-23 |
| First published | 2016-07-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Gijs Boekema |
| Maintainers | bartvanandel, gpcboekema |
| Keywords | loglevel |

## Links

- npm: https://www.npmjs.com/package/loglevel-local-storage
- Repository: https://github.com/gpcboekema/loglevel-localStorage
- Homepage: https://github.com/gpcboekema/loglevel-localStorage#readme
- Issues: https://github.com/gpcboekema/loglevel-localStorage/issues
- npm.io page: https://npm.io/package/loglevel-local-storage

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) ^4.13.1

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

- 1.1.3 (latest) — 2017-05-23
- 1.1.2 — 2016-08-17
- 1.1.1 — 2016-07-21
- 1.1.0 — 2016-07-19
- 1.0.0 — 2016-07-18
- 0.0.3 — 2016-07-15
- 0.0.2 — 2016-07-15
- 0.0.1 — 2016-07-15

## README

NPM plugin for JS logger [loglevel](https://github.com/pimterry/loglevel) which will log all messages to localStorage

# Features
- Messages put on a stack, stack is stored entirely.
- Max stack size for logs to be stored can be set.
- You can specify prefix function which would dynamically add prefix for all log messages (app name, current user, etc.).

# Installation
- Using npm = `npm install loglevel-local-storage`

# Configuration
import `loglevel` and `loglevel-localStorage` scripts to your file first. Then configure the plugin:

##loglevelLocalStorage(logger, options)
Extend loglevel with new plugin which will send log information to the log-sever

| Param | Type | Description |
| ----- | ---- | ----------- |
| logger | <code>object</code> | loglevel instance to be extended |
| options | <code>object</code> |  |
| \[options.storeKey=<code>'loglevelLocalStorage'</code>\] | <code>string</code> | Name for the store in localStorage. |
| \[options.maxLogStackSize=<code>100</code>\] | <code>number</code> | Max stack size to store. |
| \[options.callOriginal=<code>false</code>\] | <code>Bool</code> | If set to true - original loglevel method for logging would be called |
| \[options.prefix=<code>null</code>\] | <code>string</code> \| <code>function</code> | Prefix for all log messages. Either string or function wich should return string and accept log severity and message as parameters |
| \[options.level=<code>logger.levels.WARN</code>\] | <code>string</code> \| <code>number</code> | Log level. Either string or number, See [loglevel](https://github.com/pimterry/loglevel).setLevel. |
| \[options.persistentLog=<code>false</code>\] | <code>Bool</code> | If set to true - persist log entries between instantiations of the same log stack. |

**Example**
```js
                                                
var debuglog = require('loglevel'); // import debuglog from 'loglevel';
var loglevelLocalStorage = require('../loglevel-localStorage.js'); // import loglevelLocalStorage from 'loglevel-local-storage';
 
debuglog.setLevel(1);
 
loglevelLocalStorage(debuglog, {
    level: 'info',
    prefix: function (logSev /* , ...message */) {
        var message = Array.prototype.slice.call(arguments, 1);
        return '[' + new Date().toISOString() + '] ' + logSev + ': ' + message.join(' ');
    },
    callOriginal: true,
    maxLogStackSize: 10
});
 
var foo = function () {
    var test = "123";
    debuglog.info("test", test);
};
 
foo();
 
// output console (callOriginal = true):  
// test 123
// output localStorage (see prefix function above):
// [2016-07-19T15:39:18.110Z] info: test 123
 
// ES6 example:
import debuglog from 'loglevel';
import loglevelLocalStorage from 'loglevel-local-storage';

debuglog.setLevel("DEBUG");
loglevelLocalStorage(debuglog, {
    level: 'info',
    prefix(logSev, ...args) {
        // Stringify for storage in localStorage
        const newArgs = args.map(arg => ((typeof arg === 'object' && !(arg instanceof Date)) ? JSON.stringify(arg) : arg));
        return `[${new Date().toISOString()}] ${logSev}: ${newArgs.join(' ')}`;
    },
    callOriginal: true,
    maxLogStackSize: 10,
});
 
```

**Another Prefix Example**

You may keep the log in a JSON format to be able to parse later
```js
    ...
    prefix: function (logLevel) {
        var message = Array.prototype.slice.call(arguments, 1);
        return [new Date(), logLevel, message];
    }
    ...
```

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