KyleRoss / node-lambda-log

Basic logging mechanism for Node.js Lambda Functions and other AWS services, which produces logs in JSON format for easier reading through Cloudwatch Logs.
https://lambdalog.dev
MIT License
193 stars 16 forks source link

Manage log levels #23

Closed ghost closed 5 years ago

ghost commented 5 years ago

Hello, nice package, thanks for it :)

I have a small question regarding log level management in AWS, and what would be the best approach to manage those.

I work in a 3-stages project ("dev", "staging" and "production") with AWS Lambda. I would like the "dev" stage to be the most verbose, and increase the level for the other ones. (e.g. perhaps "info" for "staging" and "warn" for "production", to be defined)

The deployment of node.js handlers is done via serverless framework where I can use environment parameters.

I was thinking to set-up a custom parameter called for instance log-level that would become the minimal level you wish to log (default to "debug")...

But how to reflect that with this package, should I write my own instance of LambdaLog programatically disabling some levels based on it:

import { LambdaLog } from 'lambda-log';

const levels = {
  debug: 0,
  info: 1,
  warn: 2,
  error: 3
};

const logLevel = process.env.LOG_LEVEL in levels
  ? levels[process.env.LOG_LEVEL]
  : levels.error;

export default new LambdaLog(
  {
    debug: Boolean(process.env.DEBUG)
  },
  {
    debug: () => logLevel <= levels.debug ? 'debug' : false,
    info: () => logLevel <= levels.info ? 'info' : false,
    warn: () => logLevel <= levels.warn ? 'warn' : false
  }
);

Or is there another way to do so?

KyleRoss commented 5 years ago

@flosch-hb That would be the correct way to handle it, yes. Since this package is not built using the same ideas of other logging packages, such as winston, there is currently no option to only log to a certain level. I do like the idea of adding it as a feature sometime in the near future, it will, of course, require some major underlying changes similar to your example in which I'm not sure is fully beneficial as a whole.

Long story short, what you wrote is the way I'd handle the same thing in one of my applications if I needed the same. I'll continue to look into a type of feature that could handle something like this for a future version.

ghost commented 5 years ago

I am completely fine with that approach, I tested it and it works, that is all I need :)

joyrexus commented 4 years ago

Having the ability to easily set minimum log level for a given environment seems key. Otherwise this logger looks nice.