fastify-now icon indicating copy to clipboard operation
fastify-now copied to clipboard

How to log using Fastify-Now

Open RicardoViteriR opened this issue 2 years ago • 3 comments

I am using fastify logging in a project as well as schema validation.

I am wondering what options would I need the log to print anything other than INFO when the schema does not pass validation. I think it has something to do with the opts property of the REST verb but I have not had any luck.

Anyone can point me in the right direction?

RicardoViteriR avatar Aug 28 '22 02:08 RicardoViteriR

are you talking about general logging in fastify, or something related to this library fastify-now? Also, can you provide a code example of what you are trying to do right now?

yonathan06 avatar Aug 28 '22 07:08 yonathan06

hi, yes so I need to create an error log when the schema validation does not pass. So how would I call the log method from a fastify-now POST method?

type InvoiceSchema = NowRequestHandler<{
  Body: typeof datilInvoiceSchemaHasura.properties;
  Reply: {
    id: { type: 'number' };
    invoice_url: { type: 'string' };
  };
}>;

export const POST = async (request: any, reply: any) => {
  const requestBody = request.body?.event ?? request.body;
  const bodyPayload = requestBody?.data?.new ?? requestBody;
  const { id, body_factura_electronica } = bodyPayload;

  const responseDatil = await fetchToDatil(URL_DATIL, body_factura_electronica);

  const variables = {
    id,
    id_factura: responseDatil.id,
    clave_acceso: responseDatil.clave_acceso
  };

  const response = await fetchMutation('updateDatilInvoice', variables);
  return response;
};

POST.opts = {
  schema: {
    body: datilInvoiceSchemaHasura,
    description: 'This endpoint sends an invoice',
    tags: ['Datil'],
    params: {
      description: 'SRI Invoice',
      type: 'object',
      properties: { datilInvoiceSchemaHasura }
    }
    // response: {
    //   200: {
    //     description: 'Success Response',
    //     type: 'object',
    //     properties: {
    //       id: { type: 'string' },
    //       invoice_url: { type: 'string' },
    //       clave_acceso: { type: 'string' }
    //     }
    //   }
    // }
  }
};

RicardoViteriR avatar Aug 31 '22 18:08 RicardoViteriR

You can use the request.log.error(...) function You can also use the server instance, but using the function keyword for the handler (instead of an arrow function)

export const POST =  function (request: any, reply: any) {
this.log.error(...);

BTW this is not related to this repo, if you have any more questions regarding fastify I suggest to go the the main repo https://github.com/fastify/fastify

yonathan06 avatar Sep 01 '22 08:09 yonathan06

Hi @yonathan06 thanks for letting me know. Will look into it.

RicardoViteriR avatar Sep 13 '22 05:09 RicardoViteriR