Development logging middleware for Mali
npm install @malijs/logger
!Tests
Development style logger middleware for Mali.
``sh`
--> GetFeature unary
--> GetFeature unary
<-- GetFeature unary 22ms
<-- GetFeature unary 32ms
--> ListFeatures response_stream
<-- ListFeatures response_stream 21ms
--> RecordRoute request_stream
<-- RecordRoute request_stream 10s
--> RouteChat duplex
<-- RouteChat duplex 10ms
`js`
$ npm install @malijs/logger
`js
import logger from '@malijs/logger'
import Mali from 'mali'
const app = new Mali(path.resolve(__dirname, './helloworld.proto'), 'Greeter')
app.use(logger())
app.use({
sayHello: ({ res, req }) => (res = Hello ${req.name})`
})
app.start('0.0.0.0:50051')
#### options.fullName
To log full name (fullName) from context, otherwise logs just the name. Default: false.
`js`
app.use(logger({ fullName: true }))
Output:
`sh`
--> /routeguide.RouteGuide/GetFeature unary
<-- /routeguide.RouteGuide/GetFeature unary 22ms
#### options.timestamp
Enables or disables the inclusion of a timestamp in the log message.
If a function is supplied, it is passed a Date timestamp and it must synchronously return the string representation to be logged.epochTime
There are predefined timestamp functions: (default), unixTime, and isoTime.
Default timestamp:
`js`
app.use(logger({timestamp: true}))
Output:
``
--> 1556325859071 GetFeature unary
<-- 1556325859071 GetFeature unary 6ms
With custom predefined timestamp:
`js`
app.use(logger({timestamp: logger.isoTime}))
Output:
``
--> 2019-04-27T00:44:19.083Z GetFeature unary
<-- 2019-04-27T00:44:19.083Z GetFeature unary 4ms
With custom timestamp function:
`js${date.toDateString()} ${date.toLocaleTimeString()}
app.use(logger({ timestamp: date => }))`
Output:
``
--> Fri Apr 26 2019 9:44:19 PM GetFeature unary
<-- Fri Apr 26 2019 9:44:19 PM GetFeature unary 18ms
#### Timestamp functions
- logger.epochTime - Milliseconds since Unix epoch. Default. Used when timestamp: true.logger.unixTime
- - Seconds since Unix epoch.logger.isoTime
- - Timestamp in ISO time.
#### options.request
Enables or disables the inclusion of request in the log message.
By default JSON.stringify is used. If a function is supplied it is passed the request from the context.
`js`
app.use(logger({ request: true }))
Output:
``
--> GetFeature {"latitude":409146138,"longitude":-746188906} unary
<-- GetFeature unary 2ms
#### options.response
Enables or disables the inclusion of response in the log message.
By default JSON.stringify is used. If a function is supplied it is passed the response from the context.
`js`
app.use(logger({ response: true }))
Output:
``
--> GetFeature unary
<-- GetFeature {"location":{"latitude":409146138,"longitude":-746188906},"name":"Berkshire Valley Management Area Trail, Jefferson, NJ, USA"} unary 3ms
With custom request and response logging functions:
`js(${req.latitude}, ${req.longitude})
app.use(logger({
request: req => ,[${res.name}]
response: res => `
}))
Output:
``
--> GetFeature (409146138, -746188906) unary
<-- GetFeature [Berkshire Valley Management Area Trail, Jefferson, NJ, USA] unary 4ms
Recommended that you .use()` this middleware near the top to "wrap" all subsequent middleware.
Apache 2.0