Is there an easy way to have the Ant logger (default or other) add a timestamp to each message?
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),
I'd be interested in hearing about it.
**\*.sql means "in the given directory and inside all of its subdirectories, all the files that end with .sql"
Message events are logged according to their Ant logging level, mapping directly to a corresponding Log4j level. To use Log4j you will need the Log4j JAR file and a log4j. properties configuration file. Both should be placed somewhere in your Ant classpath.
Properties are immutable: whoever sets a property first freezes it for the rest of the build; they are most definitely not variables. There are seven ways to set properties: By supplying both the name and one of value or location attributes.
It is the most complete Java build and deployment tool available. It is platform neutral and can handle platform specific properties, such as file separators. It can be used to perform platform specific tasks such as modifying the modified time of a file using 'touch' command. Ant scripts are written using plain XML.
Try this
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.
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.
Using antcall gives you a fresh session which means you can reuse the property, although it is a little clumsy.
<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
<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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With