From 0bab8f8912ba166a28b811b00ad00ca970b2b11f Mon Sep 17 00:00:00 2001 From: Atinux Date: Fri, 8 Dec 2017 16:26:44 +0100 Subject: [PATCH] debug: Add debug log informations --- lib/conf.js | 3 +++ lib/http.js | 9 +++++++-- lib/jwt.js | 3 +++ lib/log.js | 2 ++ 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/conf.js b/lib/conf.js index afd7938..16cf064 100644 --- a/lib/conf.js +++ b/lib/conf.js @@ -1,4 +1,6 @@ const { join, dirname } = require('path') + +const debug = require('debug')('mono:conf') const { isArray, isObject, isRegExp, mergeWith, isUndefined } = require('lodash') const clearModule = require('clear-module') @@ -59,6 +61,7 @@ module.exports = function (dir, { log, pkg, appDir }) { // Conf path const confPath = process.env.MONO_CONF_PATH || join(dir, 'conf') + debug(`Loading conf from ${confPath}`) // Files to load const files = [ diff --git a/lib/http.js b/lib/http.js index be59c68..9b2bcb2 100644 --- a/lib/http.js +++ b/lib/http.js @@ -1,6 +1,7 @@ const { createServer } = require('http') -const chalk = require('chalk') +const debug = require('debug')('mono:http') +const chalk = require('chalk') const express = require('express') const morgan = require('morgan') const helmet = require('helmet') @@ -9,7 +10,7 @@ module.exports = async function ({ conf, log }) { const httpOptions = conf.mono.http // Default options const port = httpOptions.port = parseInt(process.env.PORT, 10) || httpOptions.port || 8000 - httpOptions.logLevel = (typeof httpOptions.logLevel !== 'undefined' ? httpOptions.logLevel : 'dev') + httpOptions.logLevel = (typeof httpOptions.logLevel !== 'undefined' ? httpOptions.logLevel : (conf.env === 'production' ? 'combined' : 'dev')) httpOptions.host = process.env.HOST || httpOptions.host || 'localhost' httpOptions.bodyParser = httpOptions.bodyParser || {} // Create server & helpers @@ -51,11 +52,15 @@ module.exports = async function ({ conf, log }) { }) } // Middleware + debug(`Helmet ${httpOptions.helmet !== false ? 'activated' : 'disabled'}`) if (httpOptions.helmet !== false) app.use(helmet(httpOptions.helmet)) + debug(`Body parser (url encoded) ${httpOptions.bodyParser.urlencoded !== false ? 'activated' : 'disabled'}`) if (httpOptions.bodyParser.urlencoded !== false) app.use(express.urlencoded(Object.assign({ extended: false }, httpOptions.bodyParser.urlencoded))) + debug(`Body parser (json) ${httpOptions.bodyParser.json !== false ? 'activated' : 'disabled'}`) if (httpOptions.bodyParser.json !== false) app.use(express.json(httpOptions.bodyParser.json)) // Logger middleware if (typeof httpOptions.logLevel === 'string') { + debug(`HTTP log level: ${httpOptions.logLevel}`) app.use(morgan(httpOptions.logLevel, { stream: log.stream })) } // Return app & server diff --git a/lib/jwt.js b/lib/jwt.js index 07231a6..5fdfe87 100644 --- a/lib/jwt.js +++ b/lib/jwt.js @@ -1,3 +1,4 @@ +const debug = require('debug')('mono:jwt') const jsonwebtoken = require('jsonwebtoken') const HttpError = require('./http-error') @@ -69,6 +70,8 @@ module.exports = function (options) { options.headerKey = options.headerKey || 'Authorization' options.secret = options.secret || 'secret' options.expiresIn = options.expiresIn || '7d' + debug(`JWT header key: ${options.headerKey}`) + debug(`JWT duration: ${options.expiresIn}`) // Bind jwt methods with options module.exports.jwt.generateJWT = generateJWT.bind({ jwt: options }) diff --git a/lib/log.js b/lib/log.js index 317d955..97640e0 100644 --- a/lib/log.js +++ b/lib/log.js @@ -1,3 +1,4 @@ +const debug = require('debug')('mono:log') const winston = require('winston') class MonoLog { @@ -12,6 +13,7 @@ class MonoLog { this.options.files = this.options.files || [] this.options.http = this.options.http || [] this.options.transports = this.options.transports || [] + debug(`Log level: ${this.level}`) // Logger this.log = this.createLogger(this.level)