Monday, August 27, 2012

Java: Logging in single line for grep

Have you ever tried to grep a java application log? So what do you think about multiline log entries? Yes it's painful.

Here I'll show you my logback configuration useful to avoid this problem and grep the log easily.


    
        
            %-30(%d [%thread]) %-5level %logger{16} - %replace(%msg){'\n','|'} %xEx%n
        
    

    
        
    

    


The class below produces logs like this. Too bad that even if you try to log in single line the Exception stack trace adding '%replace' in %xEx, it works only for the first stack trace line, if you have a solution please tell me.
package com.googlecode.log.pretty;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class App {
 private static final Logger log = LoggerFactory.getLogger(App.class);
 
 public static void main(String[] args) {
  log.info("Hello World!");
  String message = "I'm a message\nI m a new line\n\tI'm a tabbed new line";
  log.trace(message);
  log.debug(message);
  log.info(message);
  log.warn(message);
  log.error(message);
  log.error("I'm an error", new Exception("To see stackTrace"));
  
 }
}
2012-08-18 16:48:25,156 [main] INFO  c.g.l.pretty.App - Hello World! 
2012-08-18 16:48:25,159 [main] TRACE c.g.l.pretty.App - I'm a message|I m a new line| I'm a tabbed new line 
2012-08-18 16:48:25,159 [main] DEBUG c.g.l.pretty.App - I'm a message|I m a new line| I'm a tabbed new line 
2012-08-18 16:48:25,159 [main] INFO  c.g.l.pretty.App - I'm a message|I m a new line| I'm a tabbed new line 
2012-08-18 16:48:25,159 [main] WARN  c.g.l.pretty.App - I'm a message|I m a new line| I'm a tabbed new line 
2012-08-18 16:48:25,159 [main] ERROR c.g.l.pretty.App - I'm a message|I m a new line| I'm a tabbed new line 
2012-08-18 16:48:25,162 [main] ERROR c.g.l.pretty.App - I'm an error java.lang.Exception: Oh mi grep styackTrace
 at com.googlecode.log.pretty.App.main(App.java:21) [classes/:na]
How to grep and print the original message
cat output.log | grep -E 'INFO|WARN|ERROR' | tr "|" "\n"

Monday, August 20, 2012

TopCoder Eclipse plugin

Have you ever tried topcoder's programming contests? If your answer to this question is yes, maybe you are thinking that topcoder Java client is not so good. If you like programming in Eclipse maybe you'll like the EclipseCoder plugin.


This plugin will let you do topcoder contests in C++, Java, and Python. When you choose a problem the plugin automatically creates a Java class ready for testing with automatically created JUnit tests based on topcoder examples.



I've tried EclipseCoder plugin for Java in Eclipse 4.2, and everything is gone fine.

Saturday, August 11, 2012

Lucene in Google App Engine (LAE)

I'm glad to announce that now it's possible to use the Apache Lucene framework in Google App Engine thanks to my project hosted on googlecode: lucene-appengine (LAE).


Main features:
  • Lucene 3.6.x compatible
  • Storage in Google App Engine
  • Supported operations: Add, Remove, Update, Index, Deindex, everything?! (I need you to check)
  • Multiple indexes in the same application
  • No more RAMDirectory for Google App Engine powered applications
  • No more index size limit of 1MB
  • Yes! It works, see it in action (demo site)
  • Open your mind to new Google App Engine applications powered by Lucene

Project resources:

Coming Soon:
  • Performance improvements
    • asynch indexing
    • buffer management

I'll post also some interesting examples about how to use appengine taskqueue based indexing technique.
I hope I'll be able to follow every major release of Apache Lucene.

Related posts:

Sunday, August 5, 2012

Maven: pom.xml template for new projects

In this post I'll show you a snippet that I use for every new maven project. Thanks to this configuration you can minimize the always verbose pom.xml. Using the right properties you can automatically set default values for many maven plugins, so you can avoid writing configuration tags for every plugin. Using the pluginManagement tag every child pom inherits plugin version and all the properties.

I hope that everyone using this pom will check for new versions of the plugins specified below and if you'll find any new version on the (official maven plugins site) please comment this post and I'll update this snippet. Remember that using the latest version of plugins can improve your building time.


    
        
            
                
                    maven-clean-plugin
                    2.5
                
                
                    maven-compiler-plugin
                    3.0
                
                
                    maven-resources-plugin
                    2.6
                
                
                    maven-dependency-plugin
                    2.6
                
                
                    maven-source-plugin
                    2.2.1
                
                
                    maven-war-plugin
                    2.3
                
                
                    maven-failsafe-plugin
                    2.13
                
                
                    maven-surefire-plugin
                    2.13
                
            
        
    
    
        UTF-8
        1.6
        1.6