2017-10-26 15 views
2

저는 Node로 시작 했으므로 이제 응용 프로그램에 일부 로깅을 추가하려고합니다.이 경우에는 Winstonjs이 가장 적합합니다. 그래서 먼저 설치 :Winstonjs logger를 통한 노드 로깅은 TypeError를 제공합니다

npm install winston 

을 그리고 나는 추가 정보에서 첫 번째 예제 코드를 복사 (그리고 그것을하기 전에 필요에 추가) :

"use strict"; 

let winston = require('winston'); 

const logger = winston.createLogger({ 
    level: 'info', 
    format: winston.format.json(), 
    transports: [ 
    // 
    // - Write to all logs with level `info` and below to `combined.log` 
    // - Write all logs error (and below) to `error.log`. 
    // 
    new winston.transports.File({ filename: 'error.log', level: 'error' }), 
    new winston.transports.File({ filename: 'combined.log' }) 
    ] 
}); 

// 
// If we're not in production then log to the `console` with the format: 
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) ` 
// 
if (process.env.NODE_ENV !== 'production') { 
    logger.add(new winston.transports.Console({ 
    format: winston.format.simple() 
    })); 
} 

하지만 오류 얻을 :

/Users/kramer65/mms/testlogging.js:7 
    format: winston.format.json(), 
         ^
TypeError: Cannot read property 'json' of undefined 

아무도 내가 여기서 잘못하고있는 것을 알고 있습니까? 모든 팁을 환영합니다!

+0

, 내가 복제 할 수 있습니다에 V2의 문서를 읽을 수 있습니다 이 결과는 독립적으로 나타납니다. 'require ('winston')'에서 얻은 객체에는'format' 속성이 없습니다. –

+1

버전이 'npm ls winston'으로 설치되어 있는지 확인하십시오.이 버전은 v3 코드로 보입니다. –

+1

@ T.J.Crowder - 시도했지만 내 사무실 의자 주위를 걷는 것도 아무런 차이가 없습니다. ;-). 주제 : 문제를 복제 해 주셔서 감사합니다. 방금 소스를 탐색하고 json이없는 https://github.com/winstonjs/winston/tree/master/lib/winston/transports 폴더를 찾았습니다. 그게 우리가 어떻게 든 도움이 될지 모르겠다. – kramer65

답변

2

코드는 아직 출시되지 않은 새로운 v3과 호환됩니다. 당신이 원하는 경우를 설치합니다 : npm i [email protected] --save

을 아니면 v2를 고수하려는 경우, 당신은 npm 그것은 문서의 오류처럼 보이는

ref