The Holy Java

Building the right thing, building it right, fast

Posts Tagged ‘analysis’

AWK: Extract Logs for the Given Date(s) from a Log File

Posted by Jakub Holý on December 18, 2011

If your log file has entries like these:

2011-12-10T22:00:27.996+0000 [http-8080-1] INFO  my.package.MyClass Hello, I'm alive!
2011-12-11T17:05:46.811+0000 [http-8080-15] ERROR my.package.MyClass  - Error caught in DispatcherServlet
        at my.package.MyServiceClass(
2011-12-11T17:06:10.120+0000 [http-8080-14] DEBUG my.package.MyClass Whoo, that has been a long day!

Then you can use the following bash script snippet to extract logs only for a particular day or consecutive days, including everything – even lines not starting with the date such as stacktraces – between the first log of the date up to the first log of a subsequent date (default: yesterday):

if [ -z "$LOGDAY" ]; then LOGDAY=$(date +%F -d "-1 days"); fi
if [ -z "$AFTERLOGDAY" ]; then AFTERLOGDAY=$(date +%F -d "$LOGDAY +1 days"); fi
echo "Extracting logs in the range (>= $LOGDAY && < $AFTERLOGDAY) into $LOGFILE ..." awk "/^$LOGDAY/,/^$AFTERLOGDAY/ {if(!/^$AFTERLOGDAY/) print}" $LOGFILE_ORIG > $LOGFILE

This date format works on Linux. Date is very flexible and can provide dates in any format, not only yyyy-mm-dd. You may also want to read more about Awk ranges and other tips.

You would run it in one of the following ways:

$ ./ /path/to/logfile.log
$ LOGDAY=2011-12-12 AFTERLOGDAY=2011-12-17 ./ /path/to/logfile.log

Posted in General, Tools | Tagged: , , | Comments Off

Hacking Jasper to Get Object Model of a JSP Page

Posted by Jakub Holý on June 10, 2011

To perform some checks and statistical analysis on my JSPs I needed a DOM-like, hierarchical model of elements contained in them. But parsing JSP pages isn’t trivial and is best left to a tool that excels in it – the Jasper JSP compiler used by Tomcat, Jetty, GlassFish and likely also by all others. There is an easy way to tweak it to produce whatever output you need nad to transform a JSP into whatever form you want, including an object model of the page:

  1. Define a Node.Visitor subclass for handling the nodes (tags etc.) of a JSP
  2. Write a simple subclass of Compiler, overriding its generateJava() to invoke the visitor
  3. Subclass the compiler executor JspC overriding its method getCompilerClassName() to return the class of the Compiler of yours

Let’s see the code.

Read the rest of this entry »

Posted in j2ee, Languages, Tools | Tagged: , , , , | 1 Comment »

Dependency Finder 1.2.0 for Java

Posted by Jakub Holý on June 15, 2007

Dependency Finder for java can help you to find your way in unknown class files/library.


  1. Import the class files – File > Extract; wait… Note: you may need to increase the JVM’s memory.
  2. Store the extracted information about the imported classes – File > Save
  3. Select the programming elements to examine by selecting the proper checkboxes – by default it’s set to packages, you may rather want classes or packages+classes.
  4. Display a dependency ‘graph’ – File > Dependency. The symbol –> means uses, <– means is used by.
  5. You may want to limit the elements for which to show dependencies (the box Select programming elements) or their dependencies to show (the box Show dependencies (stop for closure)). The expressions use Perl regular expressions (RegExp): you specify 1 or more RegExp enclosed by ‘/’ and ‘/’ and separated by a comma. Example: classes containiny MyClass and (presumabely) packages starting with /MyClass/,/^

To run it, execute a script similar to the one below from its bin directory:

set DEPENDENCYFINDER_HOME=c:DependencyFinder-1.2.0
java %DEPENDENCYFINDER_OPTS% -Xms512m -Xmx1024m -classpath "%DEPENDENCYFINDER_HOME%classes";"%DEPENDENCYFINDER_HOME%libDependencyFinder.jar";"%DEPENDENCYFINDER_HOME%liblog4j.jar";"%DEPENDENCYFINDER_HOME%libjakarta-oro.jar";"%CLASSPATH%" com.jeantessier.dependencyfinder.gui.DependencyFinder

Posted in Languages, Tools | Tagged: , , | Comments Off