logdna / logger-node

A nodejs logger client for LogDNA
MIT License
34 stars 17 forks source link

TypeError: this.emit is not a function #31

Closed moop-moop closed 3 years ago

moop-moop commented 3 years ago

I am attempting to use this library with Netlify serverless functions, which are built on AWS lambda.

Following this example almost verbatim: https://github.com/logdna/logger-node#aws-lambda-support

I keep getting this error (TypeError: this.emit is not a function). Runtime stack error

Request from ::1: GET /.netlify/functions/redirector?uri=%2Fasdasd%2Fasd
Response with status 500 in 65 ms.
TypeError: this.emit is not a function
    at Array.log (C:\Users\me\GitHub\site.org\node_modules\@logdna\logger\lib\logger.js:475:12)
    at Object.log (C:\Users\me\GitHub\site.org\serverless\redirector\index.js:24:14)
    at serveRedirect (C:\Users\me\GitHub\site.org\node_modules\netlify-cli\src\utils\proxy.js:236:15)
TypeError: this.emit is not a function
    at Array.log (C:\Users\me\GitHub\site.org\node_modules\@logdna\logger\lib\logger.js:475:12)
    at Object.log (C:\Users\me\GitHub\site.org\serverless\redirector\index.js:24:14)
    at serveRedirect (C:\Users\me\GitHub\site.org\node_modules\netlify-cli\src\utils\proxy.js:236:15)

C:\Users\me\GitHub\site.org\node_modules\netlify-redirector\lib\redirects.js:116
      throw ex;
      ^
abort({}) at Error
    at jsStackTrace (C:\Users\me\GitHub\site.org\node_modules\netlify-redirector\lib\redirects.js:1070:13)
    at stackTrace (C:\Users\me\GitHub\site.org\node_modules\netlify-redirector\lib\redirects.js:1087:12)
    at process.abort (C:\Users\me\GitHub\site.org\node_modules\netlify-redirector\lib\redirects.js:8502:44)
    at process.emit (events.js:310:20)
    at processPromiseRejections (internal/process/promises.js:209:33)
    at processTicksAndRejections (internal/process/task_queues.js:98:32)
(Use `node --trace-uncaught ...` to show where the exception was thrown)
moop-moop commented 3 years ago

I should note:

  1. using a bad API ingestion key throws an appropriate error, so a connection is made to create the logger.
  2. Using the REST API directly without the logger seems to work fine.

This class works just fine for example:

/* eslint-disable no-console */
const fetch = require('isomorphic-unfetch');

class LogDNA {
  constructor(options = { hostname: 'host', app: 'logdna', env: 'test', apikey: null }) {
    this.hostname = options.hostname;
    this.app = options.app;
    this.env = options.env;
    this.apikey = options.apikey;
  }

  async log(line = 'Default log line.', level = 'INFO', meta = {}) {
    const body = {
      app: this.app,
      env: this.env,
      level,
      line,
      meta
    };

    const response = await fetch(`https://logs.logdna.com/logs/ingest?hostname=${this.hostname}`, {
      method: 'post',
      body: JSON.stringify({ lines: [body] }),
      headers: {
        'Content-Type': 'application/json; charset=UTF-8',
        'apikey': this.apikey
      }
    });
    const json = await response.json();
    return Promise.resolve(console.log(json));
  }
}

module.exports = LogDNA;
darinspivey commented 3 years ago

HI @moop-moop, thanks for reaching out. We're happy to help you try and get the logger working in your Netlify function. At a glance, it feels like the this context may be getting corrupted (although the methods are still being called, so...). To help us try and reproduce this, can you provide a code sample of your function and how it's instantiating and calling the logger?

If we can reproduce the issue, perhaps we can put together a fix that also has a formal serverless function test in the test suite. Thanks!

mdeltito commented 3 years ago

Hi @moop-moop, thanks again for bringing this up. We've put together a PR to fix the documentation here, you can take a look at the updated version if you'd like to give it a try before it is merged:

https://github.com/logdna/logger-node/blob/004784521027811ba474cbd5da01164226b64ffb/README.md#using-with-aws-lambda

One thing I'll add here is that it is not strictly necessary to override console.log or console.error when deploying with AWS Lambda. That is more for the convenience of integrating LogDNA with existing code that is using console to output to stdout/stderr. You can definitely just call logger.log() instead, if you are only interested in sending logs to LogDNA and not CloudWatch. The updated documentation is (hopefully) clearer about this 😄

moop-moop commented 3 years ago

I got the same errors using logger.log() directly using the original example, but I will update my tests and get out there publicly to look at.

moop-moop commented 3 years ago

It works as per the updated documentation. Tested with this: https://github.com/moop-moop/netlify-logdna Built here:

Webhook custom implementation: https://logdna.netlify.app/logtest

This example is logging to logDNA and the Netlify Functions console (console.* output) https://logdna.netlify.app/loggertest