Socket
Socket
Sign inDemoInstall

loglevel-plugin-prefix

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

loglevel-plugin-prefix - npm Package Compare versions

Comparing version 0.6.0 to 0.7.0

.travis.yml

2

dist/loglevel-plugin-prefix.min.js

@@ -1,1 +0,1 @@

!function(e,t){"function"==typeof define&&define.amd?define(t):"object"==typeof module&&module.exports?module.exports=t():e.prefix=t(e)}(this,function(e){"use strict";var t,r={template:"[%t] %l:",timestampFormatter:function(e){return e.toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/,"$1")},levelFormatter:function(e){return e.toUpperCase()},nameFormatter:function(e){return e||"root"}},n={},o={apply:function(e,t){if(!e||!e.setLevel)throw new TypeError("Argument is not a logger");var o=!!e.getLogger,a=e.methodFactory,i=e.name||"",l=n[i]||n[""]||r;return n[i]||(e.methodFactory=function(e,t,r){var i=a(e,t,r),l=n[r||""],f=-1!==l.template.indexOf("%t"),p=-1!==l.template.indexOf("%l"),m=-1!==l.template.indexOf("%n");return function(){for(var t=l.template,a=arguments.length,u=Array(a),c=0;c<a;c++)u[c]=arguments[c];o&&n[r]||(f&&(t=t.replace(/%t/,l.timestampFormatter(new Date))),p&&(t=t.replace(/%l/,l.levelFormatter(e))),m&&(t=t.replace(/%n/,l.nameFormatter(r))),u.length&&"string"==typeof u[0]?u[0]=t+" "+u[0]:u.unshift(t)),i.apply(void 0,u)}}),n[i]=function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(t in arguments[r])Object.prototype.hasOwnProperty.call(arguments[r],t)&&(e[t]=arguments[r][t]);return e}({},l,t),e.setLevel(e.getLevel()),e}};return e&&(t=e.prefix,o.noConflict=function(){return e.prefix===o&&(e.prefix=t),o}),o});
!function(e,t){"function"==typeof define&&define.amd?define(t):"object"==typeof module&&module.exports?module.exports=t():e.prefix=t(e)}(this,function(e){"use strict";var t,r={template:"[%t] %l:",timestampFormatter:function(e){return e.toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/,"$1")},levelFormatter:function(e){return e.toUpperCase()},nameFormatter:function(e){return e||"root"}},n={},o={apply:function(e,t){if(!e||!e.setLevel)throw new TypeError("Argument is not a logger");var o=!!e.getLogger,a=e.methodFactory,f=e.name||"",i=n[f]||n[""]||r;return n[f]||(e.methodFactory=function(e,t,r){var f=a(e,t,r),i=n[r||""],l=-1!==i.template.indexOf("%t"),m=-1!==i.template.indexOf("%l"),p=-1!==i.template.indexOf("%n");return function(){for(var t="",a=arguments.length,u=Array(a),c=0;c<a;c++)u[c]=arguments[c];o&&n[r]||(i.format?t+=i.format(e,r):(t+=i.template,l&&(t=t.replace(/%t/,i.timestampFormatter(new Date))),m&&(t=t.replace(/%l/,i.levelFormatter(e))),p&&(t=t.replace(/%n/,i.nameFormatter(r)))),u.length&&"string"==typeof u[0]?u[0]=t+" "+u[0]:u.unshift(t)),f.apply(void 0,u)}}),(t=t||{}).format="function"==typeof t.format?t.format:void 0,n[f]=function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(t in arguments[r])Object.prototype.hasOwnProperty.call(arguments[r],t)&&(e[t]=arguments[r][t]);return e}({},i,t),e.setLevel(e.getLevel()),e}};return e&&(t=e.prefix,o.noConflict=function(){return e.prefix===o&&(e.prefix=t),o}),o});

@@ -12,3 +12,3 @@ (function (root, factory) {

var merge = function merge(target) {
var merge = function (target) {
var i = 1;

@@ -40,5 +40,5 @@ var length = arguments.length;

var loggers = {};
var configs = {};
var apply = function apply(logger, config) {
var apply = function (logger, config) {
if (!logger || !logger.setLevel) {

@@ -52,3 +52,3 @@ throw new TypeError('Argument is not a logger');

var name = logger.name || '';
var parent = loggers[name] || loggers[''] || defaults;
var parent = configs[name] || configs[''] || defaults;
/* eslint-enable vars-on-top */

@@ -58,3 +58,3 @@

var originalMethod = originalFactory(methodName, logLevel, loggerName);
var options = loggers[loggerName || ''];
var options = configs[loggerName || ''];

@@ -66,3 +66,3 @@ var hasTimestamp = options.template.indexOf('%t') !== -1;

return function () {
var content = options.template;
var content = '';

@@ -76,10 +76,17 @@ var length = arguments.length;

if (!(isRoot && loggers[loggerName])) {
if (hasTimestamp) content = content.replace(/%t/, options.timestampFormatter(new Date()));
if (hasLevel) content = content.replace(/%l/, options.levelFormatter(methodName));
if (hasName) content = content.replace(/%n/, options.nameFormatter(loggerName));
if (!(isRoot && configs[loggerName])) {
if (options.format) {
content += options.format(methodName, loggerName);
} else {
content += options.template;
if (hasTimestamp) {
content = content.replace(/%t/, options.timestampFormatter(new Date()));
}
if (hasLevel) content = content.replace(/%l/, options.levelFormatter(methodName));
if (hasName) content = content.replace(/%n/, options.nameFormatter(loggerName));
}
if (args.length && typeof args[0] === 'string') {
// concat prefix with first argument to support string substitutions
args[0] = '' + content + ' ' + args[0];
args[0] = content + ' ' + args[0];
} else {

@@ -94,7 +101,10 @@ args.unshift(content);

if (!loggers[name]) {
if (!configs[name]) {
logger.methodFactory = methodFactory;
}
loggers[name] = merge({}, parent, config);
config = config || {};
config.format = typeof config.format === 'function' ? config.format : undefined;
configs[name] = merge({}, parent, config);
logger.setLevel(logger.getLevel());

@@ -109,2 +119,3 @@ return logger;

var save;
if (root) {

@@ -111,0 +122,0 @@ save = root.prefix;

@@ -25,3 +25,3 @@ {

"license": "MIT",
"version": "0.6.0",
"version": "0.7.0",
"main": "./lib/loglevel-plugin-prefix",

@@ -39,2 +39,3 @@ "scripts": {

"eslint-plugin-chai-friendly": "^0.3.6",
"eslint-plugin-import": "^2.8.0",
"loglevel": "^1.6.0",

@@ -41,0 +42,0 @@ "loglevel-plugin-mock": "^0.1.0",

# loglevel-plugin-prefix
Plugin for [loglevel](https://github.com/pimterry/loglevel) message prefixing
Plugin for [loglevel](https://github.com/pimterry/loglevel) message prefixing.
[![NPM version](https://img.shields.io/npm/v/loglevel-plugin-prefix.svg?style=flat-square)](https://www.npmjs.com/package/loglevel-plugin-prefix)[![Build Status](https://img.shields.io/travis/kutuluk/loglevel-plugin-prefix/master.svg?style=flat-square)](https://travis-ci.org/kutuluk/loglevel-plugin-prefix)
## Installation
```sh
npm install loglevel-plugin-prefix --save
npm i loglevel-plugin-prefix --save
```

@@ -12,9 +15,9 @@

```javascript
apply(log[, options]);
```
**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(logger, options)```
This method applies the plugin to the logger.
**log** - loglevel logger
**logger** - loglevel logger

@@ -48,8 +51,20 @@ **options** - configuration object

## Base usage
Alternatively, you can use **format** option. This is a function with two arguments (level and logger), which should return a prefix string. For example,
```javascript
function simpleFormat(level, logger) {
return level + " (" + logger + "):";
}
```
If the format function is present, the other options are ignored.
## Usage
### Browser directly
```html
<script src="https://unpkg.com/loglevel/dist/loglevel.min.js"></script>
<script src="https://unpkg.com/loglevel-plugin-prefix/dist/loglevel-plugin-prefix.min.js"></script>
<script src="https://unpkg.com/loglevel-plugin-prefix@^0.7/dist/loglevel-plugin-prefix.min.js"></script>

@@ -56,0 +71,0 @@ <script>

@@ -1,10 +0,31 @@

const expect = require("chai").expect;
const loglevel = require("loglevel");
const other = require("loglevel-plugin-mock");
const sinon = require("sinon");
const prefix = require("../lib/loglevel-plugin-prefix");
const expect = require('chai').expect;
const loglevel = require('loglevel');
const other = require('loglevel-plugin-mock');
const sinon = require('sinon');
const prefix = require('../lib/loglevel-plugin-prefix');
const spy = sinon.spy();
describe("", () => {
loglevel.enableAll();
describe('API', () => {
it('Methods', () => {
expect(prefix).to.have.property('apply').with.be.a('function');
expect(prefix).not.to.have.property('noConflict');
});
it('Empty arguments', () => {
expect(prefix.apply).to.throw(TypeError, 'Argument is not a logger');
});
it('Incorrect argument', () => {
expect(() => prefix.apply('logger')).to.throw(TypeError, 'Argument is not a logger');
});
});
describe('Prefix', () => {
other.apply(loglevel, { method: spy });
const child = loglevel.getLogger('child');
child.enableAll();
beforeEach(() => {

@@ -14,71 +35,59 @@ spy.reset();

describe("API", () => {
it("Methods", () => {
expect(prefix).to.have.property("apply").with.be.a("function");
expect(prefix).not.to.have.property("noConflict");
});
it('Applying', () => {
expect(() => prefix.apply(loglevel)).to.not.throw();
});
it("Empty arguments", () => {
expect(prefix.apply).to.throw(TypeError, "Argument is not a logger");
});
it('Reapplying', () => {
expect(() => prefix.apply(loglevel)).to.not.throw();
});
it("Incorrect argument", () => {
expect(() => prefix.apply("logger")).to.throw(TypeError, "Argument is not a logger");
it('Format', () => {
prefix.apply(loglevel, {
format: (level, logger) => `${level} (${logger}):`
});
it("Applying", () => {
expect(() => prefix.apply(loglevel)).to.not.throw();
});
loglevel.info('test');
it("Reapplying", () => {
expect(() => prefix.apply(loglevel)).to.not.throw();
});
expect(spy.calledWith('info (undefined): test')).to.be.true;
});
describe("Prefix", () => {
other.apply(loglevel, { method: spy });
const child = loglevel.getLogger("child");
child.enableAll();
it('All methods of the previous plugin should be called', () => {
prefix.apply(loglevel);
it("All methods of the previous plugin should be called", () => {
prefix.apply(loglevel);
loglevel.trace();
loglevel.debug();
loglevel.info();
loglevel.warn();
loglevel.error();
expect(spy.callCount).to.equal(5);
});
loglevel.enableAll();
loglevel.trace();
loglevel.debug();
loglevel.info();
loglevel.warn();
loglevel.error();
expect(spy.callCount).to.equal(5);
});
it('Child logger', () => {
prefix.apply(child, { template: '%l (%n):' });
child.info('test');
expect(spy.calledWith('INFO (child): test')).to.be.true;
});
it("Child logger", () => {
prefix.apply(child, { template: "%l (%n):" });
child.info("test");
expect(spy.calledWith("INFO (child): test")).to.be.true;
it('Child reapplyng', () => {
prefix.apply(child, {
levelFormatter(level) {
return level;
}
});
child.info('test');
expect(spy.calledWith('info (child): test')).to.be.true;
});
it("Child reapplyng", () => {
prefix.apply(child, {
levelFormatter: function(level) {
return level;
}
});
child.info("test");
expect(spy.calledWith("info (child): test")).to.be.true;
});
it('Root reapplyng', () => {
prefix.apply(loglevel, { template: '%l (%n):' });
loglevel.info('test');
expect(spy.calledWith('INFO (root): test')).to.be.true;
});
it("Root reapplyng", () => {
prefix.apply(loglevel, { template: "%l (%n):" });
loglevel.info("test");
expect(spy.calledWith("INFO (root): test")).to.be.true;
});
it('The prefix must be combined with the first argument, if it is a string', () => {
prefix.apply(loglevel, { template: '%l:' });
it("The prefix must be combined with the first argument, if it is a string", () => {
prefix.apply(loglevel, { template: "%l:" });
loglevel.warn("foo %s", "bar");
expect(spy.calledWith("WARN: foo %s", "bar")).to.be.true;
});
loglevel.warn('foo %s', 'bar');
expect(spy.calledWith('WARN: foo %s', 'bar')).to.be.true;
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc