Skip to content

Commit

Permalink
Merge pull request #201 from SAP/fastify
Browse files Browse the repository at this point in the history
Add fastify middleware
  • Loading branch information
christiand93 authored Mar 6, 2024
2 parents 9c2978e + 4936daa commit 91ed3b5
Show file tree
Hide file tree
Showing 25 changed files with 877 additions and 190 deletions.
51 changes: 33 additions & 18 deletions docs/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
GEM
remote: https://rubygems.org/
specs:
activesupport (7.0.7.2)
activesupport (7.1.3)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
addressable (2.8.4)
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
base64 (0.2.0)
bigdecimal (3.1.6)
coffee-script (2.4.1)
coffee-script-source
execjs
coffee-script-source (1.11.1)
colorator (1.1.0)
commonmarker (0.23.10)
concurrent-ruby (1.2.2)
concurrent-ruby (1.2.3)
connection_pool (2.4.1)
dnsruby (1.70.0)
simpleidn (~> 0.2.1)
drb (2.2.0)
ruby2_keywords
em-websocket (0.5.3)
eventmachine (>= 0.12.9)
http_parser.rb (~> 0)
ethon (0.16.0)
ffi (>= 1.15.0)
eventmachine (1.2.7)
execjs (2.8.1)
faraday (2.7.5)
faraday-net_http (>= 2.0, < 3.1)
ruby2_keywords (>= 0.0.4)
faraday-net_http (3.0.2)
ffi (1.15.5)
execjs (2.9.1)
faraday (2.9.0)
faraday-net_http (>= 2.0, < 3.2)
faraday-net_http (3.1.0)
net-http
ffi (1.16.3)
forwardable-extended (2.6.0)
gemoji (3.0.1)
github-pages (228)
Expand Down Expand Up @@ -196,10 +206,11 @@ GEM
gemoji (~> 3.0)
html-pipeline (~> 2.2)
jekyll (>= 3.0, < 5.0)
just-the-docs (0.3.3)
just-the-docs (0.7.0)
jekyll (>= 3.8.5)
jekyll-seo-tag (~> 2.0)
rake (>= 12.3.1, < 13.1.0)
jekyll-include-cache
jekyll-seo-tag (>= 2.0)
rake (>= 12.3.1)
kramdown (2.3.2)
rexml
kramdown-parser-gfm (1.1.0)
Expand All @@ -214,7 +225,10 @@ GEM
jekyll (>= 3.5, < 5.0)
jekyll-feed (~> 0.9)
jekyll-seo-tag (~> 2.1)
minitest (5.19.0)
minitest (5.22.2)
mutex_m (0.2.0)
net-http (0.4.1)
uri
nokogiri (1.16.2)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
Expand All @@ -231,11 +245,11 @@ GEM
forwardable-extended (~> 2.6)
public_suffix (4.0.7)
racc (1.7.3)
rake (13.0.6)
rake (13.1.0)
rb-fsevent (0.11.2)
rb-inotify (0.10.1)
ffi (~> 1.0)
rexml (3.2.5)
rexml (3.2.6)
rouge (3.26.0)
ruby2_keywords (0.0.5)
rubyzip (2.3.2)
Expand All @@ -252,15 +266,16 @@ GEM
unf (~> 0.1.4)
terminal-table (1.8.0)
unicode-display_width (~> 1.1, >= 1.1.1)
typhoeus (1.4.0)
typhoeus (1.4.1)
ethon (>= 0.9.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unf (0.1.4)
unf_ext
unf_ext (0.0.8.2)
unf_ext (0.0.9.1)
unicode-display_width (1.8.0)
webrick (1.7.0)
uri (0.13.0)
webrick (1.8.1)

PLATFORMS
ruby
Expand Down
4 changes: 2 additions & 2 deletions docs/_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# You can create any custom variable you would like, and they will be accessible
# in the templates via {{ site.myvariable }}.

title: cf-nodejs-logging-support
title: "CF Node.js Logging Support"
description: >- # this means to ignore newlines until "baseurl:"
Node.js Logging Support for Cloud Foundry provides the creation of structured log messages and the collection of request metrics.
baseurl: "/cf-nodejs-logging-support" # the subpath of your site, e.g. /blog
Expand Down Expand Up @@ -60,7 +60,7 @@ back_to_top: true
back_to_top_text: "Back to top"

# Footer copyright
footer_content: "Copyright &copy; 2021 SAP SE | <a href=\"https://www.sap.com/about/legal/privacy.html\">SAP Privacy Statement</a>"
footer_content: "Copyright &copy; 2024 SAP SE | <a href=\"https://www.sap.com/about/legal/privacy.html\">SAP Privacy Statement</a>"

# Footer last edited timestamp
last_edit_timestamp: true # show or hide edit time - page must have `last_modified_date` defined in the frontmatter
Expand Down
4 changes: 4 additions & 0 deletions docs/_sass/custom/custom.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

.site-title {
font-size: 1.2em !important;
}
1 change: 1 addition & 0 deletions docs/configuration/fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ Our supported server frameworks are:
* [Express](https://expressjs.com/): declare as `express`
* [Restify](http://restify.com/): declare as `restify`
* [Connect](https://www.npmjs.com/package/connect): declare as `connect`
* [Fastify](https://fastify.dev/): declare as `fastify`
* [Node.js HTTP](https://nodejs.org/api/http.html): declare as `plainhttp`

Example of field with multiple framework specific sources:
Expand Down
1 change: 1 addition & 0 deletions docs/configuration/framework.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ Our supported server frameworks are:
* [Express](https://expressjs.com/): declare as `express`
* [Restify](http://restify.com/): declare as `restify`
* [Connect](https://www.npmjs.com/package/connect): declare as `connect`
* [Fastify](https://fastify.dev/): declare as `fastify`
* [Node.js HTTP](https://nodejs.org/api/http.html): declare as `plainhttp`
18 changes: 13 additions & 5 deletions docs/getting-started/01-installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,26 @@ permalink: /getting-started/installation/
---

# Installation

The latest release version can be downloaded from [npm](https://www.npmjs.com/package/cf-nodejs-logging-support) and [github](https://github.com/SAP/cf-nodejs-logging-support/releases).

## Requirements

To take full advantage of cf-nodejs-logging-support in your application you need to fulfill following requirements:
* Node.js app to be deployed on Cloud Foundry
* Installed [npm](https://docs.npmjs.com/) package manager
* Use [node.js](https://nodejs.org/) version 10.0 or higher
* Use [express](https://expressjs.com/), [connect](https://www.npmjs.com/package/connect), [restify](http://restify.com/) or pure [Node.js HTTP](https://nodejs.org/api/http.html) server framework
To take full advantage of cf-nodejs-logging-support make sure to fulfill following requirements:

* Node.js app to be deployed on Cloud Foundry
* Use [node.js](https://nodejs.org/) version 14.14 or higher
* Use one of the supported server frameworks:
* [Express](https://expressjs.com/)
* [Connect](https://www.npmjs.com/package/connect)
* [Restify](http://restify.com/)
* [Fastify](https://fastify.dev/)
* [Node.js HTTP](https://nodejs.org/api/http.html)

## Install using npm

Use following command to add cf-nodejs-logging-support and its dependencies to your app.

```bash
npm install cf-nodejs-logging-support --save
```
167 changes: 105 additions & 62 deletions docs/getting-started/02-framework-samples.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ permalink: /getting-started/framework-samples/

# Samples for supported Server Frameworks
{: .no_toc }
This library can be used in combination with different server frameworks:
[express](https://expressjs.com/), [connect](https://www.npmjs.com/package/connect), [restify](http://restify.com/) and also pure [Node.js HTTP](https://nodejs.org/api/http.html).

This library can be used in combination with several different server frameworks.
You can find small code samples for each supported framework below.

<details open markdown="block">
Expand All @@ -21,96 +21,139 @@ You can find small code samples for each supported framework below.
{:toc}
</details>


## Express

```js
const HTTP_PORT = 3000;
var express = require('express');
var log = require('cf-nodejs-logging-support');
var app = express();
const app = require('express')()
const log = require('cf-nodejs-logging-support')

// Set the minimum logging level (Levels: off, error, warn, info, verbose, debug, silly)
log.setLoggingLevel("info");
// Configure logger for working with Express framework
log.setFramework(log.Framework.Express)

// Bind to express app
app.use(log.logNetwork);
// Add the logger middleware to write access logs
app.use(log.logNetwork)

app.get('/', function (req, res) {
// Context bound custom message
req.logger.info("Hello World will be sent");

res.send('Hello World');
});
// Handle '/' path
app.get("/", (req, res) => {
// Write a log message bound to request context
req.logger.info(`Sending a greeting`)
res.send("Hello Express")
})

// Listen on specified port
app.listen(HTTP_PORT);

// Formatted log message
log.info("Server is listening on port %d", HTTP_PORT);
const listener = app.listen(3000, () => {
// Formatted log message
log.info("Server is listening on port %d", listener.address().port)
})
```

## Connect

```js
const HTTP_PORT = 3000;
var connect = require('connect');
var http = require('http');
var log = require('cf-nodejs-logging-support');
var app = connect();
const app = require('connect')()
const http = require('http')
const log = require('cf-nodejs-logging-support')

// Force logger to run the connect version. (default is express, forcing express is also legal)
log.forceLogger("connect");
// Configure logger for working with Connect framework
log.setFramework(log.Framework.Connect)

// Add the logger middleware, so each time a request is received, it will get logged.
app.use(log.logNetwork);
// Add the logger middleware to write access logs
app.use(log.logNetwork)

// Create node.js http server and listen on port
http.createServer(app).listen(HTTP_PORT, () => {
// Formatted log message
log.info("Server is listening on port %d", HTTP_PORT);
});
// Handle '/' path
app.use("/", (req, res) => {
// Write a log message bound to request context
req.logger.info(`Sending a greeting`)
res.end("Hello Connect")
})

// Listen on specified port
const server = http.createServer(app).listen(3000, () => {
// Formatted log message
log.info("Server is listening on port %d", server.address().port)
})
```

## Restify

```js
const HTTP_PORT = 3000;
var restify = require('restify');
var log = require('cf-nodejs-logging-support');
var app = restify.createServer();
const restify = require('restify')
const log = require('cf-nodejs-logging-support')
const app = restify.createServer()

// Configure logger for working with Restify framework
log.setFramework(log.Framework.Restify)

// Force logger to run the restify version. (default is express, forcing express is also legal)
log.forceLogger("restify");
// Add the logger middleware to write access logs
app.use(log.logNetwork)

// Add the logger middleware, so each time a request is received, it will get logged.
app.use(log.logNetwork);
// Handle '/' path
app.get("/", (req, res, next) => {
// Write a log message bound to request context
req.logger.info(`Sending a greeting`)
res.send("Hello Restify")
next()
})

// Listen on specified port
server.listen(HTTP_PORT, () => {
app.listen(3000, () => {
// Formatted log message
log.info("Server is listening on port %d", HTTP_PORT);
});
log.info("Server is listening on port %d", app.address().port)
})
```

## Fastify

```js
const log = require('cf-nodejs-logging-support')
const app = require('fastify')()

// Configure logger for working with Fastify framework
log.setFramework(log.Framework.Fastify)

// Add the logger middleware to write access logs
app.addHook("onRequest", log.logNetwork)

// Handle '/' path
app.get("/", (request, reply) => {
// Write a log message bound to request context
request.logger.info(`Sending a greeting`)
reply.send("Hello Fastify")
})

// Listen on specified port
app.listen({ port: 3000 }, (err, address) => {
if (err) {
// Formatted error message
log.error("Failed to run server", err.message)
process.exit(1)
}
// Formatted log message
log.info(`Server is listening on ${address}`)
})
```

## Node.js HTTP

```js
const HTTP_PORT = 3000;
const http = require('http');
var log = require("cf-nodejs-logging-support");
const log = require('cf-nodejs-logging-support')
const http = require('http')

// Force logger to run the http version.
log.forceLogger("plainhttp");
// Configure logger for working with Node.js http framework
log.setFramework(log.Framework.NodeJsHttp)

const server = http.createServer((req, res) => {
// Binds logging to the given request for request tracking
log.logNetwork(req, res);
// Context bound custom message
req.logger.info("Hello World will be sent");
res.end('Hello World');
});
// Call logger middleware to write access logs
log.logNetwork(req, res)

// Write a log message bound to request context
req.logger.info(`Sending a greeting`)
res.send("Hello Node.js HTTP")
})

// Listen on specified port
server.listen(HTTP_PORT, () => {
// Formatted log message
log.info("Server is listening on port %d", HTTP_PORT);
});
server.listen(3000, () => {
// Formatted log message
log.info("Server is listening on port %d", server.address().port)
})
```
Loading

0 comments on commit 91ed3b5

Please sign in to comment.