You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For detailed release notes see [Releases](https://github.com/jeremydaly/lambda-api/releases).
115
124
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
+
116
128
### v0.7: Restrict middleware execution to certain paths
117
129
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.
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).
710
722
711
723
The logger is attached to the `REQUEST` object and can be used anywhere the object is available (e.g. routes, middleware, and error handlers).
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.
723
735
724
736
### 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.
726
738
727
739
| Property | Type | Description | Default |
728
740
| -------- | ---- | ----------- | ------- |
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`|
730
742
| customKey |`string`| Sets the JSON property name for custom data passed to logs. |`custom`|
731
743
| detail |`boolean`| Enables/disables adding `REQUEST` and `RESPONSE` data to all log entries. |`false`|
732
744
| 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
735
747
| nested |`boolean`| Enables/disables nesting of JSON logs for serializer data. See [Serializers](#serializers). |`false`|
736
748
| 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`|
737
749
| 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). ||
739
751
| stack |`boolean`| Enables/disables the inclusion of stack traces in caught errors. |`false`|
740
752
753
+
Example:
741
754
```javascript
742
755
constapi=require('lambda-api')({
743
756
logger: {
@@ -768,7 +781,7 @@ Logs are generated using Lambda API's standard JSON format. The log format can b
768
781
"remaining":2000, // remaining milliseconds until function timeout
769
782
"function":"my-function-v1", // function name
770
783
"memory":2048, // allocated function memory
771
-
"sample":true// generated during sampling request
784
+
"sample":true//is generated during sampling request?
772
785
}
773
786
```
774
787
@@ -781,11 +794,13 @@ Access logs use the same format as the standard logs above, but include addition
781
794
```javascript
782
795
{
783
796
... 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
789
804
"foo":"bar"
790
805
}
791
806
}
@@ -848,7 +863,7 @@ req.log.info('This is the main log message',['val1','val2','val3']) // array
848
863
req.log.info('This is the main log message',true) // boolean
849
864
```
850
865
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.
852
867
853
868
### Serializers
854
869
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')({
955
970
})
956
971
```
957
972
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.
958
974
959
975
## Middleware
960
976
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