2016-09-01 3 views
0

Winston 2.2.0의 구성에서 배열을 올바르게 출력하는 데 문제가있어서 뭔가 잘못되었습니다. 올바른 말은 console.log이 출력 할 것임을 의미합니다. 구성이 기본값 transports.console 만 있습니다.Node.js 윈스턴 로깅 : 배열 출력

로거에 인수가 하나만있는 경우 배열은 인덱스가 먼저 표시되고 두 개가 지정되면 올바르게 인쇄됩니다.

예 :

logger.debug ([ 1,2 ]) 
> 0=1, 1=2 

logger.debug ( [ 1,2], '') 
> [ 1, 2 ] '' 

logger.debug ({x:1,y:2,z:{i:3}}) 
> x=1, y=2, i=3 

prettyPrint:true 켜기

는 JSON 객체가 제대로 표시 할 수 있지만 추가 색상, 캐리지 리턴을 추가하고 여전히 인덱스 배열을 표시 할 수 있습니다.

+1

'logger.debug ('% j', [1, 2])'? – robertklep

답변

0

이 해결 방법은 불쾌한 모양과 여러 인수 로거에 대한 호출을 제외하지만, 작동 :

logger.dbg = function() { 
    if(arguments.length>1) this.warn("more than one arg given to dbg()") 
    if(Array.isArray(arguments[0])) 
     logger.debug('%j',arguments[0]) 
    else 
     logger.debug(arguments[0],'') 
} 

결과 :

logger.dbg([ 1, 2, {x:1} ]) 
logger.dbg({x:1,z:2, s:[1,]}) 
logger.dbg("AAA") 
logger.dbg(undefined) 
logger.dbg(null) 
logger.dbg([]) 

출력 :

debug: [log.js:131] [1,2,{"x":1}] 
debug: [log.js:132] { x: 1, z: 2, s: [ 1 ] } 
debug: [log.js:133] AAA 
debug: [log.js:134] undefined 
debug: [log.js:135] null 
debug: [log.js:136] [] 
1

적합하지, 그러나 아마도 여전히 유용 할 것입니다 :

var logger = new winston.Logger({ 
    transports : [ new winston.transports.Console({}) ], 
    rewriters : [ 
    function (level, msg, meta) { 
     return meta ? JSON.stringify(meta) : meta; 
    } 
    ] 
}); 
+0

여분의 "{}"을 지우려면 : "return meta &&! isEmpty (meta)?" JSON.stringify (메타) : 메타; 그러나 이것은''null''의 출력을 사라지게합니다. – Flint