@@ -6,12 +6,11 @@ const chalk = require('chalk');
66const utils = require ( 'util' ) ;
77const ora = require ( 'ora' ) ;
88const os = require ( 'os' ) ;
9+ const stringifyObject = require ( 'stringify-object' ) ;
10+ const _ = require ( 'lodash' ) ;
911
1012npmlog . heading = require ( '../constants' ) . SCOPE_NAME || '@micro-app' ;
1113
12- if ( process . env . MICRO_APP_LOGGER_LEVEL ) {
13- npmlog . level = process . env . MICRO_APP_LOGGER_LEVEL === 'true' ? 'silly' : process . env . MICRO_APP_LOGGER_LEVEL ;
14- }
1514// npmlog.prefixStyle = {};
1615const CUSTOM_LEVEL = {
1716 success : {
@@ -48,11 +47,21 @@ const getStdoutMethod = function(type) {
4847 return process . stdout . write . bind ( process . stdout ) ;
4948} ;
5049
50+ function formatObject ( message ) {
51+ if ( _ . isString ( message ) ) {
52+ return message ;
53+ }
54+ return stringifyObject ( message , {
55+ indent : ' ' ,
56+ singleQuotes : false ,
57+ inlineCharacterLimit : 12 ,
58+ } ) ;
59+ }
60+
61+ // TODO 优化输出
5162function dyeMessage ( type , message ) {
63+ message = formatObject ( message ) ;
5264 switch ( type . toLowerCase ( ) ) {
53- case 'debug' : {
54- return chalk . visible ( message ) ;
55- }
5665 case 'warn' : {
5766 return chalk . yellowBright ( message ) ;
5867 }
@@ -107,7 +116,6 @@ class Logger {
107116 }
108117
109118 debug ( ) {
110- // if (!process.env.MICRO_APP_DEBUG_LOGGER) return; // 是否开启
111119 return this . getMethod ( 'debug' ) ( ...arguments ) ;
112120 }
113121 warn ( ) {
@@ -162,24 +170,26 @@ class Logger {
162170 }
163171
164172 getMethod ( type ) {
173+ if ( [ 'debug' ] . includes ( type ) ) { // 不再需要 debug
174+ type = 'verbose' ;
175+ }
165176 const logger = this . getNpmlogMethod ( type ) ;
177+ if ( typeof logger !== 'function' ) {
178+ return logger ;
179+ }
166180 return function ( ...args ) {
167181 if ( args . length <= 1 ) {
168182 return logger ( false , ...args . map ( arg => dyeMessage ( type , arg ) ) ) ;
169183 }
170184 return logger ( args [ 0 ] , ...args . splice ( 1 ) . map ( arg => dyeMessage ( type , arg ) ) ) ;
171185 } ;
172- // const logger = getStdoutMethod(type);
173- // return function(...args) {
174- // return logger(format[type].call(format, ...args));
175- // };
176186 }
177187
178188 getNpmlogMethod ( type ) {
179- if ( [ 'debug' ] . includes ( type ) ) {
180- type = 'verbose' ;
189+ if ( typeof this . npmlog [ type ] === 'function' ) {
190+ return this . npmlog [ type ] . bind ( this . npmlog ) ;
181191 }
182- return this . npmlog [ type ] . bind ( this . npmlog ) ;
192+ return this . npmlog [ type ] ;
183193 }
184194
185195 setAlias ( type , value ) {
@@ -194,15 +204,15 @@ class Logger {
194204 return this . aliasMap . get ( type ) ;
195205 }
196206
207+ addCustomToString ( key , value ) {
208+ this . customFormatMap . set ( key , value ) ;
209+ }
210+
197211 newGroup ( name , ...args ) {
198212 const newLog = this . npmlog . newGroup ( name , ...args ) ;
199213 return factroy ( newLog , { alias : this . aliasMap , customFormat : this . customFormatMap } ) ;
200214 }
201215
202- addCustomToString ( key , value ) {
203- this . customFormatMap . set ( key , value ) ;
204- }
205-
206216 get format ( ) { // 兼容 toString
207217 return new Proxy ( this , {
208218 get ( target , prop ) {
@@ -215,14 +225,21 @@ class Logger {
215225function factroy ( log , opts = { } ) {
216226 return new Proxy ( new Logger ( log , opts ) , {
217227 get ( target , prop ) {
218- if ( ! ( prop in target ) && typeof prop === 'string' && ( prop in npmlog ) ) {
228+ if ( prop in target ) {
229+ return target [ prop ] ;
230+ }
231+ if ( [ 'silly' , 'verbose' , 'info' , 'timing' , 'http' , 'notice' , 'warn' , 'error' , 'silent' ] . includes ( prop ) ) {
219232 return target . getMethod ( prop ) ;
220233 }
221234 const alias = target . getAlias ( prop ) ;
222235 if ( alias ) {
223236 return target . getMethod ( alias ) ;
224237 }
225- return target [ prop ] ;
238+ return log [ prop ] ;
239+ } ,
240+ set ( target , prop , value ) {
241+ log [ prop ] = value ;
242+ return true ;
226243 } ,
227244 } ) ;
228245}
@@ -231,3 +248,4 @@ module.exports = factroy(npmlog, { customFormat: Object.entries(format) });
231248
232249module . exports . getStdoutMethod = getStdoutMethod ;
233250
251+ module . exports . Logger = Logger ;
0 commit comments