Skip to content

Commit 837476e

Browse files
committed
add toc for logging and other doc updates
1 parent 7fb74f9 commit 837476e

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

README.md

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ Lambda API was written to be extremely lightweight and built specifically for se
7777
- [Enabling Binary Support](#enabling-binary-support)
7878
- [Path Parameters](#path-parameters)
7979
- [Wildcard Routes](#wildcard-routes)
80+
- [Logging](#logging)
81+
- [Logging Configuration](#logging-configuration)
82+
- [Log Format](#log-format)
83+
- [Access Logs](#access-logs)
84+
- [Logging Levels](#logging-levels)
85+
- [Custom Logging Levels](#custom-logging-levels)
86+
- [Adding Additional Detail](#adding-additional-detail)
87+
- [Serializers](#serializers)
88+
- [Sampling](#sampling)
8089
- [Middleware](#middleware)
8190
- [Clean Up](#clean-up)
8291
- [Error Handling](#error-handling)
@@ -113,6 +122,9 @@ const api = require('lambda-api')({ version: 'v1.0', base: 'v1' });
113122
## Recent Updates
114123
For detailed release notes see [Releases](https://github.com/jeremydaly/lambda-api/releases).
115124

125+
### v0.8: Logging Support and Sampling
126+
Lambda API has added a powerful (and customizable) logging engine that utilizes native JSON support for CloudWatch Logs. Log entries can be manually added using standard severities like `info` and `warn`. In addition, "access logs" can be automatically generated with detailed information about each requests. See [Logging](#logging) for more information about logging and auto sampling for request tracing.
127+
116128
### v0.7: Restrict middleware execution to certain paths
117129
Middleware now supports an optional path parameter that supports multiple paths, wildcards, and parameter matching to better control middleware execution. See [middleware](#middleware) for more information.
118130

@@ -706,7 +718,7 @@ api.options('/users/*', (req,res) => {
706718
```
707719

708720
## Logging
709-
Lambda API includes a robust logging engine specifically designed for integration with AWS CloudWatch Logs' native JSON support. Not only is it ridiculously fast, but it's also highly configurable. Logging is disabled by default, but can be enabled by passing `{ logger: true }` when you create the Lambda API instance (or by passing a [Logging Configuration](#logging-configuration) definition).
721+
Lambda API includes a robust logging engine specifically designed to utilize native JSON support for CloudWatch Logs. Not only is it ridiculously fast, but it's also highly configurable. Logging is disabled by default, but can be enabled by passing `{ logger: true }` when you create the Lambda API instance (or by passing a [Logging Configuration](#logging-configuration) definition).
710722

711723
The logger is attached to the `REQUEST` object and can be used anywhere the object is available (e.g. routes, middleware, and error handlers).
712724

@@ -722,11 +734,11 @@ api.get('/status', (req,res) => {
722734
In addition to manual logging, Lambda API can also generate "access" logs for your API requests. API Gateway can also provide access logs, but they are limited to contextual information about your request (see [here](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html)). Lambda API allows you to capture the same data **PLUS** additional information directly from within your application.
723735

724736
### Logging Configuration
725-
Logging can be enabled setting the `logger` option to `true` when creating the Lambda API instance. Logging can be configured by setting `logger` to an object that contains configuration information. The following table contains available logging configuration properties.
737+
Logging can be enabled by setting the `logger` option to `true` when creating the Lambda API instance. Logging can be configured by setting `logger` to an object that contains configuration information. The following table contains available logging configuration properties.
726738

727739
| Property | Type | Description | Default |
728740
| -------- | ---- | ----------- | ------- |
729-
| access | `boolean` or `string` | Enables/disables automatic access log generation for each request. See [Access Logs](#access-logs)) | `false` |
741+
| access | `boolean` or `string` | Enables/disables automatic access log generation for each request. See [Access Logs](#access-logs). | `false` |
730742
| customKey | `string` | Sets the JSON property name for custom data passed to logs. | `custom` |
731743
| detail | `boolean` | Enables/disables adding `REQUEST` and `RESPONSE` data to all log entries. | `false` |
732744
| level | `string` | Minimum logging level to send logs for. See [Logging Levels](#logging-levels). | `info` |
@@ -735,9 +747,10 @@ Logging can be enabled setting the `logger` option to `true` when creating the L
735747
| nested | `boolean` | Enables/disables nesting of JSON logs for serializer data. See [Serializers](#serializers). | `false` |
736748
| timestamp | `boolean` or `function` | By default, timestamps will return the epoch time in milliseconds. A value of `false` disables log timestamps. A function that returns a value can be used to override the default format. | `true` |
737749
| sampling | `object` | Enables log sampling for periodic request tracing. See [Sampling](#sampling). | |
738-
| serializers | `object` | Serializers to manipulate the log format. See [Serializers](#serializers). | |
750+
| serializers | `object` | Adds serializers that manipulate the log format. See [Serializers](#serializers). | |
739751
| stack | `boolean` | Enables/disables the inclusion of stack traces in caught errors. | `false` |
740752

753+
Example:
741754
```javascript
742755
const api = require('lambda-api')({
743756
logger: {
@@ -768,7 +781,7 @@ Logs are generated using Lambda API's standard JSON format. The log format can b
768781
"remaining": 2000, // remaining milliseconds until function timeout
769782
"function": "my-function-v1", // function name
770783
"memory": 2048, // allocated function memory
771-
"sample": true // generated during sampling request
784+
"sample": true // is generated during sampling request?
772785
}
773786
```
774787

@@ -781,11 +794,13 @@ Access logs use the same format as the standard logs above, but include addition
781794
```javascript
782795
{
783796
... Standard Log Data ...,
784-
"path": "/user/123",
785-
"ip": "12.34.56.78",
786-
"ua": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6)...",
787-
"version": "v1",
788-
"qs": {
797+
"path": "/user/123", // path accessed
798+
"ip": "12.34.56.78", // client ip address
799+
"ua": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6)...", // User-Agent
800+
"version": "v1", // specified API version
801+
"device": "mobile", // client device (as determined by CloudFront)
802+
"country": "US", // client country (as determined by CloudFront)
803+
"qs": { // query string parameters
789804
"foo": "bar"
790805
}
791806
}
@@ -848,7 +863,7 @@ req.log.info('This is the main log message',['val1','val2','val3']) // array
848863
req.log.info('This is the main log message',true) // boolean
849864
```
850865

851-
If an object is provided, the keys will be merged into the main log entry's JSON. If an other value is provided, the value will be assigned to a key using the the `customKey` setting as its name. If `nested` is set to `true`, objects will be nested under the value of `customKey` as well.
866+
If an `object` is provided, the keys will be merged into the main log entry's JSON. If any other `type` is provided, the value will be assigned to a key using the the `customKey` setting as its property name. If `nested` is set to `true`, objects will be nested under the value of `customKey` as well.
852867

853868
### Serializers
854869
Serializers allow you to customize log formats as well as add additional data from your application. Serializers can be defined by adding a `serializers` property to the `logger` configuration object. A property named for an available serializer (`main`, `req`, `res`, `context` or `custom`) needs to return an anonymous function that takes one argument and returns an object. The returned object will be merged into the main JSON log entry. Existing properties can be removed by returning `undefined` as their values.
@@ -955,6 +970,7 @@ const api = require('lambda-api')({
955970
})
956971
```
957972

973+
Any combination of rules can be provided to customize sampling behavior. Note that each rule tracks requests and velocity separately, which could limit the number of samples for infrequently accessed routes.
958974

959975
## Middleware
960976
The API supports middleware to preprocess requests before they execute their matching routes. Middleware is defined using the `use` method and requires a function with three parameters for the `REQUEST`, `RESPONSE`, and `next` callback. For example:

0 commit comments

Comments
 (0)