From d979bd442e202528e963cf0bbc585a883db30787 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Mon, 10 Oct 2016 15:17:16 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20=20Add=20support=20for=20short?= =?UTF-8?q?=20one-line=20log=20format=20(#7523)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refs #7116 - The long format logs were making it hard to see the debug statements - Seeing headers for every asset is a bit much - "short" format doesn't output bodyPretty - This needs love as there's no reason to calculate bodyPretty if we aren't using it - The default output should be discussed - req.url doesn't include the full path when apps are mounted - Use mode instead of format & pass config correctly --- core/server/logging/GhostLogger.js | 2 +- core/server/logging/PrettyStream.js | 42 ++++++++++++++++------------- core/server/logging/index.js | 2 +- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/core/server/logging/GhostLogger.js b/core/server/logging/GhostLogger.js index 88706711d3..b710e33426 100644 --- a/core/server/logging/GhostLogger.js +++ b/core/server/logging/GhostLogger.js @@ -113,7 +113,7 @@ GhostLogger.prototype.setStreams = function setStreams() { } if (transport === 'stdout') { - prettyStdOut = new GhostPrettyStream(); + prettyStdOut = new GhostPrettyStream({mode: self.mode}); prettyStdOut.pipe(process.stdout); streams.push({ diff --git a/core/server/logging/PrettyStream.js b/core/server/logging/PrettyStream.js index 5e0892563c..bcf4fffb6a 100644 --- a/core/server/logging/PrettyStream.js +++ b/core/server/logging/PrettyStream.js @@ -40,7 +40,8 @@ var _ = require('lodash'), }; -function PrettyStream() { +function PrettyStream(options) { + this.mode = options.mode || 'short'; } util.inherits(PrettyStream, Stream); @@ -59,7 +60,8 @@ PrettyStream.prototype.write = function write(data) { } } - var body = {}, + var output = '', + body = {}, time = moment(data.time).format('YYYY-MM-DD HH:mm:ss'), logLevel = __private__.levelFromName[data.level].toUpperCase(), codes = __private__.colors[__private__.colorForLevel[data.level]], @@ -74,12 +76,11 @@ PrettyStream.prototype.write = function write(data) { if (data.req && data.res) { _.each(data.req, function (value, key) { if (['headers', 'query', 'body'].indexOf(key) !== -1 && !_.isEmpty(value)) { - bodyPretty += colorize('yellow', key.toUpperCase()) + '\n'; + bodyPretty += '\n' + colorize('yellow', key.toUpperCase()) + '\n'; bodyPretty += prettyjson.render(value, {}) + '\n'; } }); - bodyPretty += '\n'; if (data.err) { if (data.err.level) { bodyPretty += colorize('yellow', 'ERROR (' + data.err.level + ')') + '\n'; @@ -100,7 +101,7 @@ PrettyStream.prototype.write = function write(data) { } if (key === 'level') { - bodyPretty += colorize('underline', key + ':' + value) + '\n\n'; + bodyPretty += colorize('underline', key + ':' + value) + '\n'; } else if (key === 'message') { bodyPretty += colorize('red', value) + '\n'; @@ -122,27 +123,31 @@ PrettyStream.prototype.write = function write(data) { try { if (data.req && data.res) { - this.emit('data', format('[%s] %s --> %s %s (%s) \n%s\n\n', + output += format('[%s] %s %s %s (%s)\n', time, logLevel, data.req.method, - data.req.url, - data.res.statusCode, - colorize('grey', bodyPretty) - )); + data.req.originalUrl, + data.res.statusCode + ); + } else if (data.err) { - this.emit('data', format('[%s] %s \n%s\n\n', + output += format('[%s] %s\n', time, - logLevel, - colorize('grey', bodyPretty) - )); + logLevel + ); } else { - this.emit('data', format('[%s] %s %s\n', + output += format('[%s] %s\n', time, - logLevel, - colorize('grey', bodyPretty) - )); + logLevel + ); } + + if (this.mode !== 'short' && bodyPretty) { + output += format('%s\n', colorize('grey', bodyPretty)); + } + + this.emit('data', output); } catch (err) { this.emit('data', err); } @@ -150,5 +155,4 @@ PrettyStream.prototype.write = function write(data) { return true; }; - module.exports = PrettyStream; diff --git a/core/server/logging/index.js b/core/server/logging/index.js index a4feb50333..90c62d8003 100644 --- a/core/server/logging/index.js +++ b/core/server/logging/index.js @@ -2,7 +2,7 @@ var config = require('../config'), GhostLogger = require('./GhostLogger'), adapter = new GhostLogger({ env: config.get('env'), - mode: process.env.NODE_MODE, + mode: process.env.NODE_MODE || config.get('logging:mode'), level: process.env.NODE_LEVEL || config.get('logging:level'), transports: config.get('logging:transports'), rotation: config.get('logging:rotation'),