I'm trying to upload local logs generated by log4j2 to AWS S3 bucket without storing logs in my local.
I want to make log4j2's logs transfer to S3 bucket directly.
The method what I think is putting console logs to ByteBuffer, and upload to S3 bucket using by AWS JAVA sdk v2.
here is my code.
package com.ibiz.logging;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.Appender;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class S3LoggingApplication {
// log instance create
final static Logger logs = LogManager.getLogger(S3LoggingApplication.class);
// why has to use string[] args?
public static void main(String[] args) throws InterruptedException {
// log gen
logs.info("Log Gen Start!!!");
logs.info("Log Gen Start!!!");
logs.info("Log Gen Start!!!");
logs.info("Log Gen Start!!!");
Long started = System.currentTimeMillis();
Long now = System.currentTimeMillis();
// loop for 3 min
// while (now - started < TimeUnit.MINUTES.toMillis(3)) {
// logs.info("You knock kncok mineral!");
// logs.warn("We are under attack!!");
// logs.error("Nuclear launch detected!!");
// logs.trace("GG");
// // term 7 sec
// Thread.sleep(TimeUnit.SECONDS.toMillis(7));
// }
// stream
// InputStream input = new ByteArrayInputStream();
// input byte buffer
// ByteBuffer byteBuffer = ByteBuffer.wrap();
// log gotcha
// upload
// S3Client s3 = S3Client.create();
// TODO upload function
// s3.putObject(PutObjectRequest.builder().bucket("jk.test").key("jk").build(), RequestBody.fromByteBuffer(byteBuffer));
}
}