New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

abstract-logger

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

abstract-logger - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

lib/log-levels.js

113

lib/index.js
(function() {
var AbstractLogger, format, isObject, isString;
var AbstractLogger, defineProperty, format, isArray, isNumber, isObject, isString, logLevels,
__slice = [].slice;

@@ -8,4 +9,12 @@ isObject = require('util-ex/lib/is/type/object');

isNumber = require('util-ex/lib/is/type/number');
isArray = require('util-ex/lib/is/type/array');
format = require('util-ex/lib/format');
defineProperty = require('util-ex/lib/defineProperty');
logLevels = require('./log-levels');
module.exports = AbstractLogger = (function() {

@@ -24,2 +33,11 @@ function AbstractLogger(aName, aOptions) {

}
if (aOptions.levels != null) {
this.levels = aOptions.levels;
}
if (aOptions.level != null) {
this.level = aOptions.level;
}
if (aOptions.enabled === false) {
this.enabled = aOptions.enabled;
}
}

@@ -30,3 +48,20 @@ }

AbstractLogger.prototype.levels = logLevels;
defineProperty(AbstractLogger.prototype, '_level', logLevels.ERROR);
defineProperty(AbstractLogger.prototype, 'level', void 0, {
get: function() {
return this.levelId2Str(this._level);
},
set: function(value) {
if (isString(value)) {
this._level = this.levelStr2Id(value);
} else if (isNumber(value)) {
this._level = value;
}
}
});
/* !pragma coverage-skip-next */

@@ -38,6 +73,28 @@

AbstractLogger.prototype.formatter = function(msg, ctx) {
var end, start, v;
AbstractLogger.prototype.levelId2Str = function(aId) {
var i, result, vLevelStr, _ref;
_ref = this.levels;
for (vLevelStr in _ref) {
i = _ref[vLevelStr];
if (i === aId) {
result = vLevelStr;
break;
}
}
return result;
};
AbstractLogger.prototype.levelStr2Id = function(aStr) {
return this.levels[aStr.toUpperCase()];
};
AbstractLogger.prototype.formatter = function() {
var aContext, args, end, msg, start, v;
aContext = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
msg = aContext.message;
end = -1;
start = msg.indexOf('${');
if (this.name != null) {
aContext.name = this.name;
}
while (start !== -1 && end < msg.length) {

@@ -49,3 +106,3 @@ end = msg.indexOf('}', start);

v = msg.slice(start + 2, end).trim();
v = ctx[v];
v = aContext[v];
if (v != null) {

@@ -58,8 +115,44 @@ v = v.toString();

}
if (args.length) {
args.unshift(msg);
msg = format.apply(null, args);
}
return msg;
};
AbstractLogger.prototype.log = function(message, context) {
if (isString(message) && isObject(context) && !Array.isArray(context)) {
this.writeln(this.formatter(message, context));
AbstractLogger.prototype.inLevel = function(aLevel) {
var result;
if (!isNumber(aLevel)) {
aLevel = this.levels[aLevel];
}
result = isNumber(aLevel) && aLevel <= this._level;
return result;
};
AbstractLogger.prototype.inLevelContext = function(aContext) {
var result, vLevel;
vLevel = aContext.level;
result = vLevel != null;
if (result) {
result = this.inLevel(vLevel);
if (result && isNumber(vLevel)) {
aContext.level = this.levelId2Str(vLevel);
}
}
return result;
};
AbstractLogger.prototype.log = function() {
var aContext, arg2, args;
aContext = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
arg2 = args[0];
if (isString(aContext) && isObject(arg2) && !isArray(arg2)) {
if ((arg2.level == null) || this.inLevelContext(arg2)) {
arg2.message = aContext;
this.writeln(this.formatter.apply(this, args));
}
} else if (isObject(aContext) && isString(aContext.message)) {
if (this.inLevelContext(aContext)) {
this.writeln(this.formatter.apply(this, arguments));
}
} else {

@@ -73,4 +166,6 @@ this.writeln.apply(this, arguments);

var vStr;
vStr = arguments.length ? format.apply(null, arguments) : this.NEWLINE;
this._write(vStr);
if (this.enabled !== false) {
vStr = arguments.length ? format.apply(null, arguments) : this.NEWLINE;
this._write(vStr);
}
return this;

@@ -77,0 +172,0 @@ };

2

package.json
{
"name": "abstract-logger",
"description": "It's an abstract logger.",
"version": "0.1.0",
"version": "0.2.0",
"homepage": "https://github.com/snowyu/abstract-logger.js",

@@ -6,0 +6,0 @@ "repository": {

@@ -25,3 +25,18 @@ ## abstract-logger [![npm](https://img.shields.io/npm/v/abstract-logger.svg)](https://npmjs.org/package/abstract-logger)

_write: (str)->console.error str
```
```js
var logger = new MyLogger('test')
logger.log({
message: '${name} - ${level}: here is a %s logging "${title}"!'
, title: 'Today news'
, level: log.levels.ERROR
}, 'pretty')
//or:
logger.log('${name} - ${level}: here is a %s logging "${title}"!', {
title: 'Today news'
, level: log.levels.ERROR
}, 'pretty')
//result: 'test - ERROR: here is a pretty logging "Today news"'
```

@@ -32,3 +47,14 @@

* Methods:
* `log(message, context)`:eg, log('hi ${user}', {user:'Mikey'})
* `log(message[, context], args...)`:eg, log('hi ${user}', {user:'Mikey'})
* message *(String)*: The message to show up
* context *(Object)*: The optional context to escape the message against and pass the options to the log:
* level *(Number|String)*: the logLevel. it will be translated to the string if it's a number
* label *(String)*: the status label.
* name *(String)*: the logger name if exists.
* `log(context, args...)`:eg, log({message:'${name} - ${level}: hi ${user}', level:'info', user:'Mikey'})
* The context to escape the message against and pass the options to the log:
* message *(String)*: The message to show up
* level *(Number|String)*: the logLevel. it will be translated to the string if it's a number
* label *(String)*: the status label.
* name *(String)*: the logger name if exists.
* `write(...)`: write a new-line if no arguments.

@@ -41,15 +67,31 @@ * `writeln(...)`: Same as `log.write()` but automatically appends a `\n` at the end

+ LogLevel
* 0 EMERGENCY system is unusable
* 1 ALERT action must be taken immediately
* 2 CRITICAL the system is in critical condition
* 3 ERROR error condition
* 4 WARNING warning condition
* 5 NOTICE a normal but significant condition
* 6 INFO a purely informational message
* 7 DEBUG messages to debug an application
+ Stream with transport (like https://github.com/winstonjs/winston)
## Changes
### v0.2
+ `enabled` *(Boolean)*: enable/disable the logger. default to true.
+ `levels` *(LogLevels)*: customizable logging levels
+ The default LogLevels:
* SILENT:-1
* EMERGENCY:0 system is unusable
* ALERT:1 action must be taken immediately
* CRITICAL:2 the system is in critical condition
* ERROR:3 error condition
* WARNING:4 warning condition
* NOTICE:5 a normal but significant condition
* INFO:6 a purely informational message
* DEBUG:7 messages to debug an application
+ `level`: use the property to get/set the log level.
* defaults to levels.ERROR.
* set `'SILENT'` to mute the loglevel msg, it will still print it out if the msg without loglevel.
* setter *(Nubmer|String)*: set the logging level via number or string.
* getter *(String)*: get the logging level string, or get the level number via `_level` property.
* `log()`
+ level, name options to context.
+ log(context, args...)
## License
MIT

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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