Skip to main content

SL4J and parameterized messages

It's common to log messages that contain run-time information. In Java, this is easier if you use SL4J instead of log4j.


Here's a useful snippet from http://javarevisited.blogspot.com/2013/08/why-use-sl4j-over-log4j-for-logging-in.html

This is how you would do in Log4j, but surely this is not fun and reduce readability of code by adding unnecessary boiler-plate code.


if (logger.isDebugEnabled()) {
    logger.debug("Processing trade with id: " + id + " symbol: " + symbol);
}


On the other hand if you use SLF4J, you can get same result in more concise format as shown below:


logger.debug("Processing trade with id: {} and symbol : {} ", id, symbol);


In SLF4J, we don't need String concatenation and don't incur cost of temporary not need String. Instead, we write log message in a template format with placeholder and supply actual value as parameters.



Comments

Popular posts from this blog

Running 2560x1080 on LG 29UM57-P on an old MacBook Pro OSX El Capitan

Yes. A mid-2011 MacBook Pro running OSX El Capitan can drive a 2560x1080 monitor via HDMI. I was not able to get 60 Hz working. I am settling with 53 Hz. I tried many settings before finding something that works. The refresh rate seems fine to me. Maybe the text could be clearer but for a $200 monitor running this resolution, I'll take it. Apple MacBook 2015 retina resolution is 2560 x 1800. Consider buying a better monitor that may literally give you fewer headaches. Install  SwitchResX Follow the instructions for disabling SIP After rebooting, run SwitchResX via System Preferences Select your LG monitor Click Custom Resolutions Click the + icon  Enter these settings  ( source ) Exit SwitchResX and save Reboot Run SwitchResX via System Preferences Select your LG monitor Click Current Resolutions Select the 2560x1080, 53 Hz setting Enable SIP If you discover better settings, please leave a comment.

MySQL: This stored procedure inserts 'empty' rows into a specified table

This stored procedure accepts two arguments: a table name and a row count. It inserts the specified number of rows into the specified table. Of course, all columns in the table must either have default values or allow NULL. DROP PROCEDURE InsertDimension; DELIMITER $$ CREATE PROCEDURE InsertDimension(IN TableName VARCHAR(50), IN NumRows INT) BEGIN DECLARE i INT; SET i = 1; SET @sql_text = concat('INSERT INTO ', TableName, ' VALUES ()' ); PREPARE stmt FROM @sql_text; START TRANSACTION; WHILE i <= NumRows DO EXECUTE stmt; SET i = i + 1; END WHILE; COMMIT; DEALLOCATE PREPARE stmt; END$$ DELIMITER ;

Terris' Spicy Vegan Lentil "Soup"

Lentils with Indian flavors Most of the liquid will be absorbed by the lentils so this dish will be more of a cassole than a soup. This recipe yields 10-12 large bowls. Great for leftovers and lunch-at-work! Credits: I started with this recipe . Estimates: Prep time: 30-45 mins, depending on whether you have help chopping or drinking Cook time: 20 mins Ingredients 1 bay leaf 1 Tbsp salt 2 tsp cumin 1 tsp curry powder 2 tsp roasted paprika 1/2 tsp garam masala 1 tsp roasted chipotle powder  4 yellow potatoes 6 oz crimini mushrooms, sliced 1 cup dry lentils 1 fresh jalapeƱo finely chopped 1 large yellow onion 1 pound cherry tomatoes - or add two more tomatoes 1 large whole tomato 3 carrots, chopped 1 lb cauliflower, chopped 1 lb broccoli florets, chopped 6 oz tomato paste 2 Tbsp olive oil 1   lemon, juiced (about 2 tablespoons) 3 cups vegetable broth Instructions Sautee the oil, garlic, and onions for 5 minutes in medium-low heat in pressure coo