Storing log file in AWS S3 from Spring boot application

amazon-s3, amazon-web-services, java, log4j, spring

Solution

    import com.amazonaws.services.s3.AmazonS3;
    import com.amazonaws.services.s3.model.PutObjectRequest;
    import org.springframework.core.env.Environment;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.scheduling.annotation.Scheduled;


@Autowired
    private AmazonS3 s3Client;
@Scheduled(cron = "0 5 1 * * *")
public void moveLogsFromEC2ToS3() {
    try {
        File logsDir = new File(env.getProperty("AWS_EC2_LOG_PATH"));
        for (File logFile : logsDir.listFiles()) {
            String fileName = logFile.getName();
            if (fileName.endsWith(".log")) {
                s3Client.putObject(new PutObjectRequest(env.getProperty("AWS_S3_LOGS_BUCKET_NAME"), fileName, logFile));
            }
        }
    } catch (Exception e) {
        logger.error("Error in moving log files! : {}", e);
    }
}

Problem

I am building a Spring-boot application where in all the logging is stored in a specified path in log4j.properties. I am hosting this application on AWS Beanstalk. Once I host the application on the AWS, the specified path for the log file will become invalid. How can I resolve this issue where in the log file should also get stored in a different path in cloud, say Amazon S3, but not on server as the log file takes considerable amount of size. ``` log4j.appender.file.File=/my_log.log ``` How can I change the above line to store the "my_log.log" in AWS S3?

Original source