technicallyjosh / pino-http-send

A basic PinoJS http(s) transport with batching!
6 stars 13 forks source link

pino-http-send - HTTPError: Response code 404 (Not Found) #18

Open aaestrada opened 2 years ago

aaestrada commented 2 years ago

I'm trying to send logs to and endpoint and optional pass also to the stadout when is needed...

But i'm getting the following error

Screen Shot 2022-01-09 at 8 26 24

See my current config:

....................................................................................................................................................................index.js

"use strict";
// see docs https://github.com/pinojs/pino/blob/master/docs/pretty.md *prod
// https://www.npmjs.com/package/pino-transmit-http/v/1.0.0-alpha.3
// see docs https://github.com/pinojs/pino-pretty *dev
const Hapi = require("@hapi/hapi");
//const {createWriteStream} = require('pino-http-send');
const pino = require('pino');
const hapiPino = require('hapi-pino');
const getResponse = [
  { string: "string1", number: 1, boolean: true },
  { string: "string2", number: 2, boolean: false }
];

async function start() {

  // Create a server with a host and port
  const server = Hapi.server({
    host: "localhost",
    port: 3000,
    debug: false // disable Hapi debug console logging
  });

  // Add the route
  server.route({
    method: "GET",
    path: "/items",
    options: {
      log: { collect: true },
      cache: { expiresIn: 5000 },
      handler: async function (request, h) {
        try {
          // you can also use a pino instance, which will be faster
          request.logger.info('GET_items', getResponse)
          return h.response(getResponse);
        } catch (err) {
          console.log(err);
          return request.logger.error('GET_error', err)
        }
      }
    }
  });

  await server.register({
    plugin: hapiPino,
    options: {
      instance: pino()
      //   {
      //   level: 'info',
      //   stream: createWriteStream({
      //     url: 'http://localhost:3000'
      //   })
      // })
    }
  });

  await server.start();
  server.log(["SERVER_INFO"], `server running: ${server.info.uri}/items`);
  return server;
}

start().catch((err) => {
  console.log(err);
  process.exit(1);
});

....................................................................................................................................................................package.json

{
  "name": "pino-example",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "PINO_HTTP_SEND_url=http://localhost:3000 node . | pino-http-send --url=http://localhost:3000"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@hapi/hapi": "^20.2.1",
    "hapi-pino": "^9.1.0",
    "pino-http-send": "^0.4.2"
  }
}

What i'm missing here? How can i see the logs in some endpoint? Where i can/need to configure that endpoint?

technicallyjosh commented 2 years ago

It looks like you have an incorrect env var there in your start script. PINO_HTTP_SEND_url should be PINO_HTTP_SEND_URL

aaestrada commented 2 years ago

Hi @technicallyjosh ! thanks for your help!!

i change my code as your recommendation and get the same issue

See my code now:

index.js

"use strict";
// see docs https://github.com/pinojs/pino/blob/master/docs/pretty.md *prod
// https://www.npmjs.com/package/pino-transmit-http/v/1.0.0-alpha.3
// see docs https://github.com/pinojs/pino-pretty *dev
const Hapi = require("@hapi/hapi");
//const {createWriteStream} = require('pino-http-send');
const pino = require('pino');
const hapiPino = require('hapi-pino');
const getResponse = [
  { string: "string1", number: 1, boolean: true },
  { string: "string2", number: 2, boolean: false }
];

async function start() {

  // Create a server with a host and port
  const server = Hapi.server({
    host: "localhost",
    port: 3000,
    debug: false // disable Hapi debug console logging
  });

  // Add the route
  server.route({
    method: "GET",
    path: "/items",
    options: {
      log: { collect: true },
      cache: { expiresIn: 5000 },
      handler: async function (request, h) {
        try {
          // you can also use a pino instance, which will be faster
          request.logger.info('GET_items', getResponse)
          return h.response(getResponse);
        } catch (err) {
          console.log(err);
          return request.logger.error('GET_error', err)
        }
      }
    }
  });

  await server.register({
    plugin: hapiPino,
    options: {
      instance: pino()
      //   {
      //   level: 'info',
      //   stream: createWriteStream({
      //     url: 'http://localhost:3000'
      //   })
      // })
    }
  });

  await server.start();
  server.log(["SERVER_INFO"], `server running: ${server.info.uri}/items`);
  return server;
}

start().catch((err) => {
  console.log(err);
  process.exit(1);
});

package.json

{
  "name": "hapi-pino-http-send",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "PINO_HTTP_SEND_URL=http://localhost:3000 node . | ./node_modules/.bin/pino-http-send --url=http://localhost:3000"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@hapi/hapi": "^20.2.1",
    "hapi-pino": "^9.2.0",
    "pino-http-send": "^0.4.2"
  }
}

and result:

Screen Shot 2022-03-23 at 8 00 39
ength":60}},"responseTime":1,"msg":"[response] post / 404 (1ms)"},{"level":30,"time":1648043853462,"pid":26550,"hostname":"MacBook-Pro-de-Aaron.local","req":{"id":"1648043853461:MacBook-Pro-de-Aaron.local:26550:l13moz3x:10066","method":"post","url":"/","headers":{"user-agent":"got (https://github.com/sindresorhus/got)","content-type":"application/json","content-length":"3802","accept-encoding":"gzip, deflate, br","host":"localhost:3000","connection":"close"},"remoteAddress":"127.0.0.1","remotePort":55347},"res":{"statusCode":404,"headers":{"content-type":"application/json; charset=utf-8","cache-control":"no-cache","content-length":60}},"responseTime":1,"msg":"[response] post / 404 (1ms)"},{"level":30,"time":1648043854472,"pid":26550,"hostname":"MacBook-Pro-de-Aaron.local","req":{"id":"1648043854471:MacBook-Pro-de-Aaron.local:26550:l13moz3x:10067","method":"post","url":"/","headers":{"user-agent":"got (https://github.com/sindresorhus/got)","content-type":"application/json","content-length":"3802","accept-encoding":"gzip, deflate, br","host":"localhost:3000","connection":"close"},"remoteAddress":"127.0.0.1","remotePort":55348},"res":{"statusCode":404,"headers":{"content-type":"application/json; charset=utf-8","cache-control":"no-cache","content-length":60}},"responseTime":1,"msg":"[response] post / 404 (1ms)"}]
pino-http-send - HTTPError: Response code 404 (Not Found) ...retrying in 1000ms
pino-http-send - HTTPError: Response code 404 (Not Found) ...retrying in 1000ms
pino-http-send - HTTPError: Response code 404 (Not Found) ...retrying in 1000ms
pino-http-send - HTTPError: Response code 404 (Not Found) ...retrying in 1000ms
pino-http-send - HTTPError: Response code 404 (Not Found) ...retrying in 1000ms
pino-http-send - HTTPError: Response code 404 (Not Found) 

I'm missing somethiing?

Where the logs should be sended? and how can i see them?