Node.js 使用Bunyan和Logentries登录Typescript
我想在ionic应用程序中使用logentries.com设置远程日志记录 这是我的Node.js 使用Bunyan和Logentries登录Typescript,node.js,angular,ionic-framework,ionic2,logentries,Node.js,Angular,Ionic Framework,Ionic2,Logentries,我想在ionic应用程序中使用logentries.com设置远程日志记录 这是我的包.json的摘录: 代码 问题 我的IDE不会警告我任何错误。但一旦我运行应用程序,就会出现以下错误: exists不是一个函数。(在“exists(pkgPath)”中,“exists”未定义) 我认为问题的核心是@types与实际的节点模块不匹配,但不清楚应该如何解决这个问题。我也遇到过同样的问题。 bunyan日志条目模块取决于leu节点:节点的日志条目模块 le_节点使用浏览器中不可用的net和tls模
包.json的摘录:
代码
问题
我的IDE不会警告我任何错误。但一旦我运行应用程序,就会出现以下错误:
exists不是一个函数。(在“exists(pkgPath)”中,“exists”未定义)
我认为问题的核心是@types
与实际的节点模块不匹配,但不清楚应该如何解决这个问题。我也遇到过同样的问题。
bunyan日志条目
模块取决于leu节点
:节点的日志条目模块
le_节点
使用浏览器中不可用的net
和tls
模块。
为了继续,我实现了一个定制的bunayan流,它通过日志文件将日志发送到日志条目。这是直接的代码,它的工作
下面是一个示例代码来说明此解决方案
创建班扬实例的LoggerService:
@Injectable()
export class LoggerService {
constructor(private http: Http) {
}
public create(name: string): Logger{
return Bunyan.createLogger({
name: name,
streams: [{
stream: new LogentriesBunyanStream(AppConfig.LogEntries.token, this.http),
type: 'raw'
}]
});
}
}
向日志条目发送日志的bunyan自定义流:
export class LogentriesBunyanStream extends Writable {
private token: string;
private http: Http;
constructor(token: string, http: Http) {
super({objectMode: true});
this.http = http;
this.token = token;
}
public _write(rec: any, encoding?: string, cb?: Function) {
// Replace the level code with a friendly string
rec.level = LogentriesBunyanStream.resolveLevel(rec.level);
// Send the post request to logentries
// https://docs.logentries.com/docs/http-post
const LOGENTRIES_URL = "https://webhook.logentries.com/noformat/logs/";
let headers = new Headers();
headers.append("Content-Type", 'application/json');
let requestoptions = new RequestOptions({
method: RequestMethod.Post,
headers: headers,
body: JSON.stringify(rec)
});
this.http.request(LOGENTRIES_URL + this.token, requestoptions).toPromise().then((response) => {
cb();
}).catch((error) =>{
console.log("faield to send log to the logentries server");
});
};
private static resolveLevel(bunyanLevel) {
let levelToName = {
10: 'DEBUG',
20: 'DEBUG',
30: 'INFO',
40: 'WARN',
50: 'ERROR',
60: 'CRIT'
};
return levelToName[bunyanLevel] || 'INFO';
}
};
也许有帮助
问候
findPackage@http://localhost:8101/build/main.js:131257:18
register@http://localhost:8101/build/main.js:131332:31
http://localhost:8101/build/main.js:112585:50
http://localhost:8101/build/main.js:113391:34
__webpack_require__@http://localhost:8101/build/main.js:20:34
http://localhost:8101/build/main.js:129423:37
__webpack_require__@http://localhost:8101/build/main.js:20:34
http://localhost:8101/build/main.js:29972:95
__webpack_require__@http://localhost:8101/build/main.js:20:34
http://localhost:8101/build/main.js:80592:90
__webpack_require__@http://localhost:8101/build/main.js:20:34
http://localhost:8101/build/main.js:59390:96
__webpack_require__@http://localhost:8101/build/main.js:20:34
http://localhost:8101/build/main.js:59495:94
__webpack_require__@http://localhost:8101/build/main.js:20:34
http://localhost:8101/build/main.js:128048:94
__webpack_require__@http://localhost:8101/build/main.js:20:34
http://localhost:8101/build/main.js:116775:92
__webpack_require__@http://localhost:8101/build/main.js:20:34
http://localhost:8101/build/main.js:149625:89
__webpack_require__@http://localhost:8101/build/main.js:20:34
http://localhost:8101/build/main.js:66:37
global code@http://localhost:8101/build/main.js:67:12
@Injectable()
export class LoggerService {
constructor(private http: Http) {
}
public create(name: string): Logger{
return Bunyan.createLogger({
name: name,
streams: [{
stream: new LogentriesBunyanStream(AppConfig.LogEntries.token, this.http),
type: 'raw'
}]
});
}
}
export class LogentriesBunyanStream extends Writable {
private token: string;
private http: Http;
constructor(token: string, http: Http) {
super({objectMode: true});
this.http = http;
this.token = token;
}
public _write(rec: any, encoding?: string, cb?: Function) {
// Replace the level code with a friendly string
rec.level = LogentriesBunyanStream.resolveLevel(rec.level);
// Send the post request to logentries
// https://docs.logentries.com/docs/http-post
const LOGENTRIES_URL = "https://webhook.logentries.com/noformat/logs/";
let headers = new Headers();
headers.append("Content-Type", 'application/json');
let requestoptions = new RequestOptions({
method: RequestMethod.Post,
headers: headers,
body: JSON.stringify(rec)
});
this.http.request(LOGENTRIES_URL + this.token, requestoptions).toPromise().then((response) => {
cb();
}).catch((error) =>{
console.log("faield to send log to the logentries server");
});
};
private static resolveLevel(bunyanLevel) {
let levelToName = {
10: 'DEBUG',
20: 'DEBUG',
30: 'INFO',
40: 'WARN',
50: 'ERROR',
60: 'CRIT'
};
return levelToName[bunyanLevel] || 'INFO';
}
};