Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

898
Visualizações
How to log raw JSON to Cloudwatch from AWS Lambda in node.js?

I have some node.js based Lambdas that are logging data.

In order to properly query and filter the data etc, I want to log as pure JSON data from my Lambdas.

However, when I do a regular console.log it makes an ordinary string of the data.

console.log({a:1,b:2,x:"xxx"})

Results in this:

2020-04-29T14:46:45.722Z    3f64c499-fbae-4a84-996c-5e5f0cb5302c    INFO    { a: 1, b: 2, x: 'xxx' }

The logged line above does not seem to be searchable as JSON using the various filter matching options in CloudWatch.

I've tried to call the AWS.CloudWatchLogs API directly but since I'm using lambda I cannot maintain a token between invocations of the functions, so I'm not sure that's the way to go.

Have anyone else had success in logging raw JSON from a Javascript Lambda?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The problem is that console.log() does not go directly to stdout/stderr. You can see that using this Lambda:

const process = require('process');

exports.handler = async (event) => {
    console.log("message 1");
    process.stdout.write("message 2\n");
};

If you invoke that, you will see output like this:

START RequestId: 6942bebc-1997-42cd-90c2-d76b44c637283 Version: $LATEST
2020-04-29T17:06:07.303Z    6935bebc-1d97-42cd-90c2-d76b4a637683    INFO    message 1
message 2
END RequestId: 6942bebc-1997-42cd-90c2-d76b44c637283

So to get the output you want you could either redefine console.log to go to stderr, or write to stdout/stderr directly.

Or you could use a logging framework that writes to stdout/stderr, which may give you more flexibility on how your messages are written. I don't do Node development, but I've heard that Winston is the standard logging framework.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda