-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
README.hbs
215 lines (164 loc) · 9.98 KB
/
README.hbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# lambda-log
[![npm](https://img.shields.io/npm/v/lambda-log.svg?style=for-the-badge)](https://www.npmjs.com/package/lambda-log) [![npm](https://img.shields.io/npm/dt/lambda-log.svg?style=for-the-badge)](https://www.npmjs.com/package/lambda-log) [![David](https://img.shields.io/david/KyleRoss/node-lambda-log.svg?style=for-the-badge)](https://david-dm.org/KyleRoss/node-lambda-log) [![Travis](https://img.shields.io/travis/KyleRoss/node-lambda-log/master.svg?style=for-the-badge)](https://travis-ci.org/KyleRoss/node-lambda-log) [![Coveralls](https://img.shields.io/coveralls/github/KyleRoss/node-lambda-log.svg?style=for-the-badge)](https://coveralls.io/github/KyleRoss/node-lambda-log) [![license](https://img.shields.io/github/license/KyleRoss/node-lambda-log.svg?style=for-the-badge)](https://github.com/KyleRoss/node-lambda-log/blob/master/LICENSE) [![Beerpay](https://img.shields.io/beerpay/KyleRoss/node-lambda-log.svg?style=for-the-badge)](https://beerpay.io/KyleRoss/node-lambda-log)
Basic logging mechanism for **Node 6.10+** Lambda Functions which properly formats various logs into JSON format for easier reading through Cloudwatch Logs. The module includes functionality to include custom metadata and tags for each log, allowing increased filtering capabilities within Cloudwatch.
> This module is not just for Lambda! You can use this is many different environments that support reading JSON from logs. While the name remains `lambda-log`, it's really a universal JSON logger.
**Why another lambda logger?**
There are others out there, but seemed to be convoluted, included more functionality than needed, not maintained, or not configurable enough. I created lambda-log to include the important functionality from other loggers, but still keeping it simple and dependency-free.
### Features
* Global metadata and tags that are included with every log.
* Pluggable by wrapping/extending the LambdaLog class.
* Emits event on log to allow third-party integration.
* Error and Error-like objects logged include stacktraces in the metadata automatically.
* Pretty-printing of JSON object in [dev](#lambdalogconfig) mode.
* Well-documented and commented source.
* Small footprint!
#### New in Version 2.0.0
* Dynamic metadata can be added to every log (ex. timestamp).
* ~~Logs can be piped to a custom stream instead of stdout/stderr.~~
* In 2.1.0, you can override the logging mechanism with a `console`-like object.
* Log levels and their corresponding console method can be customized.
* Logs are now an instance of a class with a simple API.
* When logs are converted to JSON, you can customize/mask certain data using a replacer function.
* And a bunch more...
There are a few breaking changes in version 2.0.0. If you are upgrading from the previous version, please read the Changelog and documentation to see how these changes could affect your implementation.
---
## Getting Started
### Requirements
Node v6.10+ is required. You need to ensure that your Lambda function is running with the correct Node version.
### Install
Install via NPM:
```bash
$ npm install lambda-log --save
```
### Usage
Here is a basic usage example, read the API documentation below to learn more.
```js
const log = require('lambda-log');
exports.handler = function(event, context, callback) {
// set some optional metadata to be included in all logs (this is an overkill example)
log.options.meta.event = event;
// add additional tags to all logs
log.options.tags.push(event.env);
// Log info message
log.info('my lambda function is running!');
//=> { _logLevel: 'info' msg: 'my lambda function is running!', event:..., _tags: ['log', 'info', ...] }
if(somethingHappenedButNotFatal) {
log.warn('something is missing, but it is OK');
//=> { _logLevel: 'warn', msg: 'something is missing, but it is OK', event:..., _tags: ['log', 'warn', ...] }
}
// Debug messages are not generated or displayed unless enabled in the config
log.debug('some debug message');
//=> false
// Enable debug messages
log.options.debug = true;
log.debug('some debug message again');
//=> { _logLevel: 'debug', msg: 'some debug message again', event:..., _tags: ['log', 'debug', ...] }
someAsyncTask(function(err, results) {
if(err) {
log.error(err);
//=> { _logLevel: 'error', msg: 'Error from someAsyncTask', stack: ..., event: ..., _tags: ['log', 'error', ...]}
} else {
log.info('someAsyncTask completed successfully!', { results });
//=> { _logLevel: 'info', msg: 'someAsyncTask completed successfully!', results:..., event: ..., _tags: ['log', 'info', ...]}
}
});
// New in version 1.4.0 - assert
someAsyncTask(function(err, results) {
if(err) {
log.error(err);
//=> { _logLevel: 'error', msg: 'Error from someAsyncTask', stack: ..., event: ..., _tags: ['log', 'error', ...]}
} else {
// Will only log if no results are returned
log.assert(results, 'No results returned from someAsyncTask');
}
});
};
```
---
# API Documentation
{{#orphans ~}}
{{>docs~}}
{{/orphans~}}
---
### Log Output
Each log generated is a custom object that has a set of properties containing all of the values of the log. This output is converted to JSON and logged to the console. Below are the properties included in the log.
#### Default Properties:
* `_logLevel` _(String)_ - The log level (ex. error, warn, info, debug) _Since 1.3.0_
* `msg` _(String)_ - The message of the log
* `_tags` _(Array[String])_ - Array of tags applied to the log
#### Conditional Properties:
* `*` _(Any)_ - Any metadata provided, dynamic metadata and global metadata as individual properties
* `stack` _(String)_ - Stack trace of an error if an `Error` was provided
---
### Custom Log Levels
New in version 2.0.0, the ability to customize log levels is now available. In order to customize the log levels, you must create a new instance of `LambdaLog` as any changes directly to a pre-existing instance will not function correctly. The custom log levels are manipulated by passing in an object to the `levels` argument of the LambdaLog constructor. The provided custom log levels object will override and extend the existing log levels.
Any custom log levels added will be available as shorthand methods on the LambdaLog instance.
#### Log Levels Object
The log levels object is a simple object that should contain key/value pairs. The keys should be the log level and the values may be either a string or function. If a string is provided, it should be the corresponding `console` method name to use for the log (ex. `log` or `info`). If a function is provided, it must return either a string `console` method or `false` to prevent logging. The function will be called for every log with `message` ([LogMessage](#logmessage)) as the only parameter, allowing customizing the logging for certain messages.
```js
const LambdaLog = require('lambda-log').LambdaLog;
const log = new LambdaLog({}, {
fatal: 'error',
poop: function(message) {
// prepend an emoji to every message
message.msg = '💩 ' + message.msg;
return 'log';
},
info: function() {
// Make `log.info()` work like `log.debug()`
if(this.options.debug) return false;
return 'info';
}
});
log.poop('This is a test');
//=> "💩 This is a test"
```
---
### Dynamic Metadata
New in version 2.0.0, lambda-log now has the ability to execute and include dynamic metadata for each log. Dynamic metadata is generated using a function (`log.options.dynamicMeta`) on the creation of each message and included into the metadata.
#### Dynamic Metadata Function
The dynamic metadata function is included into `log.options` and will run for every log. The function is called with parameters `message` (the LogMessage instance) and `options` (`log.options` object). The function should return an object with metadata to inject into the log.
```js
// Add timestamp to each log
log.options.dynamicMeta = function(message) {
return {
timestamp: new Date().toISOString()
};
};
```
---
### Log Handler
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:
* log
* debug
* info
* error
* warn
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.
```js
const log = require('lambda-log');
// example using `Console` instance
const { Console } = require('console');
log.options.logHandler = new Console(myStdoutStream, myStderrStream);
// or with a console-like object
const myConsole = {
log(message) {
// log `message` somewhere custom
},
error(message) {
// ...
},
...
};
```
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.
---
## Tests
Tests are written and provided as part of the module. It requires mocha to be installed which is included as a `devDependency`. You may run the tests by calling:
```bash
$ npm run test
```
## Contributing
Feel free to submit a pull request if you find any issues or want to integrate a new feature. Keep in mind, this module should be lightweight and advanced functionality should be published to NPM as a wrapper around this module. Ensure to write and run the tests before submitting a pull request. The code should work without any special flags in Node 6.10.
## License
MIT License. See [License](https://github.com/KyleRoss/node-lambda-log/blob/master/LICENSE) in the repository.