4040import java .io .File ;
4141import java .io .FileFilter ;
4242import java .io .IOException ;
43+ import java .nio .file .Path ;
4344import java .util .ArrayList ;
4445import java .util .Arrays ;
4546import java .util .HashMap ;
@@ -94,7 +95,7 @@ public class LogStream {
9495
9596 // The descriptor of the LogStream which consists of SingleLog name and LogStream name.
9697 // It is globally unique descriptor of the LogStream.
97- private final String logStreamDescriptor ;
98+ private String logStreamDescriptor ;
9899
99100 private Object logFilesInfoLock = new Object ();
100101
@@ -117,8 +118,25 @@ public class LogStream {
117118
118119 private long lastCompleteCycleTime ;
119120
121+ // the directory that this logStream pertains to
122+ private String dir ;
123+
120124 private FileNameMatchMode fileNameMatchMode ;
121125
126+ /**
127+ * New constructor if there are multiple directories in a config because we need to know the directory through the
128+ * full added path to derive the directory, we can no longer use the log config to derive the directory
129+ *
130+ * Example full added path: "/var/log/singer/singer.log.0"
131+ * With the Multi-directory feature, the directory can be "/var/log/*" and the above full added path can be used.
132+ * */
133+ public LogStream (SingerLog singerLog , Path fullAddedPath ) {
134+ this (singerLog , fullAddedPath .toFile ().getName ());
135+ this .dir = SingerUtils .extractDirectoryPath (fullAddedPath .toString ());
136+ this .logStreamDescriptor = String .format ("%s:%s:%s" , singerLog .getLogName (), fileNamePrefix , this .dir );
137+ this .fullPathPrefix = FilenameUtils .concat (dir , fileNamePrefix );
138+ }
139+
122140 public LogStream (SingerLog singerLog , String fileNamePrefix ) {
123141 Preconditions .checkArgument (!Strings .isNullOrEmpty (fileNamePrefix ));
124142
@@ -129,7 +147,8 @@ public LogStream(SingerLog singerLog, String fileNamePrefix) {
129147 this .logFilePathsIndex = new HashMap <>();
130148 this .lastStreamModificationTime = -1L ;
131149
132- String dir = this .singerLog .getSingerLogConfig ().getLogDir ();
150+ this .dir = this .singerLog .getSingerLogConfig ().getLogDir ();
151+ this .logStreamDescriptor = String .format ("%s:%s:%s" , singerLog .getLogName (), fileNamePrefix , this .dir );
133152 this .fileNameMatchMode = singerLog .getSingerLogConfig ().getFilenameMatchMode ();
134153 this .fileNamePrefix = fileNamePrefix ;
135154 this .fullPathPrefix = FilenameUtils .concat (dir , fileNamePrefix );
@@ -147,7 +166,7 @@ public int size() {
147166 public void initialize () throws IOException {
148167 SingerLogConfig singerLogConfig = singerLog .getSingerLogConfig ();
149168 String regexStr = fileNamePrefix ;
150- File logDir = new File (singerLogConfig . getLogDir () );
169+ File logDir = new File (dir );
151170
152171 if (singerLogConfig .getFilenameMatchMode () == FileNameMatchMode .PREFIX ) {
153172 regexStr += ".*" ;
@@ -283,7 +302,7 @@ public void setLatestProcessedMessageTime(long timestamp) {
283302 }
284303
285304 public String getLogDir () {
286- return this .singerLog . getSingerLogConfig (). getLogDir () ;
305+ return this .dir ;
287306 }
288307
289308 /**
0 commit comments