Comparing version 0.11.1 to 0.12.0
@@ -6,3 +6,3 @@ <h1 align="center">Fastify</h1> | ||
By default Fastify uses [pino-http](https://github.com/pinojs/pino-http) as logger, with the log level setted to `'fatal'`. | ||
By default Fastify uses [pino-http](https://github.com/pinojs/pino-http) as the logger, with the log level set to `'fatal'`. | ||
@@ -27,2 +27,21 @@ If you want to pass some options to the logger, just pass the logger option to Fastify. | ||
``` | ||
You can also supply your own logger instance. Instead of passing configuration options, simply pass the instance. | ||
The logger you supply must conform to the Pino interface; that is, it must have the following methods: | ||
`info`, `error`, `debug`, `fatal`, `warn`, `trace`, `child`. | ||
Example: | ||
```js | ||
const log = require('pino')({ level: 'info' }) | ||
const fastify = require('fastify')({ logger: log }) | ||
log.info('does not have request information') | ||
fastify.get('/', function (req, reply) { | ||
req.log.info('includes request information, but is the same logger instance as `log`') | ||
reply.send({ hello: 'world' }) | ||
}) | ||
``` | ||
*The logger instance for the current request is available in every part of the [lifecycle](https://github.com/fastify/fastify/blob/master/docs/Lifecycle.md).* |
@@ -17,2 +17,3 @@ 'use strict' | ||
const hooksManager = require('./lib/hooks') | ||
const isValidLogger = require('./lib/validation').isValidLogger | ||
@@ -25,5 +26,10 @@ function build (options) { | ||
options.logger = options.logger || {} | ||
options.logger.level = options.logger.level || 'fatal' | ||
const logger = pinoHttp(options.logger) | ||
var logger | ||
if (options.logger && isValidLogger(options.logger)) { | ||
logger = pinoHttp({logger: options.logger}) | ||
} else { | ||
options.logger = options.logger || {} | ||
options.logger.level = options.logger.level || 'fatal' | ||
logger = pinoHttp(options.logger) | ||
} | ||
@@ -30,0 +36,0 @@ const router = wayfarer('/404') |
@@ -68,3 +68,15 @@ 'use strict' | ||
module.exports = { build, validate, serialize } | ||
function isValidLogger (logger) { | ||
var result = true | ||
const methods = ['info', 'error', 'debug', 'fatal', 'warn', 'trace', 'child'] | ||
for (var i = 0; i < methods.length; i += 1) { | ||
if (!logger[methods[i]] || typeof logger[methods[i]] !== 'function') { | ||
result = false | ||
break | ||
} | ||
} | ||
return result | ||
} | ||
module.exports = { build, validate, serialize, isValidLogger } | ||
module.exports.symbols = { payloadSchema, querystringSchema, outputSchema, paramsSchema } |
{ | ||
"name": "fastify", | ||
"version": "0.11.1", | ||
"version": "0.12.0", | ||
"description": "Fast and low overhead web framework, for Node.js", | ||
@@ -48,2 +48,3 @@ "main": "fastify.js", | ||
"koa": "^2.1.0", | ||
"pino": "^4.0.3", | ||
"pre-commit": "^1.2.2", | ||
@@ -50,0 +51,0 @@ "request": "^2.81.0", |
@@ -25,3 +25,3 @@ 'use strict' | ||
t.ok(req.log) | ||
reply.send(null, 200, { hello: 'world' }) | ||
reply.send({ hello: 'world' }) | ||
}) | ||
@@ -46,1 +46,41 @@ | ||
}) | ||
test('can use external logger instance', t => { | ||
t.plan(7) | ||
const lines = [] | ||
const splitStream = split(JSON.parse) | ||
splitStream.on('data', (line) => { | ||
lines.push(line) | ||
}) | ||
splitStream.on('end', () => { | ||
t.is(lines.length, 4) | ||
t.is(lines[0].msg, 'log success') | ||
t.is(lines[1].msg, 'log success') | ||
t.is(lines[2].msg, 'log success') | ||
t.is(lines[3].msg, 'request completed') | ||
}) | ||
const logger = require('pino')(splitStream) | ||
logger.info('log success') | ||
const localFastify = Fastify({logger: logger}) | ||
localFastify.get('/foo', function (req, reply) { | ||
t.ok(req.log) | ||
req.log.info('log success') | ||
reply.send({ hello: 'world' }) | ||
setImmediate(() => { | ||
localFastify.server.close(() => { | ||
splitStream.end() | ||
localFastify.server.unref() | ||
}) | ||
}) | ||
}) | ||
localFastify.listen(0, err => { | ||
t.error(err) | ||
logger.info('log success') | ||
http.get('http://localhost:' + localFastify.server.address().port + '/foo') | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
105270
2862
22