pinojs / pino

🌲 super fast, all natural json logger
http://getpino.io
MIT License
14.21k stars 875 forks source link

Can't send logs to CloudWatch without using aws lambda functions. #1721

Open slim-hmidi opened 1 year ago

slim-hmidi commented 1 year ago

I'm trying to send logs to cloudWatch without using pinoLambda libraries because I don't have any lambda functions in my project and I wrote this code:

import pino, { Logger } from 'pino';
import type { Level } from 'pino';
import PinoCloudWatch from 'pino-cloudwatch';

type CreateLoggerType = {
  serviceName?: string;
  functionName?: string;
  level?: Level;
};

const createLogger = ({
  serviceName,
  level = (process.env.LOG_LEVEL ?? 'debug') as Level,
  functionName,
}: CreateLoggerType = {}): Logger => {

  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: level,
      base: {
        service: serviceName,
        functionName,
      },
      serializers: pino.stdSerializers,
    }, stream
  );

  return logger;
};

const logger = createLogger();

I know that pino-cloudwatch is not maintained by pino community but I tried to find another solution for this issue and I haven't found something in the documentation that can help me to accomplish my goal. Is there any suggestion or example to send logs to cloudWatch?

mcollina commented 1 year ago

What's your setup? What AWS service are you using to deploy your code?

slim-hmidi commented 1 year ago

@mcollina the code is deployed using AWS ECS.

mcollina commented 1 year ago

You can configure AWS to send all logs to cloudwatch logs by default:

https://docs.aws.amazon.com/AmazonECS/latest/developerguide/using_awslogs.html