@stencila/logga
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -9,9 +9,9 @@ export declare enum LogLevel { | ||
message?: string; | ||
stackTrace?: string; | ||
stack?: string; | ||
} | ||
export interface LogData { | ||
appName: string; | ||
tag: string; | ||
level: LogLevel; | ||
message: string; | ||
stackTrace: string; | ||
stack: string; | ||
} | ||
@@ -37,5 +37,5 @@ /** | ||
* | ||
* @param appName The unique application or package name | ||
* @param tag The unique application or package name | ||
*/ | ||
export declare function getLogger(appName: string): { | ||
export declare function getLogger(tag: string): { | ||
error(message: string | LogInfo): void; | ||
@@ -42,0 +42,0 @@ warn(message: string | LogInfo): void; |
@@ -15,3 +15,3 @@ "use strict"; | ||
* | ||
* If `LogData` does not have a stackTrace attached, one is generated and set on the `LogData`. | ||
* If `LogData` does not have a stack attached, one is generated and set on the `LogData`. | ||
* | ||
@@ -21,3 +21,3 @@ * @param info | ||
*/ | ||
function emitLogData(info, appName, level) { | ||
function emitLogData(info, tag, level) { | ||
var message; | ||
@@ -30,11 +30,14 @@ if (typeof info === 'object') { | ||
} | ||
var stackTrace; | ||
if (typeof info === 'object' && info.stackTrace) { | ||
stackTrace = info.stackTrace; | ||
var stack; | ||
if (typeof info === 'object' && info.stack) { | ||
stack = info.stack; | ||
} | ||
else { | ||
var error = new Error(); | ||
stackTrace = error.stack; | ||
// 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'); | ||
} | ||
var data = { appName: appName, level: level, message: message, stackTrace: stackTrace }; | ||
var data = { tag: tag, level: level, message: message, stack: stack }; | ||
// @ts-ignore | ||
@@ -53,3 +56,3 @@ process.emit(LOG_EVENT_NAME, data); | ||
function (data) { | ||
console.error(data.appName + " - [" + LogLevel[data.level].toUpperCase() + "] - " + data.message); | ||
console.error(data.tag + " - [" + LogLevel[data.level].toUpperCase() + "] - " + data.message); | ||
}; | ||
@@ -66,17 +69,17 @@ // @ts-ignore | ||
* | ||
* @param appName The unique application or package name | ||
* @param tag The unique application or package name | ||
*/ | ||
function getLogger(appName) { | ||
function getLogger(tag) { | ||
return { | ||
error: function (message) { | ||
emitLogData(message, appName, LogLevel.error); | ||
emitLogData(message, tag, LogLevel.error); | ||
}, | ||
warn: function (message) { | ||
emitLogData(message, appName, LogLevel.warn); | ||
emitLogData(message, tag, LogLevel.warn); | ||
}, | ||
info: function (message) { | ||
emitLogData(message, appName, LogLevel.info); | ||
emitLogData(message, tag, LogLevel.info); | ||
}, | ||
debug: function (message) { | ||
emitLogData(message, appName, LogLevel.debug); | ||
emitLogData(message, tag, LogLevel.debug); | ||
} | ||
@@ -83,0 +86,0 @@ }; |
{ | ||
"name": "@stencila/logga", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Unified logging across related Javascript modules", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"/dist" | ||
], | ||
"scripts": { | ||
@@ -29,3 +32,3 @@ "test": "jest", | ||
"@semantic-release/github": "^5.2.10", | ||
"@semantic-release/npm": "^5.1.7", | ||
"@semantic-release/npm": "^5.1.9", | ||
"@semantic-release/release-notes-generator": "^7.1.4", | ||
@@ -32,0 +35,0 @@ "@types/jest": "^24.0.13", |
# Logga | ||
🌲 Unified logging across related Javascript modules | ||
🌲 Emit log events from anywhere. Consistently. | ||
@@ -8,2 +8,44 @@ [![Build status](https://travis-ci.org/stencila/logga.svg?branch=master)](https://travis-ci.org/stencila/logga) | ||
[![NPM](https://img.shields.io/npm/v/@stencila/logga.svg?style=flat)](https://www.npmjs.com/package/@stencila/logga) | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/stencila/logga.svg)](https://greenkeeper.io/) | ||
[![Chat](https://badges.gitter.im/stencila/stencila.svg)](https://gitter.im/stencila/stencila) | ||
## Why? | ||
We wanted to have a unified, consistent mechanism for emitting log event data across Javascript/Typescript projects. We wanted to decouple log event emission from log event consumption. We wanted something lightweight. | ||
For example, `encoda` and `dockta` are two Typescript projects that use `logga` which we combine in the `stencila` command line tool. In the future, we'll also combine them into the `stencila` desktop Electron-based application. We want users of `encoda` and `dockta` to be able to use them as standalone tools, at the command line, and get feedback at the command line. But we also want to be able to handle log events from these packages in apps that integrate them and which may use a fancier CLI display (e.g. [Ink](https://github.com/vadimdemedes/ink)) or a HTML UI display (e.g. in Electron). | ||
## Approach | ||
The approach used in `logga` is to use `process` as a bus for log events. It's a simple approach, described in [this gist](https://gist.github.com/constantology/5f04d5782c1cc019722f), that combines emitting events using `process.emit()` and registering an event handler with `process.on()`. | ||
## Install | ||
```bash | ||
npm install --save @stencila/logga | ||
``` | ||
## Usage | ||
Create a new logger by calling `getLogger` with a unique tag to identify your app and/or module. Register a handler by calling `addHandler` with a handling function (defaults to printing to the `process.stderr`). Then emit log events using the `debug`, `info`, `warn` or `error` function (you can pass them a message string or a `LogInfo` object). | ||
```ts | ||
import { getLogger, addHandler } from '@stencila/logga' | ||
const logger = getLogger('app:module') | ||
addHandler() | ||
function doSomething() { | ||
logger.debug('A debug message') | ||
try { | ||
// ... | ||
} catch (error) { | ||
const { message, stack } = error | ||
logger.error({ | ||
message: 'Woaaah something bad happened! ' + message, | ||
stack | ||
}) | ||
} | ||
} | ||
``` |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
51
0
19178
5
126