Skip to content

Commit aace440

Browse files
authored
Merge pull request #12 from KyleRoss/2.1.0-dev
Release 2.1.0
2 parents a49a5af + 56b486d commit aace440

6 files changed

Lines changed: 55 additions & 36 deletions

File tree

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
language: node_js
22
node_js:
3+
- "11"
34
- "10"
45
- "9"
56
- "8"

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# LambdaLog Changelog
22

3+
## 2.1.0 (12/19/2018)
4+
* **BREAKING:** Removed `stdoutStream` and `stderrStream` options.
5+
* **NEW:** Added `logHandler` option which takes a `console`-like object to send logs through. (#11)
6+
37
## 2.0.1 (12/7/2018)
48
* Fix console logging pointing to global `console` instead of custom console instance for streaming. (@sh1n1chi8acker - #10)
59
* Update mocha to v5.2.0

README.md

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ There are others out there, but seemed to be convoluted, included more functiona
2020

2121
#### New in Version 2.0.0
2222
* Dynamic metadata can be added to every log (ex. timestamp).
23-
* Logs can be piped to a custom stream instead of stdout/stderr.
23+
* ~~Logs can be piped to a custom stream instead of stdout/stderr.~~
24+
* In 2.1.0, you can override the logging mechanism with a `console`-like object.
2425
* Log levels and their corresponding console method can be customized.
2526
* Logs are now an instance of a class with a simple API.
2627
* When logs are converted to JSON, you can customize/mask certain data using a replacer function.
@@ -114,17 +115,16 @@ Constructor for the `LambdaLog` class. Provided to be utilized in more advanced
114115
### log.options
115116
Configuration object for LambdaLog. Most options can be changed at any time via `log.options.OPTION = VALUE;` unless otherwise noted.
116117

117-
| Option | Type | Description | Default |
118-
|----------------|---------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------|
119-
| `meta` | Object | Global metadata to be included in all logs. | `{}` |
120-
| `tags` | Array[String] | Global tags to be included in all logs. | `[]` |
121-
| `dynamicMeta` | Function | Function that runs for each log that returns additional metadata. See [Dynamic Metadata](#dynamic-metadata). | `null` |
122-
| `debug` | Boolean | Enables `log.debug()`. | `false` |
123-
| `dev` | Boolean | Enable development mode which pretty-prints JSON to the console. | `false` |
124-
| `silent` | Boolean | Disables logging to `console` but messages and events are still generated. | `false` |
125-
| `replacer` | Function | Replacer function for `JSON.stringify()` to allow handling of sensitive data before logs are written. See [JSON.stringify](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter). | `null` |
126-
| `stdoutStream` | Stream | Stream in which non-error logs will be written to. This option cannot be changed after instantiation. See [Log Streams](#log-streams). | `process.stdout` |
127-
| `stderrStream` | Stream | Stream in which error logs will be written to. This option cannot be changed after instantiation. See [Log Streams](#log-streams). | `process.stderr` |
118+
| Option | Type | Description | Default |
119+
|---------------|---------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------|
120+
| `meta` | Object | Global metadata to be included in all logs. | `{}` |
121+
| `tags` | Array[String] | Global tags to be included in all logs. | `[]` |
122+
| `dynamicMeta` | Function | Function that runs for each log that returns additional metadata. See [Dynamic Metadata](#dynamic-metadata). | `null` |
123+
| `debug` | Boolean | Enables `log.debug()`. | `false` |
124+
| `dev` | Boolean | Enable development mode which pretty-prints JSON to the console. | `false` |
125+
| `silent` | Boolean | Disables logging to `console` but messages and events are still generated. | `false` |
126+
| `replacer` | Function | Replacer function for `JSON.stringify()` to allow handling of sensitive data before logs are written. See [JSON.stringify](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter). | `null` |
127+
| `logHandler` | Object | A `console`-like object containing all standard console functions. Allows logs to be written to any custom location. See [Log Handler](#loghandler). | `console` |
128128

129129

130130
### log.<_info_|_warn_|_error_|_debug_|_*_>(msg[, _meta={}_][, _tags=[]_])
@@ -398,8 +398,37 @@ log.options.dynamicMeta = function(message) {
398398

399399
---
400400

401-
### Log Streams
402-
New in version 2.0.0, you may now customize the streams in which logs are written to. By default, all logs are written using the built-in `console` object to `process.stdout` and `process.stderr`. You may use alternative streams (like `fs.createWriteStream()` to log to a file) to customize where your logs go. This is useful for non-aws or custom implementations so logs may be sent to the most convient place.
401+
### Log Handler
402+
New in version 2.1.0, you may now customize the methods used to log messages. By default, lambda-log uses the global `console` object, but you can override this with a custom instance of <code><a href="https://nodejs.org/docs/latest-v8.x/api/console.html#console_new_console_stdout_stderr">Console</a></code> or your own `console`-like object that implements, at minimum, the following functions:
403+
404+
* log
405+
* debug
406+
* info
407+
* error
408+
* warn
409+
410+
Keep in mind that custom implementations must be synchronus. If you need it to be asynchronus, you will need to use a custom <code><a href="https://nodejs.org/docs/latest-v8.x/api/console.html#console_new_console_stdout_stderr">Console</a></code> instance and implement utilizing streams.
411+
412+
```js
413+
const log = require('lambda-log');
414+
415+
// example using `Console` instance
416+
const { Console } = require('console');
417+
log.options.logHandler = new Console(myStdoutStream, myStderrStream);
418+
419+
// or with a console-like object
420+
const myConsole = {
421+
log(message) {
422+
// log `message` somewhere custom
423+
},
424+
error(message) {
425+
// ...
426+
},
427+
...
428+
};
429+
```
430+
431+
Note that this is a breaking change from version 2.0.0 as there were issues by always utlizing a custom `Console` instance for certain users.
403432

404433
---
405434

lib/LambdaLog.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
"use strict";
2-
const { Console } = require('console');
32
const EventEmitter = require('events');
43

54
const LogMessage = require('./LogMessage');
@@ -43,10 +42,8 @@ class LambdaLog extends EventEmitter {
4342
silent: false,
4443
// Optional replacer function for `JSON.stringify`
4544
replacer: null,
46-
// Optional stream to write stdout messages to
47-
stdoutStream: process.stdout,
48-
// Optional stream to write stderr messages to
49-
stderrStream: process.stderr
45+
// Console-like object to log messages to
46+
logHandler: console
5047
}, options);
5148

5249
/**
@@ -66,10 +63,10 @@ class LambdaLog extends EventEmitter {
6663
this._levels = Object.keys(this._logLevels);
6764

6865
/**
69-
* Instance of `Console` used for logging
66+
* Console-like log handler to use for logging messages
7067
* @type {Object}
7168
*/
72-
this.console = new Console(this.options.stdoutStream, this.options.stderrStream);
69+
this.console = this.options.logHandler;
7370

7471
this._levels.forEach((lvl) => {
7572
/**

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lambda-log",
3-
"version": "2.0.1",
3+
"version": "2.1.0",
44
"description": "Basic logging mechanism for Node 6.10+ Lambda Functions",
55
"main": "index.js",
66
"scripts": {

test/lambdaLog.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,21 @@ describe('LambdaLog', function() {
1616
assert(log.options.dev === false);
1717
assert(log.options.silent === false);
1818
assert(log.options.replacer === null);
19-
assert(log.options.stdoutStream === process.stdout);
20-
assert(log.options.stderrStream === process.stderr);
19+
assert(log.options.logHandler === console);
2120
});
2221

2322
it ('should override default options', () => {
2423
let log = new LambdaLog({
2524
meta: { test: true },
2625
tags: ['test'],
2726
dynamicMeta: function() { /*empty*/ },
28-
silent: true,
29-
stderrStream: process.stdout
27+
silent: true
3028
});
3129

3230
assert(log.options.meta.test === true);
3331
assert(log.options.tags.length === 1 && log.options.tags[0] === 'test');
3432
assert(typeof log.options.dynamicMeta === 'function');
3533
assert(log.options.silent === true);
36-
assert(log.options.stderrStream === process.stdout);
3734
});
3835

3936
describe('Log Levels', function() {
@@ -82,15 +79,6 @@ describe('LambdaLog', function() {
8279
});
8380
});
8481
});
85-
86-
describe('Console', () => {
87-
it('should create a custom instance of console', () => {
88-
let { Console } = require('console'),
89-
log = new LambdaLog();
90-
91-
assert(log.console instanceof Console);
92-
});
93-
});
9482
});
9583

9684
describe('Properties', function() {

0 commit comments

Comments
 (0)