5

I have AWS Lambdas coded using Java8. I am using Log4j1.2.17 for logging needs:

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>${log4j.version}</version>
</dependency>

The logging configuration is specified using log4.properties file as follows:

# Root logger option
log4j.rootLogger=INFO, stdout

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

As you can see currently the log level is set to "info". i want to change log level using the AWS Lambda environment variables such that if Debug is needed, set some Lambda environment variable and it should be reflected in Lambda function so that it starts logging in Debug statements. Any help will be appreciated. Thanks

Sach
  • 373
  • 3
  • 9

2 Answers2

5

If you could migrate to Log4j2 (which is currently supported by AWS Lambda, you can find details on AWS GitHub), there is the "Lookups" feature which allows to use environment variables in Log4j2 configuration file: http://logging.apache.org/log4j/2.x/manual/lookups.html#EnvironmentLookup

So you could use something like this in your configuration file: "${env:VAR_NAME:-info}", where -info would be a default logging level.

Ivan Koryshev
  • 426
  • 5
  • 9
  • Thanks for the response, do you mean something like this should work? – Sach Oct 16 '18 at 06:06
  • Yes, exactly. And please note that there is a typo in Log4j2 configuration file on AWS Github -- there should be a package name in the "Configuration" tag, not a class name (see [this question](https://stackoverflow.com/questions/49973441/logging-using-log4j2-on-aws-lambda-class-not-found) for details). – Ivan Koryshev Oct 16 '18 at 16:14
-3

You can use Apache MDC(org.apache.log4j.MDC) for this purpose. Put following line of code in the start of your application/program.

MDC.put("ENV_VARIABLE_NAME", value);
meeza
  • 664
  • 1
  • 9
  • 20
  • This doesn't change the log level. i believe it can only be used in ConversionPattern, is that correct? How can i used it to change the log_level? I tried below but it did not work:: log4j.rootLogger=%X{log_level}, stdout – Sach May 16 '18 at 08:17