Store app logs in AWS Cloudwatch using Log4J appender

3.5k Views Asked by At

I'm trying to store application-level logs in AWS Cloudwatch logs from Java Web application.

My sample Java Web application is already having Log4J implementation. So without writing AWS SDK code, I just want to push logs to Cloudwatch. I just came across Log4J cloudwatch appender.

Followed this link but not sure how to configure AWS authentications(access/secret Keys, regions) https://dzone.com/articles/announcing-log4j-aws-appenders

log4j.properties

log4j.rootLogger=WARN, console

log4j.logger.org.apache.http=ERROR
log4j.logger.com.amazonaws=ERROR
log4j.logger.com.mkyong=ERROR
log4j.additivity.com.mkyong=true

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n

log4j.appender.cloudwatch=com.kdgregory.log4j.aws.CloudWatchAppender
log4j.appender.cloudwatch.layout=org.apache.log4j.PatternLayout
log4j.appender.cloudwatch.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n

log4j.appender.cloudwatch.logGroup=ExampleCloudwatchLog
log4j.appender.cloudwatch.logStream={startupTimestamp}-{sequence}
log4j.appender.cloudwatch.batchDelay=2500
log4j.appender.cloudwatch.rotationMode=daily

Looks like credentials are not read from my local machine which I configured using aws configure.

Note: Application would be deployed on AWS or non-AWS environments also. So not sure about setting up the AWS IAM role also.

Could anyone help me with passing the credentials of AWS?

1

There are 1 best solutions below

0
Prakash26790 On

Thanks for your valuable replay @kdgregory.

The troubleshooting guide really helped me a lot.

Enabled debugging using below and found dependency not resolved.

log4j.debug=true

Finally, I achieved by adding AWS ADK logs dependencies in pom. Works fine.

    <!-- 1/2 For AWS Cloudwatch Appender -->
    <dependency>
        <groupId>com.kdgregory.log4j</groupId>
        <artifactId>aws-appenders</artifactId>
        <version>1.3.0</version>
    </dependency>
    <!-- 2/2 For AWS Cloudwatch Appender -->
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-logs</artifactId>
        <version>1.11.130</version>
    </dependency>

Thanks,