@@ -8,37 +8,38 @@ This repository shows you how to use [@loopback/cli](https://github.com/stronglo
8
8
to write a complex logging extension that requires a [ Component] ( http://loopback.io/doc/en/lb4/Using-components.html ) ,
9
9
[ Decorator] ( http://loopback.io/doc/en/lb4/Decorators.html ) , and a [ Mixin] ( http://loopback.io/doc/en/lb4/Mixin.html ) .
10
10
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
14
14
logLevel will be logged.
15
15
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
+
16
19
Possible levels are: DEBUG < INFO < WARN < ERROR < OFF
17
20
18
21
* Possible levels are represented as numbers but users can use ` LOG_LEVEL.${level} `
19
22
to specify the value instead of using numbers.*
20
23
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 .
23
26
24
27
### Example Usage
25
28
26
29
``` ts
27
30
import {
28
- LogLevelMixin ,
29
- LogComponent ,
31
+ LogMixin ,
30
32
LOG_LEVEL ,
31
33
log
32
34
} from ' loopback4-example-log-extension' ;
33
35
// Other imports ...
34
36
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 );
42
43
};
43
44
}
44
45
@@ -109,8 +110,16 @@ export enum LOG_LEVEL {
109
110
}
110
111
```
111
112
113
+
112
114
### ` 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.
114
123
115
124
``` ts
116
125
import {ParsedRequest , OperationArgs } from ' @loopback/rest' ;
@@ -217,16 +226,18 @@ providing it via `ApplicationOptions` or using a helper method `app.logLevel(lev
217
226
``` ts
218
227
import {Constructor } from ' @loopback/context' ;
219
228
import {EXAMPLE_LOG_BINDINGS } from ' ../keys' ;
229
+ import {LogComponent } from ' ../component' ;
220
230
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 ) {
222
233
return class extends superClass {
234
+ // tslint:disable-next-line:no-any
223
235
constructor (... args : any []) {
224
236
super (... args );
225
- if (! this .options ) this .options = {};
226
-
227
- if (this .options .logLevel ) {
237
+ if (this .options && this .options .logLevel ) {
228
238
this .logLevel (this .options .logLevel );
229
239
}
240
+ this .component (LogComponent );
230
241
}
231
242
232
243
logLevel(level : number ) {
@@ -391,9 +402,10 @@ Package the providers in the component to their appropriate `Binding` keys so
391
402
they are automatically bound when a user adds the component to their application.
392
403
393
404
``` ts
394
- import {EXAMPLE_LOG_BINDINGS } from ' ./keys' ;
395
405
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' ;
397
409
398
410
export class LogComponent implements Component {
399
411
providers? : ProviderMap = {
0 commit comments