首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Bunyan和Logentries在类型记录中登录

使用Bunyan和Logentries在类型记录中登录
EN

Stack Overflow用户
提问于 2017-02-13 13:22:50
回答 1查看 4.1K关注 0票数 1

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

这是我的package.json的摘录

代码语言:javascript
复制
  "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"
  },

代码

代码语言:javascript
复制
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不警告我任何错误。但是,一旦我运行这个应用程序,我就会得到以下错误:

存在不是一个函数。(在“存在(PkgPath)”中,“存在”未定义)

代码语言:javascript
复制
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与实际的节点模块不匹配,但不清楚应该如何解决这个问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-01 08:17:18

我也面临过同样的问题。bunyan-logentries模块依赖于le_node:用于节点的日志条目模块。

le_node使用浏览器中不可用的nettls模块。

为了继续下去,我实现了一个定制的bunyan流,它通过日志的REST API将日志发送到日志。它的代码很简单,而且可以工作。

下面是演示此解决方案的示例代码。

创建Bunyan实例的LoggerService:

代码语言:javascript
复制
@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自定义流:

代码语言:javascript
复制
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';
    }
};

也许能帮上忙

致以问候。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42205121

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档