Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timestamps in Ant log?

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.

like image 388
Tiger Avatar asked Aug 07 '09 18:08

Tiger


People also ask

What does * stands for in * Test Java in ant?

**\*.sql means "in the given directory and inside all of its subdirectories, all the files that end with .sql"

Does ant use Log4j?

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.

Are properties immutable in ant?

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.

What is the ant command?

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.


2 Answers

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.

like image 128
Sudharma Karekar Avatar answered Oct 07 '22 08:10

Sudharma Karekar


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>
like image 43
Gavin Clarke Avatar answered Oct 07 '22 09:10

Gavin Clarke