Gathering detailed insights and metrics for loglevel-plugin-remote
Gathering detailed insights and metrics for loglevel-plugin-remote
Gathering detailed insights and metrics for loglevel-plugin-remote
Gathering detailed insights and metrics for loglevel-plugin-remote
npm install loglevel-plugin-remote
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
102 Stars
85 Commits
36 Forks
3 Watching
3 Branches
4 Contributors
Updated on 23 Aug 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-21.3%
1,709
Compared to previous day
Last week
12.8%
9,666
Compared to previous week
Last month
4.2%
40,117
Compared to previous month
Last year
-1.6%
340,799
Compared to previous year
A loglevel plugin for sending browser logs to a server.
1npm i loglevel-plugin-remote --save
This plugin is under active development and should be considered as an unstable. No guarantees regarding API stability are made. Backward compatibility is guaranteed only by path releases.
apply(loglevel, options)
This method applies the plugin to the loglevel.
loglevel
- the root logger, imported from loglevel package
options
- an optional configuration object
1const defaults = { 2 url: '/logger', 3 method: 'POST', 4 headers: {}, 5 token: '', 6 onUnauthorized: failedToken => {}, 7 timeout: 0, 8 interval: 1000, 9 level: 'trace', 10 backoff: { 11 multiplier: 2, 12 jitter: 0.1, 13 limit: 30000, 14 }, 15 capacity: 500, 16 stacktrace: { 17 levels: ['trace', 'warn', 'error'], 18 depth: 3, 19 excess: 0, 20 }, 21 timestamp: () => new Date().toISOString(), 22 format: remote.plain, 23};
url (string) - a URL of the server logging API
method (string) - a HTTP method of the server logging API
headers (object) - this is to support custom headers passed down to the post request. For example { 'my-header': 'myHeaderValue' }
token (string) - a token for Bearer authentication scheme (see RFC 6750), e.g. UUID or JWT. By default is ''
. If you set the value to undefined
, the sending of logs will be suspended until the setToken
method is called, but the logs will still accumulate in the queue.
onUnauthorized (function) - this function will be called after the sending of logs is suspended due to an authentication error. A token that has not been authenticated will be passed to the function.
timeout (number) - a timeout in milliseconds (see XMLHttpRequest.timeout)
interval (number) - a time in milliseconds between sending messages. By default is 1000 (one second).
level (string) - a plugin specific level. The plugin level does not cancel the level of the logger. The highest level will be used to send messages. If the level of the logger is warn
, and the level of the plugin is error
, only messages with the error
level will be sent to the server. By default is trace
.
backoff (function|object) - a function used to increase the sending interval after each failed send. The function receives the duration of the previous suspend and returns the duration of the next suspend. Instead of the function backoff can be represented by an object with 3 properties: multiplier
, jitter
and limit
. By default, it doubles the interval (multiplier=2) and adds 10% jitter (jitter=0.1). Having reached the value of 30 seconds (limit=30000), the interval increase stops. After successful sending, the interval will be reset to the initial value.
capacity (number) - the size of the queue in which messages are accumulated between sending. By default is 500. Overflow will delete the oldest messages. It is forbidden to set the value to 0 - in this case the default value will be used.
stacktrace (object) - object for configuring a stacktrace with the following properties:
trace
, warn
and error
.timestamp (function) - a function that returns a timestamp. By default, it returns the time in the ISO format (see ISO 8601)
format (function) - this option defines the logs format. This function will generate a log, obtaining as a single argument a log object that looks like this:
1const log = { 2 message: 'Text', 3 level: { 4 label: 'info', 5 value: 2, 6 }, 7 logger: 'child', 8 timestamp: '2017-05-29T12:53:46.000Z', 9 stacktrace: '', 10};
When the function returns a string, the logs will be sent as plain text. The default value is function remote.plain
:
1function plain(log) { 2 return `[${log.timestamp}] ${log.level.label.toUpperCase()}${ 3 log.logger ? ` (${log.logger})` : '' 4 }: ${log.message}${log.stacktrace ? `\n${log.stacktrace}` : ''}`; 5}
Then when you call:
1log.getLogger('child').info('Info message'); 2log.error('Error message');
the logs look like this:
[2017-05-29T12:53:46.000Z] INFO (child): Info message
[2017-05-29T12:53:46.001Z] ERROR: Error message
at http://localhost/test.js:12:5
When the function returns an object, the logs will be sent in json format. You can use the preset function remote.json
:
1function json(log) { 2 log.level = log.level.label; 3 return log; 4}
then the logs will look like this:
1{ 2 "logs": [ 3 { 4 "message": "Info message", 5 "level": "info", 6 "logger": "child", 7 "timestamp": "2017-05-29T12:53:46.000Z", 8 "stacktrace": "" 9 }, 10 { 11 "message": "Error message", 12 "level": "error", 13 "logger": "", 14 "timestamp": "2017-05-29T12:53:46.001Z", 15 "stacktrace": " at http://localhost/test.js:12:5" 16 } 17 ] 18}
You can include any values ​​in the log. For example:
1import log from 'loglevel'; 2import remote from 'loglevel-plugin-remote'; 3 4const getCounter = () => { 5 let count = 1; 6 return () => count++; 7}; 8const counter = getCounter(); 9 10const customPlain = log => `[${counter()}] ${log.message}`; 11 12/* 13const customJSON = log => ({ 14 msg: log.message, 15 count: counter(), 16}); 17*/ 18 19remote.apply(log, { format: customPlain }); 20// remote.apply(log, { format: customJSON }); 21 22log.enableAll(); 23log.info('Message one'); 24log.info('Message two');
customPlain:
[1] Message one
[2] Message two
customJSON:
1{ 2 "logs": [ 3 { 4 "msg": "Message one", 5 "count": 1 6 }, 7 { 8 "msg": "Message two", 9 "count": 2 10 } 11 ] 12}
setToken(token)
This method only has an effect after a successful call to the apply method and is used to assign a new authentication token. If you pass the value undefined
, the sending of logs will be suspended until the next setToken
call, but the logs will still accumulate in the queue.
disable()
This method cancels the effect of the plugin.
1<script src="https://unpkg.com/loglevel/dist/loglevel.min.js"></script> 2<script src="https://unpkg.com/loglevel-plugin-remote@^0.6/dist/loglevel-plugin-remote.min.js"></script> 3 4<script> 5 var logger = log.noConflict(); 6 var sender = remote.noConflict(); 7 sender.apply(logger); 8 logger.warn('message'); 9</script>
1import log from 'loglevel'; 2import remote from 'loglevel-plugin-remote'; 3 4remote.apply(log); 5log.warn('message');
1var log = require('loglevel'); 2var remote = require('loglevel-plugin-remote'); 3 4remote.apply(log); 5log.warn('message');
1define(['loglevel', 'loglevel-plugin-remote'], function(log, remote) { 2 remote.apply(log); 3 log.warn('message'); 4});
Code
1var log = require('loglevel'); 2var remote = require('loglevel-plugin-remote'); 3 4log.enableAll(); 5 6remote.apply(log); 7 8log.info('Log levels:'); 9log.trace('trace message'); 10log.debug('debug message'); 11log.info('info message'); 12log.warn('warn message'); 13log.error('error message');
Output in a log server
[2017-05-29T12:53:46.000Z] INFO: Log levels:
[2017-05-29T12:53:46.000Z] TRACE: trace message
at http://localhost/js/test.js:9:5
[2017-05-29T12:53:46.000Z] DEBUG: debug message
[2017-05-29T12:53:46.000Z] INFO: info message
[2017-05-29T12:53:46.000Z] WARN: warn message
at http://localhost/js/test.js:12:5
[2017-05-29T12:53:46.000Z] ERROR: error message
at http://localhost/js/test.js:13:5
Code
1log.info('String interpolation: %% %t %s', 'one', 'two'); 2log.info('Number interpolation: %d %d %d %d', 16, 1e6, '16', '1e6');
Output in a log server
[2017-05-29T12:53:46.000Z] INFO: String interpolation: % %t one two
[2017-05-29T12:53:46.000Z] INFO: Number interpolation: 16 1000000 16 1000000
Code
1log.info('Object interpolation:'); 2 3function Rectangle(width, height) { 4 this.width = width; 5 this.height = height; 6} 7var object = new Rectangle(10, 10); 8log.info('%s, %d, %o, %j', object, object, object, object, object); 9 10var date = new Date(); 11log.info('date: %o', date); 12 13var error = new Error('My error'); 14log.info('error: %o', error); 15 16var string = 'My string'; 17log.info('string: %o', string); 18 19var number = 123; 20log.info('number: %o', number); 21 22var bool = true; 23log.info('boolean: %o', bool); 24 25var array = [1, 2, 3]; 26log.info('array: %o', array);
Output in a log server
[2017-05-29T12:53:46.000Z] INFO: Object interpolation:
[2017-05-29T12:53:46.000Z] INFO: [object Object], NaN, Rectangle{"height":10,"width":10}, {"height":10,"width":10} [object Object]
[2017-05-29T12:53:46.000Z] INFO: date: Date<"2017-06-04T13:16:01.455Z">
[2017-05-29T12:53:46.000Z] INFO: error: Error{}
[2017-05-29T12:53:46.000Z] INFO: string: String<"My string">
[2017-05-29T12:53:46.000Z] INFO: number: Number<123>
[2017-05-29T12:53:46.000Z] INFO: boolean: Boolean<true>
[2017-05-29T12:53:46.000Z] INFO: array: Array[1,2,3]
Code
1var log = require('loglevel'); 2var remote = require('loglevel-plugin-remote'); 3var mock = require('loglevel-plugin-mock'); 4 5// To clean the loglevel-plugin-mock line in the stack trace: 6// options = { stacktrace: { excess: 1 } } 7remote.apply(log, { stacktrace: { excess: 1 } }); 8mock.apply(log); 9 10var array = [1, 2, 3]; 11log.warn('array: %o', array);
Output in a log server
[2017-05-29T12:53:46.000Z] WARN: array: Array[1,2,3]
at http://localhost/js/test.js:11:5
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 1/12 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
41 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More