Skip to content

Commit

Permalink
debug: Add debug log informations
Browse files Browse the repository at this point in the history
  • Loading branch information
Atinux committed Dec 8, 2017
1 parent f900e33 commit 0bab8f8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/conf.js
Original file line number Diff line number Diff line change
@@ -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')

Expand Down Expand Up @@ -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 = [
Expand Down
9 changes: 7 additions & 2 deletions lib/http.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/jwt.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const debug = require('debug')('mono:jwt')
const jsonwebtoken = require('jsonwebtoken')

const HttpError = require('./http-error')
Expand Down Expand Up @@ -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 })
Expand Down
2 changes: 2 additions & 0 deletions lib/log.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const debug = require('debug')('mono:log')
const winston = require('winston')

class MonoLog {
Expand All @@ -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)
Expand Down

0 comments on commit 0bab8f8

Please sign in to comment.