java Ant日志中的时间戳?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1246239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Timestamps in Ant log?
提问by Tiger
Is there an easy way to have the Ant logger (default or other) add a timestamp to each message?
是否有一种简单的方法可以让 Ant 记录器(默认或其他)为每条消息添加时间戳?
The only way I can think of is to use the Log4jListener and have its settings include the timestamp. Or write a custom logger that subclasses DefaultLogger and writes the timestamp. If there's a better or easier way (preferably without requiring that users install a new jar file into their Ant lib directory),
我能想到的唯一方法是使用 Log4jListener 并使其设置包括时间戳。或者编写一个自定义记录器,它继承 DefaultLogger 并写入时间戳。如果有更好或更简单的方法(最好不需要用户将新的 jar 文件安装到他们的 Ant lib 目录中),
I'd be interested in hearing about it.
我很想听听它。
采纳答案by Rich Seller
You can define an Ant macrodef to set the current timestamp, then call the macrodef each time you need to reference it throughout your build.xml
您可以定义一个 Ant 宏定义来设置当前时间戳,然后在每次需要在整个 build.xml 中引用它时调用该宏定义
The following macrodef will set the timestamp to a property (you can add an attribute to the macrodef if you want to customise the property it sets):
下面的 macrodef 将时间戳设置为一个属性(如果你想自定义它设置的属性,你可以向 macrodef 添加一个属性):
<macrodef name="set.timestamp">
<sequential>
<tstamp>
<format property="current.time" pattern="MM/dd/yyyy hh:mm"/>
</tstamp>
</sequential>
</macrodef>
Then to use it, just access the property set by the macrodef as you need:
然后要使用它,只需根据需要访问由 macrodef 设置的属性:
<target name="doFoo" depends="dir.check" if="dir.exists">
<set.timestamp/>
<!--in this example, just echo the timestamp -->
<echo message="${current.time}"/>
</target>
For more information on ant macrodefs, check out the documentation.
有关 ant 宏定义的更多信息,请查看文档。
回答by Gavin Clarke
Given properties are immutable in ant you need to do something a bit funky here otherwise you just end up logging the same timestamp again and again.
给定的属性在 ant 中是不可变的,你需要在这里做一些时髦的事情,否则你最终会一次又一次地记录相同的时间戳。
Using antcall gives you a fresh session which means you can reuse the property, although it is a little clumsy.
使用 antcall 为您提供了一个全新的会话,这意味着您可以重用该属性,尽管它有点笨拙。
<macrodef name="timestamp.echo">
<attribute name="message"/>
<sequential>
<antcall target="_timestamp.echo">
<param name="message" value="@{message}" />
</antcall>
</sequential>
</macrodef>
<target name="_timestamp.echo">
<tstamp>
<format property="current.time" pattern="dd/MM/yyyy hh:mm:ss"/>
</tstamp>
<echo message="${current.time} ${message}"/>
</target>
If you are using Ant 1.8 then you can use local which is much cleaner
如果您使用的是 Ant 1.8,那么您可以使用更干净的 local
<macrodef name="timestamp.echo">
<attribute name="message"/>
<sequential>
<local name="current.time" />
<tstamp>
<format property="current.time" pattern="dd/MM/yyyy hh:mm:ss"/>
</tstamp>
<echo message="${current.time} @{message}" />
</sequential>
</macrodef>
And here is how you can use it
这是您如何使用它
<target name="testTsEcho" depends="init" description="blah">
<timestamp.echo message="test" />
<sleep seconds="10" />
<timestamp.echo message="test2" />
</target>
回答by Sudharma Karekar
Try this
试试这个
ant -logger org.apache.tools.ant.listener.ProfileLogger
ant -logger org.apache.tools.ant.listener.ProfileLogger
It prints the entry time and exit time for each target along with the time taken for each target in ms.
它打印每个目标的进入时间和退出时间以及每个目标花费的时间(以毫秒为单位)。
回答by Niek
I like the macrodef solution if indeed it is more efficient than the target one, but I use a "var unset=true" to force a resetting of the variable, like:
我喜欢 macrodef 解决方案,如果它确实比目标解决方案更有效,但我使用“var unset=true”来强制重置变量,例如:
<macrodef name="echoTimestamp">
<sequential>
<var name="current.time" unset="true"/>
<tstamp>
<format property="current.time" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>
<echo message="${current.time}" />
</sequential>
</macrodef> <!-- end echoTimestamp -->
usage
用法
<echoTimestamp />
<sleep seconds="3"/>
<echoTimestamp />
回答by KarolDepka
Look at these loggers: http://ant.apache.org/manual/listeners.html(also one - org.apache.tools.ant.listener.ProfileLogger - is mentioned in the answer before mine). But this seem to require a new-enough version of Ant.
看看这些记录器:http: //ant.apache.org/manual/listeners.html(也是一个 - org.apache.tools.ant.listener.ProfileLogger - 在我之前的答案中提到)。但这似乎需要一个足够新的 Ant 版本。

