多个文件到单个文件的符号 link
Symbolic link for multiple file to single file
我有一个场景,我想将多个文件的日志重定向到同一个符号链接。
我假设有三个文件 file1.log
file2.log
和 file3.log
file1.log
有
This is file 1
file2.log
有
This is file 2
file3.log
有
This is file 3
现在我想创建所有这个文件的符号链接到 file.log
这样 file.log
的内容是
This is file 1
This is file 2
This is file 3
而且这是动态发生的。即如果 file1.log
的内容被修改,例如 This line append to file1.log
那么 file1.log
应该显示
This is file 1
This is file 2
This is file 3
This line append to file1.log
有什么标准的方法可以做到这一点吗?我在这里卡了好久了。
我正在使用 ubuntu 18.04
和 python 3.6.9
- - - - - - - - - - - - - - -编辑 - - - - - - - - - - --------------
这不是我拥有的所有文件,例如名称为 file*.log
的 20 个文件,并且可以有多个文件,例如 f1X.log
f2X.log
需要重定向到 f1.log
, f2.log
from itertools import chain
from pathlib import Path
from time import sleep
log_path_list = [p for p in Path('.').glob('*.log') if p.name != 'file.log']
log = Path('file.log')
files = []
for log_path in log_path_list:
files.append(log_path.open())
lines = chain(*map(lambda f: (line for line in f),files))
with log.open('w') as flog:
for line in lines:
flog.write(line)
flog.flush()
try:
while True:
oneline = ""
for f in files:
f.seek(0, 2)
oneline = f.readline()
if not oneline:
continue
flog.write(oneline)
if oneline:
flog.flush()
else:
sleep(0.3)
except Exception as e:
print(e)
for f in files:
f.close()
这不是符号链接,但它会将所有日志文件的所有内容输出到 file.log
,然后将任何新行附加到 file.log
。
只要 file.log
需要更新,python 进程就应该保留在那里。
我认为这不是一个明智的解决方案,但如果这是您唯一的方法,那么您就有了一个解决方案的开始。
我有一个场景,我想将多个文件的日志重定向到同一个符号链接。
我假设有三个文件 file1.log
file2.log
和 file3.log
file1.log
有
This is file 1
file2.log
有
This is file 2
file3.log
有
This is file 3
现在我想创建所有这个文件的符号链接到 file.log
这样 file.log
的内容是
This is file 1
This is file 2
This is file 3
而且这是动态发生的。即如果 file1.log
的内容被修改,例如 This line append to file1.log
那么 file1.log
应该显示
This is file 1
This is file 2
This is file 3
This line append to file1.log
有什么标准的方法可以做到这一点吗?我在这里卡了好久了。
我正在使用 ubuntu 18.04
和 python 3.6.9
- - - - - - - - - - - - - - -编辑 - - - - - - - - - - --------------
这不是我拥有的所有文件,例如名称为 file*.log
的 20 个文件,并且可以有多个文件,例如 f1X.log
f2X.log
需要重定向到 f1.log
, f2.log
from itertools import chain
from pathlib import Path
from time import sleep
log_path_list = [p for p in Path('.').glob('*.log') if p.name != 'file.log']
log = Path('file.log')
files = []
for log_path in log_path_list:
files.append(log_path.open())
lines = chain(*map(lambda f: (line for line in f),files))
with log.open('w') as flog:
for line in lines:
flog.write(line)
flog.flush()
try:
while True:
oneline = ""
for f in files:
f.seek(0, 2)
oneline = f.readline()
if not oneline:
continue
flog.write(oneline)
if oneline:
flog.flush()
else:
sleep(0.3)
except Exception as e:
print(e)
for f in files:
f.close()
这不是符号链接,但它会将所有日志文件的所有内容输出到 file.log
,然后将任何新行附加到 file.log
。
只要 file.log
需要更新,python 进程就应该保留在那里。
我认为这不是一个明智的解决方案,但如果这是您唯一的方法,那么您就有了一个解决方案的开始。