Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,64 @@ logger.info('hello winston')

```

### Using as an express middleware (winston 3.1+)

***NOTE: this feature is experimental. The API may change in a backwards
incompatible way until this is deemed stable. Please provide us feedback so
that we can better refine this express integration.***

We provide a middleware that can be used in an express application. Apart from
being easy to use, this enables some more powerful features of Stackdriver
Logging: request bundling. Any application logs emitted on behalf of a specific
request will be shown nested inside the request log as you see in this
screenshot:

![Request Bundling Example](https://raw.githubusercontent.com/googleapis/nodejs-logging-winston/master/doc/images/request-bundling.png)

This middleware adds a `winston`-style log function to the `request` object.
You can use this wherever you have access to the `request` object (`req` in the
sample below). All log entries that are made on behalf of a specific request are
shown bundled together in the Stackdriver Logging UI.

```javascript
const lw = require('@google-cloud/logging-winston');

// Import express module and create an http server.
const express = require('express');

async function main() {
const {logger, mw} = await lw.express.middleware();
const app = express();

// Install the logging middleware. This ensures that a Winston-style `log`
// function is available on the `request` object. Attach this as one of the
// earliest middleware to make sure that the log function is available in all
// subsequent middleware and routes.
app.use(mw);

// Setup an http route and a route handler.
app.get('/', (req, res) => {
// `req.log` can be used as a winston style log method. All logs generated
// using `req.log` use the current request context. That is, all logs
// corresponding to a specific request will be bundled in the Stackdriver
// UI.
req.log.info('this is an info log message');
res.send('hello world');
});

// `logger` can be used as a global logger, one not correlated to any specific
// request.
logger.info('bonjour');

// Start listening on the http server.
app.listen(8080, () => {
logger.info('http server listening on port 8080');
});
}

main();
```

### Error Reporting

Any `Error` objects you log at severity `error` or higher can automatically be picked up by [Stackdriver Error Reporting][error-reporting] if you have specified a `serviceContext.service` when instantiating a `LoggingWinston` instance:
Expand Down
Binary file added doc/images/request-bundling.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 11 additions & 45 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@
"eslint-plugin-prettier": "^3.0.0",
"gts": "^0.9.0",
"intelli-espower-loader": "^1.0.1",
"linkinator": "^1.1.2",
"mocha": "^6.0.0",
"post-install-check": "0.0.1",
"prettier": "^1.13.6",
"require-inject": "^1.4.3",
"proxyquire": "^2.1.0",
"source-map-support": "^0.5.6",
"teeny-request": "^3.6.0",
"typescript": "^3.0.0",
"uuid": "^3.3.2",
"winston": "^3.1.0",
"linkinator": "^1.1.2"
"winston": "^3.1.0"
},
"peerDependencies": {
"winston": "3.x"
"winston": ">=3.1.0"
}
}
1 change: 0 additions & 1 deletion src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as util from 'util';
import * as types from './types/core';

Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
import * as TransportStream from 'winston-transport';

import {LOGGING_TRACE_KEY as COMMON_TRACE_KEY, LoggingCommon} from './common';
import * as express from './middleware/express';
import * as types from './types/core';

// Export the express middleware as 'express'.
export {express};

type Callback = (err: Error, apiResponse: {}) => void;

/**
Expand Down
11 changes: 4 additions & 7 deletions system-test/logging-winston.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import * as types from '../src/types/core';

import {ErrorsApiTransport} from './errors-transport';

const inject = require('require-inject');
const proxyquire = require('proxyquire');

const winston = require('winston');

Expand Down Expand Up @@ -109,9 +109,7 @@ describe('LoggingWinston', function() {
] as TestData[]);

const LOG_NAME = logName('logging_winston_system_tests');
const LoggingWinston = inject('../src/index', {
winston,
}).LoggingWinston;
const LoggingWinston = proxyquire('../src/index', {winston}).LoggingWinston;

const logger = winston.createLogger({
transports: [new LoggingWinston({logName: LOG_NAME})],
Expand Down Expand Up @@ -143,9 +141,8 @@ describe('LoggingWinston', function() {
it('reports errors', async () => {
const start = Date.now();
const service = `logging-winston-system-test-winston3-${UUID}`;
const LoggingWinston = inject('../src/index', {
winston,
}).LoggingWinston;
const LoggingWinston =
proxyquire('../src/index', {winston}).LoggingWinston;

const logger = winston.createLogger({
transports: [new LoggingWinston(
Expand Down
Loading