我正在尝试将 log4j2 生成的本地日志上传到 AWS S3 存储桶,而不将日志存储在我的本地。
我想让 log4j2 的日志直接传输到 S3 存储桶。
我认为的方法是将控制台日志放入 ByteBuffer,并使用 AWS JAVA sdk v2 上传到 S3 存储桶。
这是我的代码。
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));
}
}