2727import java .io .Closeable ;
2828import java .io .File ;
2929import java .io .IOException ;
30- import java .nio .file .Path ;
31- import java .nio .file .Paths ;
3230
3331import lombok .extern .slf4j .Slf4j ;
3432
4038@ Slf4j
4139public class OssRemoteLogHandler implements RemoteLogHandler , Closeable {
4240
43- private static final int OBJECT_NAME_COUNT = 2 ;
44-
4541 private OSS ossClient ;
4642
4743 private String bucketName ;
4844
49- public OssRemoteLogHandler () {
45+ private static OssRemoteLogHandler instance ;
46+
47+ private OssRemoteLogHandler () {
48+
49+ }
50+
51+ public static synchronized OssRemoteLogHandler getInstance () {
52+ if (instance == null ) {
53+ instance = new OssRemoteLogHandler ();
54+ instance .init ();
55+ }
56+
57+ return instance ;
5058 }
5159
5260 public void init () {
@@ -61,7 +69,7 @@ public void init() {
6169
6270 @ Override
6371 public void sendRemoteLog (String logPath ) {
64- String objectName = getObjectNameFromLogPath (logPath );
72+ String objectName = RemoteLogUtils . getObjectNameFromLogPath (logPath );
6573
6674 try {
6775 log .info ("send remote log {} to OSS {}" , logPath , objectName );
@@ -74,7 +82,7 @@ public void sendRemoteLog(String logPath) {
7482
7583 @ Override
7684 public void getRemoteLog (String logPath ) {
77- String objectName = getObjectNameFromLogPath (logPath );
85+ String objectName = RemoteLogUtils . getObjectNameFromLogPath (logPath );
7886
7987 try {
8088 log .info ("get remote log on OSS {} to {}" , objectName , logPath );
@@ -91,18 +99,6 @@ public void close() throws IOException {
9199 }
92100 }
93101
94- private String getObjectNameFromLogPath (String logPath ) {
95- Path path = Paths .get (logPath );
96- int nameCount = path .getNameCount ();
97-
98- if (nameCount < OBJECT_NAME_COUNT ) {
99- return Paths .get (readOssBaseDir (), logPath ).toString ();
100- } else {
101- return Paths .get (readOssBaseDir (), path .subpath (nameCount - OBJECT_NAME_COUNT , nameCount ).toString ())
102- .toString ();
103- }
104- }
105-
106102 private void checkBucketNameExists (String bucketName ) {
107103 if (StringUtils .isBlank (bucketName )) {
108104 throw new IllegalArgumentException (Constants .REMOTE_LOGGING_OSS_BUCKET_NAME + " is empty" );
@@ -136,8 +132,4 @@ private String readOssEndpoint() {
136132 private String readOssBucketName () {
137133 return PropertyUtils .getString (Constants .REMOTE_LOGGING_OSS_BUCKET_NAME );
138134 }
139-
140- private String readOssBaseDir () {
141- return PropertyUtils .getString (Constants .REMOTE_LOGGING_BASE_DIR );
142- }
143135}
0 commit comments