0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00
ghost/core/server/logging/PrettyStream.js

155 lines
4.3 KiB
JavaScript
Raw Normal View History

🎨 configurable logging with bunyan (#7431) - 🛠 add bunyan and prettyjson, remove morgan - ✨ add logging module - GhostLogger class that handles setup of bunyan - PrettyStream for stdout - ✨ config for logging - @TODO: testing level fatal? - ✨ log each request via GhostLogger (express middleware) - @TODO: add errors to output - 🔥 remove errors.updateActiveTheme - we can read the value from config - 🔥 remove 15 helper functions in core/server/errors/index.js - all these functions get replaced by modules: 1. logging 2. error middleware handling for html/json 3. error creation (which will be part of PR #7477) - ✨ add express error handler for html/json - one true error handler for express responses - contains still some TODO's, but they are not high priority for first implementation/integration - this middleware only takes responsibility of either rendering html responses or return json error responses - 🎨 use new express error handler in middleware/index - 404 and 500 handling - 🎨 return error instead of error message in permissions/index.js - the rule for error handling should be: if you call a unit, this unit should return a custom Ghost error - 🎨 wrap serve static module - rule: if you call a module/unit, you should always wrap this error - it's always the same rule - so the caller never has to worry about what comes back - it's always a clear error instance - in this case: we return our notfounderror if serve static does not find the resource - this avoid having checks everywhere - 🎨 replace usages of errors/index.js functions and adapt tests - use logging.error, logging.warn - make tests green - remove some usages of logging and throwing api errors -> because when a request is involved, logging happens automatically - 🐛 return errorDetails to Ghost-Admin - errorDetails is used for Theme error handling - 🎨 use 500er error for theme is missing error in theme-handler - 🎨 extend file rotation to 1w
2016-10-04 17:33:43 +02:00
// jscs:disable
var _ = require('lodash'),
moment = require('moment'),
Stream = require('stream').Stream,
util = require('util'),
format = util.format,
prettyjson = require('prettyjson'),
__private__ = {
levelFromName: {
10: 'trace',
20: 'debug',
30: 'info',
40: 'warn',
50: 'error',
60: 'fatal'
},
colorForLevel: {
10: 'grey',
20: 'grey',
30: 'cyan',
40: 'magenta',
50: 'red',
60: 'inverse'
},
colors: {
'bold': [1, 22],
'italic': [3, 23],
'underline': [4, 24],
'inverse': [7, 27],
'white': [37, 39],
'grey': [90, 39],
'black': [30, 39],
'blue': [34, 39],
'cyan': [36, 39],
'green': [32, 39],
'magenta': [35, 39],
'red': [31, 39],
'yellow': [33, 39]
}
};
function PrettyStream() {
}
util.inherits(PrettyStream, Stream);
function colorize(color, value) {
return '\x1B[' + __private__.colors[color][0] + 'm' + value + '\x1B[' + __private__.colors[color][1] + 'm';
}
PrettyStream.prototype.write = function write(data) {
if (typeof data === 'string') {
try {
data = JSON.parse(data);
} catch (err) {
this.emit('data', err);
}
}
var 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]],
bodyPretty = '';
logLevel = '\x1B[' + codes[0] + 'm' + logLevel + '\x1B[' + codes[1] + 'm';
if (data.msg) {
body.msg = data.msg;
}
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 += prettyjson.render(value, {}) + '\n';
}
});
bodyPretty += '\n';
if (data.err) {
if (data.err.level) {
bodyPretty += colorize('yellow', 'ERROR (' + data.err.level + ')') + '\n';
} else {
bodyPretty += colorize('yellow', 'ERROR\n');
}
_.each(data.err, function (value, key) {
if (['message', 'context', 'help', 'stack'].indexOf(key) !== -1 && !_.isEmpty(value)) {
bodyPretty += value + '\n';
}
});
}
} else if (data.err) {
_.each(data.err, function (value, key) {
if (_.isEmpty(value)) {
return;
}
if (key === 'level') {
bodyPretty += colorize('underline', key + ':' + value) + '\n\n';
}
else if (key === 'message') {
bodyPretty += colorize('red', value) + '\n';
}
else if (key === 'context') {
bodyPretty += colorize('white', value) + '\n';
}
else if (key === 'help') {
bodyPretty += colorize('yellow', value) + '\n';
}
else if (key === 'stack' && !data.err['hideStack']) {
bodyPretty += colorize('white', value) + '\n';
}
});
} else {
// print string
bodyPretty += data.msg;
}
try {
if (data.req && data.res) {
this.emit('data', format('[%s] %s --> %s %s (%s) \n%s\n\n',
time,
logLevel,
data.req.method,
data.req.url,
data.res.statusCode,
colorize('grey', bodyPretty)
));
} else if (data.err) {
this.emit('data', format('[%s] %s \n%s\n\n',
time,
logLevel,
colorize('grey', bodyPretty)
));
} else {
this.emit('data', format('[%s] %s %s\n',
time,
logLevel,
colorize('grey', bodyPretty)
));
}
} catch (err) {
this.emit('data', err);
}
return true;
};
module.exports = PrettyStream;