重定向批量错误输出到file [英] Redirect batch stderr to file

查看:181
本文介绍了重定向批量错误输出到file的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个执行java应用程序的批处理文件。我试图修改它,以便每当发生异常时,它会写出来STDERR到一个文件中。

这看起来是这样的:

 启动Java something.jar方法%1%2 2  - ;> log.txt的

有没有一种方法我可以写参数%1%2到log.txt文件中呢?我不想将它写入日志文件每次这个批处理文件被调用,只有当发生异常。

我试图寻找一种方法将STDERR重定向到一个变量,但我无法弄清楚。理想情况下,我想日志文件看起来是这样的:

 调用参数批处理文件:
- 第一个参数
- 第二ARG
例外:
java.io.exception等..------------------------------------调用参数批处理文件:
- 第一个参数
- 第二ARG
例外:
java.io.exception等..


解决方案

这样的事情可能工作:

javastart.cmd

 关闭@echo
集PARAMS =%*
FOR / Fdelims =%% E在('的Java something.jar方法%1%2 ^> NUL')做(
    所谓带参数的回声批处理文件:>>的log.txt
    回声 - 参数数量[]:%*>>的log.txt
    回声 - 精氨酸[1]:%1>>的log.txt
    回声 - 精氨酸[2]:%2 - ;> log.txt的
    回声异常:%%ê

我不是一个Java程序员自己,我不能对此进行测试输出/情况,但:

1 %* 意味着每个参数,从1日到最后一个(甚至是12%,虽然它不能直接使用。)无论格式化。可能是更好的使用这个比那些分隔。在恶劣的间距/报价的,甚至,你有充分的参数了。我在日志中添加一条线,充分展现线接参数。你能看到的问题是,如果它是坏的争论问题。

2 发送标准输出为空(^> NUL)将只保留stderr输出,设置成 %%è

3 无论是在部分只会发生是在测试声明实际上有什么作为输出,也就是说,如果< STRONG> %%è设置。

使用那些虽这么说,你必须测试脚本,看看是否异常真正被发送到标准错误,像其他一些软件,到标准输出,即使发生错误。

我希望这有助于。

I have a batch file that executes a java application. I'm trying to modify it so that whenever an exception occurs, it'll write the STDERR out to a file.

It looks something like this:

start java something.jar method %1 %2 2>> log.txt

Is there a way I can write the arguments %1 and %2 to the log.txt file as well? I don't want to write it to the log file everytime this batch file gets called, only when an exception occurs.

I tried searching for a way to redirect STDERR into a variable, but I couldn't figure it out. Ideally I'd like the log file to look something like:

Batch file called with parameters:
- "first arg"
- "second arg"
Exception: 
java.io.exception etc...

------------------------------------

Batch file called with parameters:
- "first arg"
- "second arg"
Exception: 
java.io.exception etc...

解决方案

Something like this might work:

javastart.cmd

@echo off
set params=%*
for /f "delims=" %%e in ('java something.jar method %1 %2 ^>nul') do (
    echo Batch file called with parameters:>>log.txt
    echo - Args[]: %*>>log.txt
    echo - Arg[1]: %1>>log.txt
    echo - Arg[2]: %2>>log.txt
    echo Exception: %%e
)

I am not a java programmer myself, I cannot test this output/situation but:

1: %* means every parameters, from 1st to last (even %12 although it's not directly available.. ) whatever the formating. Might be better to use this than delimit those. In the even of bad spacing/quoting, you would have the full parameters too. I added a line in the log, to show the full line then the parameters. You can see where the problem was if it's a matter of bad arguments.

2: sending the stdout to null (^>nul) will only keep the stderr output, set into %%e

3: Whatever is in the do part will only happen is the for test statement actually has anything as an output, i.e. if %%e is set.

With those being said, you have to test the script and see if the exception is actually sent to stderr or, like some other softwares, to stdout even if an error occured.

I hope this helps.

这篇关于重定向批量错误输出到file的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆