Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Log4j level using environment variables in AWS Lambda (java)

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

like image 256
Sach Avatar asked Oct 17 '22 18:10

Sach


1 Answers

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.

like image 51
Ivan Koryshev Avatar answered Oct 30 '22 03:10

Ivan Koryshev