gpt4 book ai didi

node.js - 使用 Bunyan 和 Logentries 登录 Typescript

转载 作者:太空宇宙 更新时间:2023-11-03 23:27:26 26 4
gpt4 key购买 nike

我想在我的 ionic 应用程序中使用 logentries.com 设置远程日志记录。

这是我的 package.json 的摘录:

  "dependencies": {

"bunyan": "^1.8.5",
"bunyan-logentries": "^1.2.0",

},
"devDependencies": {
"@types/bunyan": "0.0.35",
"@types/bunyan-logentries": "0.1.30",
"typescript": "2.0.9"
},

代码

import {createStream} from "bunyan-logentries";
import * as Logger from "bunyan";

// ...
constructor() {
let token = 'xxxxxxxxxxxxxxxxx';
let log = Logger.createLogger({
name: '',
streams: [{
level: 'info',
stream: createStream({token: token}),
type: 'raw'
}]
});
log.info("Created log");
}

问题

我的 IDE 不会警告我任何错误。但一旦我运行该应用程序,我就会收到以下错误:

exists is not a function. (In 'exists(pkgPath)', 'exists' is undefined)

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

我认为问题的核心是@types与实际的 Node 模块不匹配,但目前还不清楚如何解决这个问题。

最佳答案

我也遇到过同样的问题。bunyan-logentries 模块依赖于 le_node: Node 的 logentries 模块。

le_node 使用浏览器无法使用的 nettls 模块。

为了继续,我实现了一个自定义的 Bunyan 流,它通过 REST API 将日志发送到 logentries 。编码简单且有效。

下面是一个示例代码来说明此解决方案。

创建 Bunyan 实例的 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'
}]
});
}
}

将日志发送到logentries的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';
}
};

也许有帮助,

问候。

关于node.js - 使用 Bunyan 和 Logentries 登录 Typescript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42205121/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com