@malijs/logger
v0.6.0
Published
Development logging middleware for Mali
Downloads
453
Readme
@malijs/logger
Development style logger middleware for Mali.
--> 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
Installation
$ npm install @malijs/logger
Example
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')
API
logger(options)
options.fullName
To log full name (fullName
) from context, otherwise logs just the name
. Default: false
.
app.use(logger({ fullName: true }))
Output:
--> /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.
There are predefined timestamp functions: epochTime
(default), unixTime
, and isoTime
.
Default timestamp:
app.use(logger({timestamp: true}))
Output:
--> 1556325859071 GetFeature unary
<-- 1556325859071 GetFeature unary 6ms
With custom predefined timestamp:
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:
app.use(logger({ timestamp: date => `${date.toDateString()} ${date.toLocaleTimeString()}` }))
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 whentimestamp: 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.
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.
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:
app.use(logger({
request: req => `(${req.latitude}, ${req.longitude})`,
response: res => `[${res.name}]`
}))
Output:
--> GetFeature (409146138, -746188906) unary
<-- GetFeature [Berkshire Valley Management Area Trail, Jefferson, NJ, USA] unary 4ms
Notes
Recommended that you .use()
this middleware near the top to "wrap" all subsequent middleware.
License
Apache 2.0