Preventing a Logger from Forwarding Log Records to Its Parent

By default, a logger sends a log record not only to its handlers but to all the handlers of ancestor loggers. If the effects of the parent handlers are not desired, it is necessary to prevent log records from being forwarded to the parent.

This example demonstrates how to stop a logger from sending log records to its parent.

Note: Although the level of a log record is tested against the logger's log level, this test is not done with any of the logger's parents. However, the log level of all handlers is still in effect.

// Get a logger Logger logger = Logger.getLogger("com.mycompany"); // Stop forwarding log records to ancestor handlers logger.setUseParentHandlers(false); // Start forwarding log records to ancestor handlers logger.setUseParentHandlers(true);

Comments

8 Jan 2010 - 1:07pm by Swapnil Akre (not verified)
Here is one for reference; Performs String to Date Validations & Required Date Format conversion:
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateFormatter { public static void main(String[] args) throws ParseException { boolean isDate; String ipDateFormat = "yyyy-MM-dd"; String opDateFormat = "MM/dd/yyyy"; String inDate = "2010-13-28"; String finalDate = null; isDate = isValidDate(inDate, ipDateFormat); if(isDate){ finalDate = transformDateByFormat(inDate, ipDateFormat, opDateFormat); }else{ finalDate = "Is not a date"; } System.out.println(finalDate); } public static boolean isValidDate(String inDate, String dtFormat) { if (inDate == null){ return false; } //set the format to use as a constructor argument SimpleDateFormat formatter = new SimpleDateFormat(dtFormat); if (inDate.trim().length() != formatter.toPattern().length()){ return false; } formatter.setLenient(false); try { //parse the inDate parameter formatter.parse(inDate.trim()); } catch (ParseException pe) { return false; } return true; } public static String transformDateByFormat(String inDate, String inputDateFormat, String outputDateFormat) { String strReturnDate = ""; //set the format to use as a constructor argument; Initial formatting is to parse a String to Date format SimpleDateFormat formatter = new SimpleDateFormat(inputDateFormat); formatter.setLenient(false); Date theDate = new Date(); try { // parse the inDate parameter theDate = (Date)formatter.parse(inDate); System.out.println("inputDateFormat: "+theDate); formatter = new SimpleDateFormat(outputDateFormat); // set the format to use as a constructor argument; Change the date to the required format strReturnDate = formatter.format(theDate); System.out.println("strReturnDate: "+strReturnDate); }catch (ParseException e) { e.printStackTrace(); } return strReturnDate; } }

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.