Skip to content

Commit

Permalink
Refactor log level configuration (#7695)
Browse files Browse the repository at this point in the history
* Refactor log level configuration

- Remove STRAPI_LOG_LEVEL environment variable related redundant code.
- Add helpful error message to the current log level configuration
  system.

Signed-off-by: Juho Viitasalo <juho.viitasalo@lildrop.com>

* Fix error message

Signed-off-by: Juho Viitasalo <juho.viitasalo@lildrop.com>

* Fix log level base value

* Fix breaking change

* Add debug message

* Change logger code to use Pino's log level values directly

* Update index.js

Co-authored-by: Alexandre BODIN <alexandrebodin@users.noreply.github.com>
  • Loading branch information
jiv-e and alexandrebodin committed Oct 8, 2020
1 parent 1cab03f commit 6fb0a63
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
23 changes: 16 additions & 7 deletions packages/strapi-utils/lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,25 @@
const pino = require('pino');
const _ = require('lodash');

const logLevels = ['fatal', 'error', 'warn', 'info', 'debug', 'trace'];
const logLevels = Object.keys(pino.levels.values);

function getLogLevel() {
if (
_.isString(process.env.STRAPI_LOG_LEVEL) &&
_.includes(logLevels, process.env.STRAPI_LOG_LEVEL.toLowerCase())
) {
return process.env.STRAPI_LOG_LEVEL;
if (!_.isString(process.env.STRAPI_LOG_LEVEL)) {
// Default value.
return 'debug';
}
return 'debug';

const logLevel = process.env.STRAPI_LOG_LEVEL.toLowerCase();

if (!_.includes(logLevels, logLevel)) {
throw new Error(
"Invalid log level set in STRAPI_LOG_LEVEL environment variable. Accepted values are: '" +
logLevels.join("', '") +
"'."
);
}

return logLevel;
}

function getBool(envVar, defaultValue) {
Expand Down
21 changes: 19 additions & 2 deletions packages/strapi/lib/middlewares/logger/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
const chalk = require('chalk');
const _ = require('lodash');

const codeToColor = code => {
return code >= 500
Expand All @@ -25,14 +26,30 @@ module.exports = strapi => {
initialize() {
const { level, exposeInContext, requests } = strapi.config.middleware.settings.logger;

if (level) {
strapi.log.level = level;
const logLevels = Object.keys(strapi.log.levels.values);

if (!_.includes(logLevels, level)) {
throw new Error(
"Invalid log level set in middleware configuration. Accepted values are: '" +
logLevels.join("', '") +
"'."
);
}

strapi.log.level = level;

if (exposeInContext) {
strapi.app.context.log = strapi.log;
}

const isLogLevelEnvVariableSet = _.isString(process.env.STRAPI_LOG_LEVEL);

if (isLogLevelEnvVariableSet && strapi.log.levelVal <= 20) {
strapi.log.debug(
`STRAPI_LOG_LEVEL environment variable is overridden by logger middleware. It only applies outside Strapi's middleware context.`
);
}

if (requests && strapi.log.levelVal <= 20) {
strapi.app.use(async (ctx, next) => {
const start = Date.now();
Expand Down

0 comments on commit 6fb0a63

Please sign in to comment.