pino-cloudwatch icon indicating copy to clipboard operation
pino-cloudwatch copied to clipboard

Nothing happens...

Open demian85 opened this issue 3 years ago • 7 comments

import pino from 'pino'
import cloudwatchStream from 'pino-cloudwatch'

const {
  CLOUDWATCH_NODEJS_LOG_GROUP,
  AWS_ACCESS_KEY_ID,
  AWS_SECRET_ACCESS_KEY,
  AWS_REGION,
} = process.env

export const logger = pino(
  {
    name: 'mqtt-broker',
    level: process.env.LOG_LEVEL || 'info',
  },
  cloudwatchStream({
    group: CLOUDWATCH_NODEJS_LOG_GROUP,
    aws_access_key_id: AWS_ACCESS_KEY_ID,
    aws_secret_access_key: AWS_SECRET_ACCESS_KEY,
    aws_region: AWS_REGION,
  })
)

in another file...

import { logger } from '../lib/logger'

logger.info(`Connecting...`, { hello: 'world' })

Nothing gets logged, no error, no nothing. What is happening? How can I debug this thing?

Thanks.

demian85 avatar Jun 21 '21 17:06 demian85

Hello? I finally used this module to capture stdout from my script, but I was hoping to use this programmatically. Anyway, I found another issue and I will report it.

demian85 avatar Jul 12 '21 13:07 demian85

I guess this project is dead...

demian85 avatar Aug 23 '21 14:08 demian85

I ran into the same problem and was about to throw my hands up in frustration and jump to Winston, but I poked around with a debugger and figured this out. In case anyone else ends up here...

The problem is that pino-cloudwatch will buffer logs and only send them to cloudwatch when one of the following three conditions is met:

If you don't specify an interval, or specify interval: 0, that last condition will never be satisfied! So for an app that logs infrequently, you must specify an interval.

  cloudwatchStream({
    group: CLOUDWATCH_NODEJS_LOG_GROUP,
    aws_access_key_id: AWS_ACCESS_KEY_ID,
    aws_secret_access_key: AWS_SECRET_ACCESS_KEY,
    aws_region: AWS_REGION,
    interval: 1000, // This is required.
  })

Note that this will make it so every time you send a log to pino-cloudwatch, it will wait 1000ms before sending. If you log another message in that interval, the timer will reset. If you log something every 900ms, your logs will not get sent until you reach one of the other conditions - you could be waiting a couple of hours. As this package is currently written, there's no fix for this - there's no option to specify a "maximum interval between sends".

jwalton avatar Nov 03 '21 15:11 jwalton

I guess this project is dead...

I've created a fork to fix some issues and eventually port it to TypeScript:

https://www.npmjs.com/package/pino-aws-cloudwatch

Looking forward to PRs.

AlexZeitler avatar Nov 29 '21 22:11 AlexZeitler

@jwalton I tried your solution but it does not work:

const stream = PinoCloudWatch({
    group: 'test',
    aws_access_key_id: process.env.AWS_ACCESS_KEY_ID,
    aws_secret_access_key: process.env.AWS_SECRET_ACCESS_KEY,
    interval: 1000,
    aws_region: 'eu-central-1'
  });
const logger = pino(
    {
      level: 'debug',
      serializers: pino.stdSerializers,
    },
    stream,
  );

I'm using pino v8.11.0.

slim-hmidi avatar May 22 '23 14:05 slim-hmidi

I made a package myself using streams and works great. I'll try to publish it once I have some free time.

demian85 avatar May 22 '23 14:05 demian85

@demian85 can you share the config you did to make the connection with cloudWatch please?

slim-hmidi avatar May 23 '23 10:05 slim-hmidi