smartive / graylog-ts

Provides a class for logging with graylog. Written in TypeScript
MIT License
1 stars 0 forks source link

Ionic 2 (Angular 2) usage #2

Open stovmascript opened 7 years ago

stovmascript commented 7 years ago

Hi guys, I'm new to TypeScript and trying to use graylog-ts in a ng2 service (Ionic 2 project). I'm wondering if you could help me out? It might just be a configuration issue.

Here is my service:

import { Graylog } from 'graylog-ts';
import { Injectable } from '@angular/core';

@Injectable()
export class GraylogService {

    constructor() {
        console.log('Hello GraylogService Provider');
        console.log(Graylog);
    }

}

But once I plug it into my app providers:

import {
    IonicApp,
    IonicModule,
    // ...
} from 'ionic-angular';

import {
    NgModule,
    // ...
} from '@angular/core';

import { GraylogService } from '../providers/graylog-service';
import { MyApp } from './app.component';

@NgModule({
    bootstrap: [IonicApp],
    declarations: [
        // ...
    ],
    entryComponents: [
        // ...
    ],
    imports: [
        IonicModule.forRoot(MyApp),
    ],
    providers: [
        GraylogService,
        // ...
    ],
})
export class AppModule {}

I get this weird error:

Uncaught TypeError: Object prototype may only be an Object or null: undefined
    at create (<anonymous>)
    at Function.Object.create (polyfills.js:2)
    at inherits (inherits_browser.js:5)
    at Object.<anonymous> (index.js:5)
    at Object.<anonymous> (index.js:90)
    at __webpack_require__ (bootstrap 56af062…:19)
    at Object.<anonymous> (browser.js:7)
    at Object.<anonymous> (browser.js:52)
    at __webpack_require__ (bootstrap 56af062…:19)
    at Object.<anonymous> (index.js:4)
Object.create @ polyfills.js:2
inherits @ inherits_browser.js:5
(anonymous) @ index.js:5
(anonymous) @ index.js:90
__webpack_require__ @ bootstrap 56af062…:19
(anonymous) @ browser.js:7
(anonymous) @ browser.js:52
__webpack_require__ @ bootstrap 56af062…:19
(anonymous) @ index.js:4
__webpack_require__ @ bootstrap 56af062…:19
(anonymous) @ graylog.js:2
(anonymous) @ graylog.js:305
__webpack_require__ @ bootstrap 56af062…:19
(anonymous) @ Graylog.js:3
__webpack_require__ @ bootstrap 56af062…:19
(anonymous) @ index.js:5
__webpack_require__ @ bootstrap 56af062…:19
(anonymous) @ messages.js:13
__webpack_require__ @ bootstrap 56af062…:19
(anonymous) @ main.js:104103
__webpack_require__ @ bootstrap 56af062…:19
(anonymous) @ main.js:165011
__webpack_require__ @ bootstrap 56af062…:19
(anonymous) @ bootstrap 56af062…:65
(anonymous) @ bootstrap 56af062…:65

I've seen errors like this on platforms where core nodejs modules aren't supported and need to be shimmed with browser counterparts. Could this be the case?

I see that node-graylog2 is using a few core node modules: https://github.com/Wizcorp/node-graylog2/blob/master/graylog.js#L1-L6.

buehler commented 7 years ago

Hi @stovmascript Is there a frontend (aka browser-usable) implementation of graylog? I thought, there was only the node package that does work in node but not in the browser.

Actually, we built this package to run in a NodeJS environment.

Something may be wrong with the shims you are using (as of the error message). Aside from that, the core node modules you found in node-graylog2 may be the case why it's not running.

I'd suggest you code your service without our package and create the api by yourself. You could create an angular service that is doing exactly what you want but calls the GELF endpoint instead of your own api.

stovmascript commented 7 years ago

Hey @buehler I dug around and tried different GELF clients and no there isn't. They're all really for the nodejs environment as you say. I was able to replace dgram and other node builtins like crypto with browser counterparts, but after getting rid of those errors I still arrived at a similar one to the one I posted above.

I don't think there's a standard UDP browser API at the moment either, even though https://github.com/icopp/browser-shim-node-dgram, which I've used to shim dgram uses something specific to Chrome and Firefox.

So yeah, at the moment, we're gonna enable TCP for using Graylog from our client apps and make our own Angular service for this case. We've just started using Graylog for all our projects from the server side and people are really liking it. Too bad we can't have the fire and forget behaviour from the client side at the moment though.

Thanks for your suggestion and hopefully we'll be able to switch to some UDP lib like yours in the near future even in client apps :)