TailDirSource
tailDir("dirname"[, fileregex=".*"[, startFromEnd=false[, recurseDepth=0]]]{,delim="regex", delimMode="exclude|prev|next"})
:: Tails all files in a directory
dirname
that match the specified
fileregex
. Be careful and make sure because the regex argument requires java style escaping of
\ and
\". For example
\w+ would have to be written as "\\w+". If a new file appears, it is added to the list of files to tail. If pointed at a new directory, it will attempt to read all files that match! If the
startFromEnd
parameter is false, tail will re-read from the beginning of each file. If it is true, it will only start reading from the current end of each file. If the
recurseDepth
parameter is > 0 then tailDir will recurse into sub-directories. The value defines max level of a directory below the
dirname
to tail files in. Zero means do not recurse into sub-directories. Note: fileregex is applied to file names only (not including dirs), all directories (which fit
recurseDepth
parameter) are recursed into. See the section on tailing a file for details on
delim
and
delimMode
.
上面是用户指南对TailDir的用法解释。与之对应的源代码是
public EventSource build(Context ctx, String... argv) {
Preconditions
. checkArgument(argv.length >= 1 && argv. length <= 4, USAGE);
String regex = ".*"; // default to accepting all
if (argv.length >= 2) {
regex = argv[1];
}
boolean startFromEnd = false;
if (argv.length >= 3) {
startFromEnd = Boolean.parseBoolean(argv[2]);
}
int recurseDepth = 0;
if (argv.length >= 4) {
recurseDepth = Integer. parseInt(argv[3]);
Preconditions. checkArgument(recurseDepth >= 0,
"\"recurseDepth\" should be >= 0, but was: " + recurseDepth
+ ".\n" + USAGE );
}
// delim regex, delim mode
Pair<String, DelimMode> mode = TailSource. extractDelimContext(ctx);
if (mode != null) {
return new TailDirSource(new File(argv[0]), regex, startFromEnd,
recurseDepth, mode.getLeft(), mode.getRight());
}
return new TailDirSource(new File(argv[0]), regex, startFromEnd,
recurseDepth);
}
它一共需要四个参数,用监控的目录,对监控目录中文件的过滤正则,是否要从文件开始收取默认是从头开始,递归监控目录的层次默认为不递归;接下来