Issue
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?
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);
}
}
Answered By - Santhosh Raj
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.