@@ -11,6 +11,10 @@ This module contains a component provides logging facilities based on
11
11
> using ` 0.x.y ` versions. Their APIs and functionality may be subject to
12
12
> breaking changes in future releases.
13
13
14
+ ## Architecture overview
15
+
16
+ ![ logging-component] ( logging-component.png )
17
+
14
18
## Installation
15
19
16
20
``` sh
@@ -34,21 +38,67 @@ In the constructor, add the component to your application:
34
38
this .component (LoggingComponent );
35
39
```
36
40
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:
38
84
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
42
92
43
93
The fluent sender and transport for winston can be configured against
44
- ` LoggingBindings. FLUENT_SENDER` :
94
+ ` FLUENT_SENDER ` :
45
95
46
96
``` ts
47
97
import {LoggingBindings } from ' @loopback/extension-logging' ;
48
98
49
99
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 ) ,
52
102
timeout: 3.0 ,
53
103
reconnectInterval: 600000 , // 10 minutes
54
104
});
@@ -75,9 +125,17 @@ points:
75
125
``` ts
76
126
import {extensionFor } from ' @loopback/core' ;
77
127
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
+ })();
81
139
82
140
ctx
83
141
.bind (' logging.winston.formats.myFormat' )
87
145
.bind (' logging.winston.formats.colorize' )
88
146
.to (format .colorize ())
89
147
.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' });
90
169
```
91
170
92
171
## Contributions
0 commit comments