@timberio/node
Advanced tools
Comparing version 0.21.0 to 0.22.0
@@ -1,5 +0,27 @@ | ||
import { ITimberOptions } from "@timberio/types"; | ||
/// <reference types="node" /> | ||
import { Duplex, Writable } from "stream"; | ||
import { ITimberLog, ITimberOptions, LogLevel } from "@timberio/types"; | ||
import { Base } from "@timberio/core"; | ||
export declare class Node extends Base { | ||
/** | ||
* Readable/Duplex stream where JSON stringified logs of type `ITimberLog` | ||
* will be pushed after syncing | ||
*/ | ||
private _writeStream?; | ||
constructor(apiKey: string, options?: Partial<ITimberOptions>); | ||
/** | ||
* Override `Base` log to enable Node.js streaming | ||
* | ||
* @param message: string - Log message | ||
* @param level (LogLevel) - Level to log at (debug|info|warn|error) | ||
* @param log: (Partial<ITimberLog>) - Initial log (optional) | ||
* @returns Promise<ITimberLog> after syncing | ||
*/ | ||
log(message: string, level?: LogLevel, log?: Partial<ITimberLog>): Promise<ITimberLog>; | ||
/** | ||
* Pipe JSON stringified `ITimberLog` to a stream after syncing | ||
* | ||
* @param stream - Writable|Duplex stream | ||
*/ | ||
pipe(stream: Writable | Duplex): Writable | Duplex; | ||
} |
@@ -43,4 +43,31 @@ "use strict"; | ||
} | ||
/** | ||
* Override `Base` log to enable Node.js streaming | ||
* | ||
* @param message: string - Log message | ||
* @param level (LogLevel) - Level to log at (debug|info|warn|error) | ||
* @param log: (Partial<ITimberLog>) - Initial log (optional) | ||
* @returns Promise<ITimberLog> after syncing | ||
*/ | ||
async log(message, level, log) { | ||
// Process/sync the log, per `Base` logic | ||
const processedLog = await super.log(message, level, log); | ||
// Push the processed log to the stream, for piping | ||
if (this._writeStream) { | ||
this._writeStream.write(JSON.stringify(processedLog) + "\n"); | ||
} | ||
// Return the transformed log | ||
return processedLog; | ||
} | ||
/** | ||
* Pipe JSON stringified `ITimberLog` to a stream after syncing | ||
* | ||
* @param stream - Writable|Duplex stream | ||
*/ | ||
pipe(stream) { | ||
this._writeStream = stream; | ||
return stream; | ||
} | ||
} | ||
exports.Node = Node; | ||
//# sourceMappingURL=node.js.map |
"use strict"; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -6,2 +13,6 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const os = __importStar(require("os")); | ||
const path = __importStar(require("path")); | ||
const fs = __importStar(require("fs")); | ||
const stream_1 = require("stream"); | ||
const nock_1 = __importDefault(require("nock")); | ||
@@ -28,3 +39,3 @@ const types_1 = require("@timberio/types"); | ||
}); | ||
it("should echo log if timber sends 20x status code", async (done) => { | ||
it("should echo log if timber sends 20x status code", async () => { | ||
nock_1.default("https://logs.timber.io") | ||
@@ -38,5 +49,4 @@ .post("/frames") | ||
expect(echoedLog.message).toEqual(expectedLog.message); | ||
done(); | ||
}); | ||
it("should throw error if timber sends non 200 status code", async (done) => { | ||
it("should throw error if timber sends non 200 status code", async () => { | ||
nock_1.default("https://logs.timber.io") | ||
@@ -48,5 +58,54 @@ .post("/frames") | ||
await expect(node.log(message)).rejects.toThrow(); | ||
done(); | ||
}); | ||
it("should enable piping logs to a writable stream", async () => { | ||
// Create a writable stream | ||
const writeStream = new stream_1.Writable({ | ||
write(chunk, encoding, callback) { | ||
// Will be a buffered JSON string -- parse | ||
const log = JSON.parse(chunk.toString()); | ||
// Expect the log to match the message | ||
expect(log.message).toEqual(message); | ||
callback(); | ||
} | ||
}); | ||
// Fixtures | ||
const timber = new node_1.Node("test"); | ||
timber.pipe(writeStream); | ||
const message = "This should be streamed"; | ||
// Mock the sync method by simply returning the same logs | ||
timber.setSync(async (logs) => logs); | ||
// Fire a log event | ||
await timber.log(message); | ||
}); | ||
it("should pipe logs to a writable file stream", async (done) => { | ||
// Create a temporary file name | ||
const temp = path.join(os.tmpdir(), `timber_${Math.random()}`); | ||
// Create a write stream based on that temp file | ||
const writeStream = fs.createWriteStream(temp); | ||
// Pass write stream to Timber | ||
const timber = new node_1.Node("test"); | ||
timber.pipe(writeStream); | ||
// Mock the sync method by simply returning the same logs | ||
timber.setSync(async (logs) => logs); | ||
// Create messages | ||
const messages = ["message 1", "message 2"]; | ||
// Log messages | ||
await Promise.all(messages.map(msg => timber.log(msg))); | ||
writeStream.on("finish", () => { | ||
// Get the stored data, and translate back to JSON | ||
const data = fs | ||
.readFileSync(temp) | ||
.toString() | ||
.trim() | ||
.split("\n") | ||
.map(line => JSON.parse(line)); | ||
// Messages should match | ||
for (let i = 0; i < messages.length; i++) { | ||
expect(data[i].message).toEqual(messages[i]); | ||
} | ||
done(); | ||
}); | ||
writeStream.end(); | ||
}); | ||
}); | ||
//# sourceMappingURL=node.test.js.map |
# License | ||
Copyright (c) 2018, Timber Technologies, Inc. | ||
Copyright (c) 2018. Timber Technologies, Inc. | ||
@@ -5,0 +5,0 @@ Permission to use, copy, modify, and/or distribute this software for any purpose |
{ | ||
"name": "@timberio/node", | ||
"version": "0.21.0", | ||
"version": "0.22.0", | ||
"description": "Timber.io - Node.js logger", | ||
@@ -42,5 +42,5 @@ "keywords": [ | ||
"dependencies": { | ||
"@timberio/core": "^0.21.0", | ||
"@timberio/tools": "^0.21.0", | ||
"@timberio/types": "^0.21.0", | ||
"@timberio/core": "^0.22.0", | ||
"@timberio/tools": "^0.22.0", | ||
"@timberio/types": "^0.22.0", | ||
"@types/msgpack5": "^3.4.1", | ||
@@ -50,3 +50,3 @@ "cross-fetch": "^2.2.3", | ||
}, | ||
"gitHead": "ad47bc5850606d875f014d39f5a81daf87810729" | ||
"gitHead": "40597144b884fdb8481049eac289b7ff9015a671" | ||
} |
@@ -1,5 +0,58 @@ | ||
# 🌲 Timber - Logging core | ||
# 🌲 Timber - Node.js logging | ||
## 👷️ WIP - Don't use yet! Use [this Timber JS lib](https://github.com/timberio/timber-node) for now | ||
![Beta: Ready for testing](https://img.shields.io/badge/early_release-beta-green.svg) | ||
![Speed: Blazing](https://img.shields.io/badge/speed-blazing%20%F0%9F%94%A5-brightgreen.svg) | ||
[![ISC License](https://img.shields.io/badge/license-ISC-ff69b4.svg)](LICENSE.md) | ||
> Docs TBA | ||
**New to Timber?** [Here's a low-down on logging in Javascript.](https://github.com/timberio/timber-js) | ||
## `@timberio/node` | ||
This NPM library is for logging in Node.js. | ||
If you have a universal or client-side app that requires logging in the browser, check out [`@timberio/browser`](https://github.com/timberio/timber-js/tree/master/packages/browser) or [`@timberio/js`](https://github.com/timberio/timber-js/tree/master/packages/js) (which combines the two packages.) | ||
Here's how to get started: | ||
### Installation | ||
Install the package directly from NPM: | ||
``` | ||
npm i @timberio/node | ||
``` | ||
### Importing | ||
In ES6/Typescript, import the `Timber` class: | ||
```typescript | ||
import { Timber } from "@timberio/node"; | ||
``` | ||
For CommonJS, require the package: | ||
```js | ||
const { Timber } = require("@timberio/node"); | ||
``` | ||
## Creating a client | ||
Simply pass your [Timber.io](https://timber.io) API key as a parameter to a new `Timber` instance: | ||
```typescript | ||
const timber = new Timber("api-goes-here"); | ||
``` | ||
## Documentation | ||
This Node.js library extends [`@timberio/core`](https://github.com/timberio/timber-js/tree/master/packages/core), which provides a simple API for logging, adding middleware and more. | ||
Visit the relevant readme section for more info/how-to: | ||
- [Logging](https://github.com/timberio/timber-js/tree/master/packages/core#logging) | ||
- [Middleware](https://github.com/timberio/timber-js/tree/master/packages/core#middleware) | ||
### LICENSE | ||
[ISC](LICENSE.md) |
@@ -0,1 +1,6 @@ | ||
import * as os from "os"; | ||
import * as path from "path"; | ||
import * as fs from "fs"; | ||
import { Writable } from "stream"; | ||
import nock from "nock"; | ||
@@ -26,3 +31,3 @@ import { ITimberLog, LogLevel } from "@timberio/types"; | ||
it("should echo log if timber sends 20x status code", async done => { | ||
it("should echo log if timber sends 20x status code", async () => { | ||
nock("https://logs.timber.io") | ||
@@ -37,7 +42,5 @@ .post("/frames") | ||
expect(echoedLog.message).toEqual(expectedLog.message); | ||
done(); | ||
}); | ||
it("should throw error if timber sends non 200 status code", async done => { | ||
it("should throw error if timber sends non 200 status code", async () => { | ||
nock("https://logs.timber.io") | ||
@@ -50,5 +53,74 @@ .post("/frames") | ||
await expect(node.log(message)).rejects.toThrow(); | ||
}); | ||
done(); | ||
it("should enable piping logs to a writable stream", async () => { | ||
// Create a writable stream | ||
const writeStream = new Writable({ | ||
write( | ||
chunk: any, | ||
encoding: string, | ||
callback: (error?: Error | null) => void | ||
): void { | ||
// Will be a buffered JSON string -- parse | ||
const log: ITimberLog = JSON.parse(chunk.toString()); | ||
// Expect the log to match the message | ||
expect(log.message).toEqual(message); | ||
callback(); | ||
} | ||
}); | ||
// Fixtures | ||
const timber = new Node("test"); | ||
timber.pipe(writeStream); | ||
const message = "This should be streamed"; | ||
// Mock the sync method by simply returning the same logs | ||
timber.setSync(async logs => logs); | ||
// Fire a log event | ||
await timber.log(message); | ||
}); | ||
it("should pipe logs to a writable file stream", async done => { | ||
// Create a temporary file name | ||
const temp = path.join(os.tmpdir(), `timber_${Math.random()}`); | ||
// Create a write stream based on that temp file | ||
const writeStream = fs.createWriteStream(temp); | ||
// Pass write stream to Timber | ||
const timber = new Node("test"); | ||
timber.pipe(writeStream); | ||
// Mock the sync method by simply returning the same logs | ||
timber.setSync(async logs => logs); | ||
// Create messages | ||
const messages = ["message 1", "message 2"]; | ||
// Log messages | ||
await Promise.all(messages.map(msg => timber.log(msg))); | ||
writeStream.on("finish", () => { | ||
// Get the stored data, and translate back to JSON | ||
const data = fs | ||
.readFileSync(temp) | ||
.toString() | ||
.trim() | ||
.split("\n") | ||
.map(line => JSON.parse(line)); | ||
// Messages should match | ||
for (let i = 0; i < messages.length; i++) { | ||
expect(data[i].message).toEqual(messages[i]); | ||
} | ||
done(); | ||
}); | ||
writeStream.end(); | ||
}); | ||
}); |
@@ -0,1 +1,3 @@ | ||
import { Duplex, Writable } from "stream"; | ||
import fetch from "cross-fetch"; | ||
@@ -5,3 +7,3 @@ // import Msgpack from "msgpack5"; | ||
import { base64Encode } from "@timberio/tools"; | ||
import { ITimberLog, ITimberOptions } from "@timberio/types"; | ||
import { ITimberLog, ITimberOptions, LogLevel } from "@timberio/types"; | ||
import { Base } from "@timberio/core"; | ||
@@ -15,2 +17,8 @@ | ||
export class Node extends Base { | ||
/** | ||
* Readable/Duplex stream where JSON stringified logs of type `ITimberLog` | ||
* will be pushed after syncing | ||
*/ | ||
private _writeStream?: Writable | Duplex; | ||
public constructor(apiKey: string, options?: Partial<ITimberOptions>) { | ||
@@ -50,2 +58,37 @@ super(apiKey, options); | ||
} | ||
/** | ||
* Override `Base` log to enable Node.js streaming | ||
* | ||
* @param message: string - Log message | ||
* @param level (LogLevel) - Level to log at (debug|info|warn|error) | ||
* @param log: (Partial<ITimberLog>) - Initial log (optional) | ||
* @returns Promise<ITimberLog> after syncing | ||
*/ | ||
public async log( | ||
message: string, | ||
level?: LogLevel, | ||
log?: Partial<ITimberLog> | ||
): Promise<ITimberLog> { | ||
// Process/sync the log, per `Base` logic | ||
const processedLog = await super.log(message, level, log); | ||
// Push the processed log to the stream, for piping | ||
if (this._writeStream) { | ||
this._writeStream.write(JSON.stringify(processedLog) + "\n"); | ||
} | ||
// Return the transformed log | ||
return processedLog; | ||
} | ||
/** | ||
* Pipe JSON stringified `ITimberLog` to a stream after syncing | ||
* | ||
* @param stream - Writable|Duplex stream | ||
*/ | ||
public pipe(stream: Writable | Duplex) { | ||
this._writeStream = stream; | ||
return stream; | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
23417
399
59
1
+ Added@timberio/core@0.22.0(transitive)
+ Added@timberio/tools@0.22.0(transitive)
+ Added@timberio/types@0.22.0(transitive)
- Removed@timberio/core@0.21.0(transitive)
- Removed@timberio/tools@0.21.0(transitive)
- Removed@timberio/types@0.21.0(transitive)
Updated@timberio/core@^0.22.0
Updated@timberio/tools@^0.22.0
Updated@timberio/types@^0.22.0