Skip to content

Commit 22b3de8

Browse files
committed
docs(example-log-extension): update readme to reflect refactored code
- includes a review of the document
1 parent dedc93e commit 22b3de8

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

packages/example-log-extension/README.md

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,38 @@ This repository shows you how to use [@loopback/cli](https://github.com/stronglo
88
to write a complex logging extension that requires a [Component](http://loopback.io/doc/en/lb4/Using-components.html),
99
[Decorator](http://loopback.io/doc/en/lb4/Decorators.html), and a [Mixin](http://loopback.io/doc/en/lb4/Mixin.html).
1010

11-
To use the extension, load the component to get access to a `LogFn` that can be
12-
used in a sequence to log information. A Mixin allows you to set the
13-
application wide logLevel. Only Controller methods configured at or above the
11+
To use this extension you can add the `LogMixin` to your Application which will
12+
provide you a function to set the Application wide log level as well as automatically
13+
load the `LogComponent`. Only Controller methods configured at or above the
1414
logLevel will be logged.
1515

16+
_You may alternatively load `LogComponent` yourself and set the log level using
17+
the appropriate binding keys manually if you don't wish to use the `LogMixin`._
18+
1619
Possible levels are: DEBUG < INFO < WARN < ERROR < OFF
1720

1821
*Possible levels are represented as numbers but users can use `LOG_LEVEL.${level}`
1922
to specify the value instead of using numbers.*
2023

21-
A decorator enables you to provide metadata for Controller methods to set the
22-
minimum logLevel.
24+
A decorator enables you to set the log level for Controller methods, at or
25+
above which it should be logged.
2326

2427
### Example Usage
2528

2629
```ts
2730
import {
28-
LogLevelMixin,
29-
LogComponent,
31+
LogMixin,
3032
LOG_LEVEL,
3133
log
3234
} from 'loopback4-example-log-extension';
3335
// Other imports ...
3436

35-
class LogApp extends LogLevelMixin(RestApplication) {
36-
constructor() {
37-
super({
38-
logLevel: LOG_LEVEL.ERROR,
39-
});
40-
this.component(LogComponent);
41-
this.controller(MyController);
37+
class LogApp extends LogMixin(BootMixin(RestApplication)) {
38+
constructor(options?: ApplicationConfig) {
39+
super(options);
40+
41+
this.projectRoot = __dirname;
42+
this.logLevel(LOG_LEVEL.ERROR);
4243
};
4344
}
4445

@@ -109,8 +110,16 @@ export enum LOG_LEVEL {
109110
}
110111
```
111112

113+
112114
### `src/types.ts`
113-
Define TypeScript type definitions / interfaces for complex types and functions here.
115+
Before we continue, we will need to install a new dependecy as follows:
116+
117+
```shell
118+
npm i @loopback/rest
119+
```
120+
121+
Now we define TypeScript type definitions / interfaces for complex types and
122+
functions here.
114123

115124
```ts
116125
import {ParsedRequest, OperationArgs} from '@loopback/rest';
@@ -217,16 +226,18 @@ providing it via `ApplicationOptions` or using a helper method `app.logLevel(lev
217226
```ts
218227
import {Constructor} from '@loopback/context';
219228
import {EXAMPLE_LOG_BINDINGS} from '../keys';
229+
import {LogComponent} from '../component';
220230

221-
export function LogLevelMixin<T extends Constructor<any>>(superClass: T) {
231+
// tslint:disable-next-line:no-any
232+
export function LogMixin<T extends Constructor<any>>(superClass: T) {
222233
return class extends superClass {
234+
// tslint:disable-next-line:no-any
223235
constructor(...args: any[]) {
224236
super(...args);
225-
if (!this.options) this.options = {};
226-
227-
if (this.options.logLevel) {
237+
if (this.options && this.options.logLevel) {
228238
this.logLevel(this.options.logLevel);
229239
}
240+
this.component(LogComponent);
230241
}
231242

232243
logLevel(level: number) {
@@ -391,9 +402,10 @@ Package the providers in the component to their appropriate `Binding` keys so
391402
they are automatically bound when a user adds the component to their application.
392403

393404
```ts
394-
import {EXAMPLE_LOG_BINDINGS} from './keys';
395405
import {Component, ProviderMap} from '@loopback/core';
396-
import {TimerProvider, LogActionProvider} from './';
406+
import {EXAMPLE_LOG_BINDINGS} from './keys';
407+
import {LogActionProvider} from './providers/log-action.provider';
408+
import {TimerProvider} from './providers/timer.provider';
397409

398410
export class LogComponent implements Component {
399411
providers?: ProviderMap = {

0 commit comments

Comments
 (0)