Skip to content

Commit c612a3b

Browse files
committed
feat(extension-logging): add http access and method invocation logging
- add architecture diagram and examples to README - improve naming and exporting - configure the http access logger for REST routes only
1 parent d786a7f commit c612a3b

File tree

19 files changed

+721
-667
lines changed

19 files changed

+721
-667
lines changed

acceptance/extension-logging-fluentd/package-lock.json

Lines changed: 27 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

acceptance/extension-logging-fluentd/src/__tests__/accpetance/fluent.acceptance.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ describe('LoggingComponent', () => {
5757
async function givenAppWithCustomConfig() {
5858
app = givenApplication();
5959
app.configure(LoggingBindings.FLUENT_SENDER).to({
60-
host: process.env.FLUENTD_SERVICE_HOST || '127.0.0.1',
61-
port: +(process.env.FLUENTD_SERVICE_PORT_TCP || 0) || 24224,
60+
host: process.env.FLUENTD_SERVICE_HOST ?? '127.0.0.1',
61+
port: +(process.env.FLUENTD_SERVICE_PORT_TCP ?? 24224),
6262
timeout: 3.0,
6363
reconnectInterval: 600000, // 10 minutes
6464
});

acceptance/extension-logging-fluentd/src/__tests__/fixtures/fluentd.docker.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {GenericContainer, StartedTestContainer} from 'testcontainers';
1010
export const ROOT_DIR = path.join(__dirname, '../../../fixtures');
1111
export const ETC_DIR = path.join(ROOT_DIR, 'etc');
1212

13-
/* eslint-disable require-atomic-updates */
1413
async function startFluentd() {
1514
if (process.env.FLUENTD_SERVICE_HOST != null) return;
1615
const container = await new GenericContainer(

extensions/logging/README.md

Lines changed: 89 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ This module contains a component provides logging facilities based on
1111
> using `0.x.y` versions. Their APIs and functionality may be subject to
1212
> breaking changes in future releases.
1313
14+
## Architecture overview
15+
16+
![logging-component](logging-component.png)
17+
1418
## Installation
1519

1620
```sh
@@ -34,21 +38,67 @@ In the constructor, add the component to your application:
3438
this.component(LoggingComponent);
3539
```
3640

37-
The component contributes bindings with keys listed below:
41+
Now your application can add a controller as follows to leverage the logging
42+
facilities:
43+
44+
```ts
45+
import {inject} from '@loopback/context';
46+
import {Logger, logInvocation} from '@loopback/extension-logging';
47+
import {get, param} from '@loopback/rest';
48+
49+
class MyController {
50+
// Inject a winston logger
51+
@inject(LoggingBindings.WINSTON_LOGGER)
52+
private logger: Logger;
53+
54+
// http access is logged by a global interceptor
55+
@get('/greet/{name}')
56+
// log the `greet` method invocations
57+
@logInvocation()
58+
greet(@param.path.string('name') name: string) {
59+
return `Hello, ${name}`;
60+
}
61+
62+
@get('/hello/{name}')
63+
hello(@param.path.string('name') name: string) {
64+
// Use the winston logger explicitly
65+
this.logger.log('info', `greeting ${name}`);
66+
return `Hello, ${name}`;
67+
}
68+
}
69+
```
70+
71+
## Configure the logging component
72+
73+
The logging component can be configured as follows:
74+
75+
```ts
76+
app.configure(LoggingBindings.COMPONENT).to({
77+
enableFluent: false, // default to true
78+
enableHttpAccessLog: true, // default to true
79+
});
80+
```
81+
82+
The component contributes bindings with keys declared in `LoggingBindings`
83+
namespace below:
3884

39-
- LoggingBindings.FLUENT_SENDER - A fluent sender
40-
- LoggingBindings.WINSTON_LOGGER - A winston logger
41-
- LoggingBindings.WINSTON_TRANSPORT_FLUENT - A fluent transport for winston
85+
- FLUENT_SENDER - A fluent sender
86+
- WINSTON_LOGGER - A winston logger
87+
- WINSTON_TRANSPORT_FLUENT - A fluent transport for winston
88+
- WINSTON_INTERCEPTOR - A local interceptor set by `@logInvocation` to log
89+
method invocations
90+
- WINSTON_HTTP_ACCESS_LOGGER - A global interceptor that logs http access with
91+
[Morgan](https://github.com/expressjs/morgan) format
4292

4393
The fluent sender and transport for winston can be configured against
44-
`LoggingBindings.FLUENT_SENDER`:
94+
`FLUENT_SENDER`:
4595

4696
```ts
4797
import {LoggingBindings} from '@loopback/extension-logging';
4898

4999
app.configure(LoggingBindings.FLUENT_SENDER).to({
50-
host: process.env.FLUENTD_SERVICE_HOST || 'localhost',
51-
port: +(process.env.FLUENTD_SERVICE_PORT_TCP || 0) || 24224,
100+
host: process.env.FLUENTD_SERVICE_HOST ?? 'localhost',
101+
port: +(process.env.FLUENTD_SERVICE_PORT_TCP ?? 24224),
52102
timeout: 3.0,
53103
reconnectInterval: 600000, // 10 minutes
54104
});
@@ -75,9 +125,17 @@ points:
75125
```ts
76126
import {extensionFor} from '@loopback/core';
77127
import {format} from 'winston';
78-
import {WINSTON_FORMAT, WINSTON_TRANSPORT} from '@loopback/extension-logging';
79-
80-
const myFormat: Format = ...;
128+
import {
129+
WINSTON_FORMAT,
130+
WINSTON_TRANSPORT,
131+
WinstonFormat,
132+
WinstonTransports,
133+
} from '@loopback/extension-logging';
134+
135+
const myFormat: WinstonFormat = format((info, opts) => {
136+
console.log(info);
137+
return false;
138+
})();
81139

82140
ctx
83141
.bind('logging.winston.formats.myFormat')
@@ -87,6 +145,27 @@ ctx
87145
.bind('logging.winston.formats.colorize')
88146
.to(format.colorize())
89147
.apply(extensionFor(WINSTON_FORMAT));
148+
149+
const consoleTransport = new WinstonTransports.Console({
150+
level: 'info',
151+
format: format.combine(format.colorize(), format.simple()),
152+
});
153+
ctx
154+
.bind('logging.winston.transports.console')
155+
.to(consoleTransport)
156+
.apply(extensionFor(WINSTON_TRANSPORT));
157+
```
158+
159+
If no transport is contributed, the winston logger uses the
160+
[console](https://github.com/winstonjs/winston/blob/master/docs/transports.md#console-transport).
161+
162+
The access log interceptor can also be configured to customize
163+
[Morgan format and options](https://github.com/expressjs/morgan#morganformat-options):
164+
165+
```ts
166+
ctx
167+
.configure(LoggingBindings.WINSTON_HTTP_ACCESS_LOGGER)
168+
.to({format: 'combined'});
90169
```
91170

92171
## Contributions
339 KB
Loading

0 commit comments

Comments
 (0)