Category: java

  • Setting log4j log level programmatically

    private static void setupLog4j() { 
      System.out.println(“setupLog4j”); 
      BasicConfigurator.resetConfiguration(); 
      // Start clean. Logger.getRootLogger().removeAllAppenders(); 
      // Create appender ConsoleAppender console = new ConsoleAppender(); 
      // Configure the appender String PATTERN = “%d –[ %p ] %l: %m%n”; 
      console.setLayout(new PatternLayout(PATTERN)); 
      console.activateOptions(); console.setName(“stdout”); 
      Logger.getRootLogger().setLevel(Level.DEBUG); 
      BasicConfigurator.configure(console); 
      LOG = Logger.getLogger(MinerTest.class); 
    }
    

  • Print java stack trace from anywhere

    Need to know which code call a specific location?
    Dump the stack trace:

    LOG.trace(ExceptionUtils.getStackTrace(new Throwable()));

  • Knowing you exception class name

    To know what is your exception class name, in case you want to have a more specific catch.

    } catch (Exception e){
    LOG.error("SQL error when processing " + requestId+". Exception class:"+ e.getClass().getCanonicalName());
    throw(e);
    }

  • Print call stack in Java from anywhere


    LOG.trace(ExceptionUtils.getStackTrace(new Throwable()));