Skip to content

Commit

Permalink
feat: altera nivel de log no request
Browse files Browse the repository at this point in the history
  • Loading branch information
leandroandrade committed Mar 19, 2024
1 parent f8560cf commit 0e6c664
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions change-log-level-header/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const Fastify = require('fastify');

(async () => {
const logLevels = [
'debug',
'error',
'fatal',
'info',
'silent',
'trace',
'warn',
];

const app = Fastify({
logger: {
level: 'debug',
},
disableRequestLogging: true,
});

app.addHook('onRequest', async (req, reply) => {
const logLevel = req.headers['x-log-level'];

if (logLevel && logLevels.includes(logLevel.toLowerCase())) {
req.log.level = logLevel.toLowerCase();
}
});

app.get('/', async (req, res) => {
req.log.info('should when log level is INFO');
return { hello: 'world' };
});

await app.listen({ port: 3000 });
})();

// curl -H 'x-log-level: error' http://localhost:3000

0 comments on commit 0e6c664

Please sign in to comment.