@stencila/logga
Advanced tools
Comparing version 1.2.1 to 1.3.0
@@ -0,1 +1,13 @@ | ||
# [1.3.0](https://github.com/stencila/logga/compare/v1.2.1...v1.3.0) (2019-09-03) | ||
### Bug Fixes | ||
* **Emit:** Only attach stack to errors ([9ef7b84](https://github.com/stencila/logga/commit/9ef7b84)) | ||
### Features | ||
* **Throttling:** Allow for throttling of events in handler ([b11633a](https://github.com/stencila/logga/commit/b11633a)) | ||
## [1.2.1](https://github.com/stencila/logga/compare/v1.2.0...v1.2.1) (2019-07-03) | ||
@@ -2,0 +14,0 @@ |
@@ -15,3 +15,3 @@ export declare enum LogLevel { | ||
message: string; | ||
stack: string; | ||
stack?: string; | ||
} | ||
@@ -22,3 +22,3 @@ /** | ||
export interface LogHandler { | ||
(data: LogData): any; | ||
(data: LogData): void; | ||
} | ||
@@ -59,4 +59,13 @@ /** | ||
* @param data The log data to handle | ||
* @param level The maximum log level to print. Defaults to `info` | ||
* @param throttle.signature The log event signature to use for throttling. Defaults to '' (i.e. all events) | ||
* @param throttle.duration The duration for throttling (milliseconds). Defaults to 1000ms | ||
*/ | ||
export declare function defaultHandler(data: LogData): void; | ||
export declare function defaultHandler(data: LogData, options?: { | ||
level?: LogLevel; | ||
throttle?: { | ||
signature?: string; | ||
duration?: number; | ||
}; | ||
}): void; | ||
/** | ||
@@ -63,0 +72,0 @@ * Get a logger for the specific application or package. |
@@ -13,2 +13,9 @@ "use strict"; | ||
}; | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
exports.__esModule = true; | ||
@@ -27,3 +34,4 @@ var LOG_EVENT_NAME = Symbol('stencila:logga'); | ||
* | ||
* If `LogData` does not have a stack attached, one is generated and set on the `LogData`. | ||
* For `LogLevel.error`, if `LogInfo` does not have a `stack`, | ||
* one is generated and set on the `LogData`. | ||
* | ||
@@ -34,21 +42,22 @@ * @param info | ||
function emitLogData(info, tag, level) { | ||
var message; | ||
if (typeof info === 'object') { | ||
var message = ''; | ||
if (typeof info === 'object' && info.message !== undefined) { | ||
message = info.message; | ||
} | ||
else { | ||
else if (typeof info === 'string') { | ||
message = info; | ||
} | ||
var stack; | ||
if (typeof info === 'object' && info.stack) { | ||
stack = info.stack; | ||
var data = { tag: tag, level: level, message: message }; | ||
if (typeof info === 'object' && info.stack !== undefined) { | ||
data.stack = info.stack; | ||
} | ||
else { | ||
else if (level <= LogLevel.error) { | ||
var error = new Error(); | ||
// Remove the first three lines of the stack trace which | ||
// are not useful (see issue #3) | ||
var lines = error.stack.split('\n'); | ||
stack = [lines[0]].concat(lines.slice(3)).join('\n'); | ||
if (error.stack !== undefined) { | ||
// Remove the first three lines of the stack trace which | ||
// are not useful (see issue #3) | ||
var lines = error.stack.split('\n'); | ||
data.stack = __spreadArrays([lines[0]], lines.slice(3)).join('\n'); | ||
} | ||
} | ||
var data = { tag: tag, level: level, message: message, stack: stack }; | ||
// @ts-ignore | ||
@@ -64,3 +73,3 @@ process.emit(LOG_EVENT_NAME, data); | ||
function addHandler(handler) { | ||
handler = handler || defaultHandler; | ||
handler = handler !== undefined ? handler : defaultHandler; | ||
// @ts-ignore | ||
@@ -76,3 +85,3 @@ process.addListener(LOG_EVENT_NAME, handler); | ||
function removeHandler(handler) { | ||
handler = handler || defaultHandler; | ||
handler = handler !== undefined ? handler : defaultHandler; | ||
process.removeListener(LOG_EVENT_NAME, handler); | ||
@@ -100,2 +109,3 @@ } | ||
exports.replaceHandlers = replaceHandlers; | ||
var defaultHandlerHistory = new Map(); | ||
/** | ||
@@ -110,7 +120,32 @@ * Default log data handler. | ||
* @param data The log data to handle | ||
* @param level The maximum log level to print. Defaults to `info` | ||
* @param throttle.signature The log event signature to use for throttling. Defaults to '' (i.e. all events) | ||
* @param throttle.duration The duration for throttling (milliseconds). Defaults to 1000ms | ||
*/ | ||
function defaultHandler(data) { | ||
var entry; | ||
if (process.stderr.isTTY) { | ||
var tag = data.tag, level = data.level, message = data.message, stack = data.stack; | ||
function defaultHandler(data, options) { | ||
var tag = data.tag, level = data.level, message = data.message, stack = data.stack; | ||
// Skip if greater than desired reporting level | ||
var maxLevel = options !== undefined && options.level !== undefined | ||
? options.level | ||
: LogLevel.info; | ||
if (level > maxLevel) | ||
return; | ||
// Skip if within throttling duration for the event signature | ||
var throttle = options !== undefined ? options.throttle : undefined; | ||
if (throttle !== undefined) { | ||
var signature = throttle.signature !== undefined ? throttle.signature : ''; | ||
var eventSignature = signature | ||
.replace(/\${tag}/, tag) | ||
.replace(/\${level}/, level.toString()) | ||
.replace(/\${message}/, message); | ||
var lastTime = defaultHandlerHistory.get(eventSignature); | ||
if (lastTime !== undefined) { | ||
var duration = throttle.duration !== undefined ? throttle.duration : 1000; | ||
if (Date.now() - lastTime < duration) | ||
return; | ||
} | ||
defaultHandlerHistory.set(eventSignature, Date.now()); | ||
} | ||
var entry = ''; | ||
if (process.stderr.isTTY === true) { | ||
var index = level < 0 ? 0 : level > 3 ? 3 : level; | ||
@@ -133,3 +168,3 @@ var label = LogLevel[index].toUpperCase().padEnd(5, ' '); | ||
entry = emoji + " " + colour + label + reset + " " + cyan + tag + reset + " " + message; | ||
if (level === LogLevel.error) | ||
if (stack !== undefined) | ||
entry += '\n ' + stack; | ||
@@ -145,3 +180,3 @@ } | ||
// already enabled e.g. by another package using `logga` | ||
if (!process.listenerCount(LOG_EVENT_NAME)) | ||
if (process.listenerCount(LOG_EVENT_NAME) === 0) | ||
addHandler(defaultHandler); | ||
@@ -156,2 +191,3 @@ /** | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type | ||
function getLogger(tag) { | ||
@@ -158,0 +194,0 @@ return { |
{ | ||
"name": "@stencila/logga", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "Unified logging across related Javascript modules", | ||
@@ -11,4 +11,6 @@ "main": "dist/index.js", | ||
"scripts": { | ||
"test": "jest", | ||
"test:cover": "jest --collectCoverage", | ||
"format": "npx prettier --write './**/*.{md,ts}'", | ||
"lint": "eslint 'index.ts' --fix", | ||
"test": "jest --runInBand", | ||
"test:cover": "jest --runInBand --collectCoverage", | ||
"build": "tsc index.ts --outDir dist --declaration" | ||
@@ -30,7 +32,7 @@ }, | ||
"devDependencies": { | ||
"@stencila/dev-config": "^1.0.6", | ||
"@types/jest": "^24.0.15", | ||
"jest": "^24.8.0", | ||
"ts-jest": "^24.0.2", | ||
"typescript": "^3.5.2" | ||
"@stencila/dev-config": "1.1.3", | ||
"@types/jest": "24.0.18", | ||
"jest": "24.9.0", | ||
"ts-jest": "24.0.2", | ||
"typescript": "3.6.2" | ||
}, | ||
@@ -37,0 +39,0 @@ "jest": { |
@@ -76,9 +76,9 @@ # Logga | ||
addHandler((data: LogData) => { | ||
// Send off to log output function | ||
winstonLogger.log(LogLevel[data.level], data.message); | ||
// or filter on tag | ||
if (data.tag === 'encoda') { | ||
// do something different | ||
} | ||
// Send off to log output function | ||
winstonLogger.log(LogLevel[data.level], data.message) | ||
// or filter on tag | ||
if (data.tag === 'encoda') { | ||
// do something different | ||
} | ||
}) | ||
@@ -85,0 +85,0 @@ ``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31104
280