Skip to content

Commit

Permalink
chore(all): prepare release 1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
EisenbergEffect committed Dec 20, 2017
1 parent 9d6464e commit db55202
Show file tree
Hide file tree
Showing 11 changed files with 611 additions and 55 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aurelia-logging",
"version": "1.3.1",
"version": "1.4.0",
"description": "A minimal but effective logging mechanism with support for log levels and pluggable log appenders.",
"keywords": [
"aurelia",
Expand Down
99 changes: 91 additions & 8 deletions dist/amd/aurelia-logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,34 @@ define(['exports'], function (exports) {
exports.getLogger = getLogger;
exports.addAppender = addAppender;
exports.removeAppender = removeAppender;
exports.getAppenders = getAppenders;
exports.clearAppenders = clearAppenders;
exports.addCustomLevel = addCustomLevel;
exports.removeCustomLevel = removeCustomLevel;
exports.setLevel = setLevel;
exports.getLevel = getLevel;



var logLevel = exports.logLevel = {
none: 0,
error: 1,
warn: 2,
info: 3,
debug: 4
error: 10,
warn: 20,
info: 30,
debug: 40
};

var loggers = {};
var appenders = [];
var globalDefaultLevel = logLevel.none;

var standardLevels = ['none', 'error', 'warn', 'info', 'debug'];
function isStandardLevel(level) {
return standardLevels.filter(function (l) {
return l === level;
}).length > 0;
}

function appendArgs() {
return [this].concat(Array.prototype.slice.call(arguments));
}
Expand All @@ -45,12 +56,44 @@ define(['exports'], function (exports) {
};
}

function logFactoryCustom(level) {
var threshold = logLevel[level];
return function () {
if (this.level < threshold) {
return;
}

var args = appendArgs.apply(this, arguments);
var i = appenders.length;
while (i--) {
var appender = appenders[i];
if (appender[level] !== undefined) {
appender[level].apply(appender, args);
}
}
};
}

function connectLoggers() {
var proto = Logger.prototype;
proto.debug = logFactory('debug');
proto.info = logFactory('info');
proto.warn = logFactory('warn');
proto.error = logFactory('error');
for (var _level in logLevel) {
if (isStandardLevel(_level)) {
if (_level !== 'none') {
proto[_level] = logFactory(_level);
}
} else {
proto[_level] = logFactoryCustom(_level);
}
}
}

function disconnectLoggers() {
var proto = Logger.prototype;
for (var _level2 in logLevel) {
if (_level2 !== 'none') {
proto[_level2] = function () {};
}
}
}

function getLogger(id) {
Expand All @@ -69,6 +112,46 @@ define(['exports'], function (exports) {
});
}

function getAppenders() {
return [].concat(appenders);
}

function clearAppenders() {
appenders = [];
disconnectLoggers();
}

function addCustomLevel(name, value) {
if (logLevel[name] !== undefined) {
throw Error('Log level "' + name + '" already exists.');
}

if (isNaN(value)) {
throw Error('Value must be a number.');
}

logLevel[name] = value;

if (appenders.length > 0) {
connectLoggers();
} else {
Logger.prototype[name] = function () {};
}
}

function removeCustomLevel(name) {
if (logLevel[name] === undefined) {
return;
}

if (isStandardLevel(name)) {
throw Error('Built-in log level "' + name + '" cannot be removed.');
}

delete logLevel[name];
delete Logger.prototype[name];
}

function setLevel(level) {
globalDefaultLevel = level;
for (var key in loggers) {
Expand Down
32 changes: 31 additions & 1 deletion dist/aurelia-logging.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export declare interface LogLevel {
* Log all messages.
*/
debug: number;

/**
* Additional log levels defined at runtime.
*/
[level: string]: number;
}

/**
Expand Down Expand Up @@ -97,11 +102,36 @@ export declare function getLogger(id: string): Logger;
export declare function addAppender(appender: Appender): void;

/**
* Removes an appender
* Removes an appender.
* @param appender An appender that has been added previously.
*/
export declare function removeAppender(appender: Appender): void;

/**
* Gets an array of all appenders.
*/
export declare function getAppenders(): any;

/**
* Removes all appenders.
*/
export declare function clearAppenders(): void;

/**
* Adds a custom log level that will be added as an additional method to Logger.
* Logger will call the corresponding method on any appenders that support it.
*
* @param name The name for the new log level.
* @param value The numeric severity value for the level (higher is more severe).
*/
export declare function addCustomLevel(name: string, value: number): void;

/**
* Removes a custom log level.
* @param name The name of a custom log level that has been added previously.
*/
export declare function removeCustomLevel(name: string): void;

/**
* Sets the level of logging for ALL the application loggers.
*
Expand Down
127 changes: 117 additions & 10 deletions dist/aurelia-logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,34 @@ interface LogLevel {
/**
* Log all messages.
*/
debug: number
debug: number,

/**
* Additional log levels defined at runtime.
*/
[level: string]: number
}

/**
* Specifies the available logging levels.
*/
export const logLevel: LogLevel = {
none: 0,
error: 1,
warn: 2,
info: 3,
debug: 4
error: 10,
warn: 20,
info: 30,
debug: 40
};

let loggers = {};
let appenders = [];
let globalDefaultLevel = logLevel.none;

const standardLevels = ['none', 'error', 'warn', 'info', 'debug'];
function isStandardLevel(level: string) {
return standardLevels.filter(l => l === level).length > 0;
}

function appendArgs() {
return [this, ...arguments];
}
Expand All @@ -62,12 +72,49 @@ function logFactory(level) {
};
}

function logFactoryCustom(level) {
//This function is the same as logFactory() except that it checks that the method
//is defined on the appender.
const threshold = logLevel[level];
return function() {
// In this function, this === logger
if (this.level < threshold) {
return;
}
// We don't want to disable optimizations (such as inlining) in this function
// so we do the arguments manipulation in another function.
// Note that Function#apply is very special for V8.
const args = appendArgs.apply(this, arguments);
let i = appenders.length;
while (i--) {
const appender = appenders[i];
if (appender[level] !== undefined) {
appender[level](...args);
}
}
};
}

function connectLoggers() {
let proto = Logger.prototype;
proto.debug = logFactory('debug');
proto.info = logFactory('info');
proto.warn = logFactory('warn');
proto.error = logFactory('error');
for (let level in logLevel) {
if (isStandardLevel(level)) {
if (level !== 'none') {
proto[level] = logFactory(level);
}
} else {
proto[level] = logFactoryCustom(level);
}
}
}

function disconnectLoggers() {
let proto = Logger.prototype;
for (let level in logLevel) {
if (level !== 'none') {
proto[level] = function() { };
}
}
}

/**
Expand All @@ -84,6 +131,7 @@ export function getLogger(id: string): Logger {
* Implemented by classes which wish to append log data to a target data store.
*/
interface Appender {

/**
* Appends a debug log.
*
Expand Down Expand Up @@ -129,13 +177,72 @@ export function addAppender(appender: Appender): void {
}

/**
* Removes an appender
* Removes an appender.
* @param appender An appender that has been added previously.
*/
export function removeAppender(appender: Appender): void {
appenders = appenders.filter(a => a !== appender);
}

/**
* Gets an array of all appenders.
*/
export function getAppenders() {
return [...appenders];
}

/**
* Removes all appenders.
*/
export function clearAppenders(): void {
appenders = [];
disconnectLoggers();
}

/**
* Adds a custom log level that will be added as an additional method to Logger.
* Logger will call the corresponding method on any appenders that support it.
*
* @param name The name for the new log level.
* @param value The numeric severity value for the level (higher is more severe).
*/
export function addCustomLevel(name: string, value: number): void {
if (logLevel[name] !== undefined) {
throw Error(`Log level "${name}" already exists.`);
}

if (isNaN(value)) {
throw Error('Value must be a number.');
}

logLevel[name] = value;

if (appenders.length > 0) {
//Reinitialize the Logger prototype with the new method.
connectLoggers();
} else {
//Add the custom level as a noop by default.
Logger.prototype[name] = function() { };
}
}

/**
* Removes a custom log level.
* @param name The name of a custom log level that has been added previously.
*/
export function removeCustomLevel(name: string): void {
if (logLevel[name] === undefined) {
return;
}

if (isStandardLevel(name)) {
throw Error(`Built-in log level "${name}" cannot be removed.`);
}

delete logLevel[name];
delete Logger.prototype[name];
}

/**
* Sets the level of logging for ALL the application loggers.
*
Expand Down
Loading

0 comments on commit db55202

Please sign in to comment.