@@ -8,8 +8,6 @@ const format = require('json-format');
88
99const { isObjectEmpty, safeStringify } = require ( './utils' ) ;
1010
11- const pretty = ( module . exports = { } ) ;
12-
1311/** @const {Object} LEVELS_CONFIG Levels colors and icons */
1412const LEVELS_CONFIG = {
1513 emergency : {
@@ -46,62 +44,128 @@ const LEVELS_CONFIG = {
4644 } ,
4745} ;
4846
47+ /**
48+ * Keys to ignore on context object when pretty printing
49+ *
50+ * @const {Array<string>} CONTEXT_IGNORE_KEYS
51+ */
52+ const CONTEXT_IGNORE_KEYS = [ 'runtime' , 'source' , 'system' ] ;
53+
54+ /**
55+ * Keys to ignore on context object when pretty printing
56+ *
57+ * @const {Array<string>} EVENT_IGNORE_KEYS
58+ */
59+ const EVENT_IGNORE_KEYS = [ 'http_request' ] ;
60+
4961/**
5062 * Write prettified log to selected output.
5163 *
5264 * @param {Object } logContext The log context to write.
53- * @param {boolean|Object } prettyOpts Possible pretty print options.
65+ * @param {Object= } prettyOpts Rendering options.
66+ * @param {boolean= } prettyOpts.noTimestamp Set to true to not log timestamps.
67+ * @param {boolean= } prettyOpts.noFilename Set to true to not log filename.
68+ * @param {boolean= } prettyOpts.onlyMessage Set to true to only log the message
69+ * and not the context.
70+ * @param {boolean= } prettyOpts.noColor Do not color the logs.
5471 * @return {string } Formatted output.
5572 * @private
5673 */
57- pretty . writePretty = function ( logContext , prettyOpts ) {
58- // current level icon and color
59- const config = LEVELS_CONFIG [ logContext . level ] ;
74+ exports . writePretty = function ( logContext , prettyOpts = { } ) {
75+ const { noTimestamp, noFilename, onlyMessage, noColor } = prettyOpts ;
6076
61- const noTimestamp = ! ! prettyOpts ?. noTimestamp ;
62- const noFilename = ! ! prettyOpts ?. noFilename ;
63- const onlyMessage = ! ! prettyOpts ?. onlyMessage ;
77+ // current level icon and color
78+ const levelsConfig = LEVELS_CONFIG [ logContext . level ] ;
6479
6580 const file = noFilename
6681 ? ''
67- : ` ${ chalk . underline . green ( logContext . context . source . file_name ) } ` ;
68- const date = noTimestamp ? '' : chalk . white ( `[${ logContext . dt } ] ` ) ;
69- const level = config . color ( `${ config . icon } ${ logContext . level } ` ) ;
70- const message = config . color ( logContext . message ) ;
71- const logs = onlyMessage ? '' : pretty . _getLogs ( logContext ) ;
82+ : ` ${ exports . _applyColor (
83+ chalk . underline . green ,
84+ logContext . context . source . file_name ,
85+ noColor ,
86+ ) } `;
87+
88+ const date = noTimestamp
89+ ? ''
90+ : exports . _applyColor ( chalk . white , `[${ logContext . dt } ] ` , noColor ) ;
91+
92+ const level = exports . _applyColor (
93+ levelsConfig . color ,
94+ `${ levelsConfig . icon } ${ logContext . level } ` ,
95+ noColor ,
96+ ) ;
97+ const message = exports . _applyColor (
98+ levelsConfig . color ,
99+ logContext . message ,
100+ noColor ,
101+ ) ;
102+
103+ const logs = onlyMessage ? '' : exports . _getLogs ( logContext ) ;
72104
73105 const output = `${ date } ${ level } ${ file } - ${ message } \n${ logs } ` ;
74106
75107 return output ;
76108} ;
77109
110+ /**
111+ * Will apply the color conditionally upon the provided noColor argument/
112+ *
113+ * @param {function } colorFn The color function.
114+ * @param {string } string The string to color or not.
115+ * @param {boolean } noColor Set to true to not apply color.
116+ * @return {string } formatted string.
117+ * @private
118+ */
119+ exports . _applyColor = ( colorFn , string , noColor ) => {
120+ if ( noColor ) {
121+ return string ;
122+ }
123+
124+ return colorFn ( string ) ;
125+ } ;
126+
78127/**
79128 * Returns formatted logs for pretty print.
80129 *
81130 * @param {Object } logContext The log context to format.
82131 * @return {string } Log output.
83132 * @private
84133 */
85- pretty . _getLogs = function ( logContext ) {
134+ exports . _getLogs = function ( logContext ) {
86135 const logs = { } ;
87- const blacklist = [ 'runtime' , 'source' , 'system' ] ;
136+
88137 const { event, context } = logContext ;
89138
90- // remove unnecessary keys
91- blacklist . forEach ( ( key ) => {
92- delete context [ key ] ;
139+ const eventKeys = Object . keys ( event ) ;
140+ const contextKeys = Object . keys ( context ) ;
141+
142+ contextKeys . forEach ( ( key ) => {
143+ if ( CONTEXT_IGNORE_KEYS . includes ( key ) ) {
144+ return ;
145+ }
146+
147+ if ( logs . context ) {
148+ logs . context [ key ] = context [ key ] ;
149+ } else {
150+ logs . context = {
151+ [ key ] : context [ key ] ,
152+ } ;
153+ }
93154 } ) ;
94- delete event . http_request ;
95155
96- // set event if exists
97- if ( ! isObjectEmpty ( event ) ) {
98- logs . event = event ;
99- }
100-
101- // set context
102- if ( ! isObjectEmpty ( context ) ) {
103- logs . context = context ;
104- }
156+ eventKeys . forEach ( ( eventKey ) => {
157+ if ( EVENT_IGNORE_KEYS . includes ( eventKey ) ) {
158+ return ;
159+ }
160+
161+ if ( logs . event ) {
162+ logs . event [ eventKey ] = event [ eventKey ] ;
163+ } else {
164+ logs . event = {
165+ [ eventKey ] : event [ eventKey ] ,
166+ } ;
167+ }
168+ } ) ;
105169
106170 // empty string if the logs are emtpy
107171 if ( isObjectEmpty ( logs ) ) {
0 commit comments