Добавить
Уведомления

LA VERITA' sulla STRAGE DI USTICA (aggiornato ai dati del 2023)

1. A simple Lombok @Slf4j annotation example 2. Lombok’s @Slf4j generates a logger instance with default name log using the SLF4J API. Note that we must also include a library that implements the Slf4j API. @Slf4j public class LombokSlf4jDemo1 { public Double getSurcharge(Double transaction) { log.info("Surcharge Caliculation begins"); log.debug("Surcharge amount " + transaction); if (transaction.isNaN(transaction)) { log.error(transaction + " is not a valid amount"); throw new RuntimeException("Invalid Transaction"); } return Math.PI; } } 2.2. Lombok generated / DeLomoked code for LombokSlf4jDemo1.java public class LombokSlf4jDemo1 { private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LombokSlf4jDemo1.class); public Double getSurcharge(Double transaction) { log.info("Surcharge Caliculation begins"); log.debug("Surcharge amount " + transaction); if (transaction.isNaN(transaction)) { log.error(transaction + " is not a valid amount"); throw new RuntimeException("Invalid Transaction"); } return Math.PI; } } 2.3. Testing LombokSlf4jDemo1.java : public class LombokSlf4JDemo1Test { public static void main(String[] args) { new LombokSlf4jDemo1() .getSurcharge(0/0.0); } } 2.4. Output Results : 20:15:33.631 [main] INFO c.j.lombok.log.LombokSlf4jDemo1 - Surcharge Caliculation begins 20:15:33.674 [main] DEBUG c.j.lombok.log.LombokSlf4jDemo1 - Surcharge amount NaN 20:15:33.677 [main] ERROR c.j.lombok.log.LombokSlf4jDemo1 - NaN is not a valid amount Exception in thread "main" java.lang.RuntimeException: Invalid Transaction at com.javabydeveloper.lombok.log.LombokSlf4jDemo1.getSurcharge(LombokSlf4jDemo1.java:15) at com.javabydeveloper.demo.log.LombokSlf4JDemo1Test.main(LombokSlf4JDemo1Test.java:10) 2. Changing Lombok Logger field name using configuration By default Lombok generates static Logger instance with name log. If you wants to change Logger filed name, following configuration in lombok.config file allows you to to do so. Now we have to get Logger by using name logger. lombok.config : # default value is "log" lombok.log.fieldName = logger # default value is "true" lombok.log.fieldIsStatic = false DeLomoked code for LombokSlf4jDemo1.java after config change : public class LombokSlf4jDemo1 { private final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(LombokSlf4jDemo1.class); public Double getSurcharge(Double transaction) { log.info("Surcharge Caliculation begins"); log.debug("Surcharge amount " + transaction); if (transaction.isNaN(transaction)) { log.error(transaction + " is not a valid amount"); throw new RuntimeException("Invalid Transaction"); } return Math.PI; } } 3. Custom Logger using Lombok @CustomLog To use custom logger we need to use @CustomLog annotation and following configuration in lombok.config file. lombok.log.custom.declaration = LoggerType LoggerFactoryType.factoryMethod(TYPE|NAME|TOPIC|NULL)(TYPE|NAME|TOPIC|NULL) Example : I am using java.util.logging.Logger with following config for the logging to run a quick test. lombok.log.custom.declaration = java.util.logging.Logger java.util.logging.Logger.getLogger(NAME)(TOPIC) lombok.config : # default value is "log" lombok.log.fieldName = logger # default value is "true" lombok.log.fieldIsStatic = true lombok.log.custom.declaration = java.util.logging.Logger java.util.logging.Logger.getLogger(NAME)(TOPIC) Lomboked LombokSlf4jDemo2.java : @CustomLog public class LombokSlf4jDemo2 { public Double getSurcharge(Double transaction) { logger.info("Surcharge Caliculation begins"); logger.info("Surcharge amount " + "transaction"); if (transaction.isNaN(transaction)) { logger.warning(transaction + " is not a valid amount"); throw new RuntimeException("Invalid Transaction"); } return Math.PI; } } DeLomboked LombokSlf4jDemo2.java : public class LombokSlf4jDemo2 { private static final java.util.logging.Logger logger = java.util.logging.Logger.getLogger(LombokSlf4jDemo2.class.getName()); public Double getSurcharge(Double transaction) { logger.info("Surcharge Caliculation begins"); logger.info("Surcharge amount " + "transaction"); if (transaction.isNaN(transaction)) { logger.warning(transaction + " is not a valid amount"); throw new RuntimeException("Invalid Transaction"); } return Math.PI; } }

Иконка канала Java Бест Практики
2 подписчика
12+
14 просмотров
2 года назад
5 декабря 2023 г.
12+
14 просмотров
2 года назад
5 декабря 2023 г.

1. A simple Lombok @Slf4j annotation example 2. Lombok’s @Slf4j generates a logger instance with default name log using the SLF4J API. Note that we must also include a library that implements the Slf4j API. @Slf4j public class LombokSlf4jDemo1 { public Double getSurcharge(Double transaction) { log.info("Surcharge Caliculation begins"); log.debug("Surcharge amount " + transaction); if (transaction.isNaN(transaction)) { log.error(transaction + " is not a valid amount"); throw new RuntimeException("Invalid Transaction"); } return Math.PI; } } 2.2. Lombok generated / DeLomoked code for LombokSlf4jDemo1.java public class LombokSlf4jDemo1 { private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LombokSlf4jDemo1.class); public Double getSurcharge(Double transaction) { log.info("Surcharge Caliculation begins"); log.debug("Surcharge amount " + transaction); if (transaction.isNaN(transaction)) { log.error(transaction + " is not a valid amount"); throw new RuntimeException("Invalid Transaction"); } return Math.PI; } } 2.3. Testing LombokSlf4jDemo1.java : public class LombokSlf4JDemo1Test { public static void main(String[] args) { new LombokSlf4jDemo1() .getSurcharge(0/0.0); } } 2.4. Output Results : 20:15:33.631 [main] INFO c.j.lombok.log.LombokSlf4jDemo1 - Surcharge Caliculation begins 20:15:33.674 [main] DEBUG c.j.lombok.log.LombokSlf4jDemo1 - Surcharge amount NaN 20:15:33.677 [main] ERROR c.j.lombok.log.LombokSlf4jDemo1 - NaN is not a valid amount Exception in thread "main" java.lang.RuntimeException: Invalid Transaction at com.javabydeveloper.lombok.log.LombokSlf4jDemo1.getSurcharge(LombokSlf4jDemo1.java:15) at com.javabydeveloper.demo.log.LombokSlf4JDemo1Test.main(LombokSlf4JDemo1Test.java:10) 2. Changing Lombok Logger field name using configuration By default Lombok generates static Logger instance with name log. If you wants to change Logger filed name, following configuration in lombok.config file allows you to to do so. Now we have to get Logger by using name logger. lombok.config : # default value is "log" lombok.log.fieldName = logger # default value is "true" lombok.log.fieldIsStatic = false DeLomoked code for LombokSlf4jDemo1.java after config change : public class LombokSlf4jDemo1 { private final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(LombokSlf4jDemo1.class); public Double getSurcharge(Double transaction) { log.info("Surcharge Caliculation begins"); log.debug("Surcharge amount " + transaction); if (transaction.isNaN(transaction)) { log.error(transaction + " is not a valid amount"); throw new RuntimeException("Invalid Transaction"); } return Math.PI; } } 3. Custom Logger using Lombok @CustomLog To use custom logger we need to use @CustomLog annotation and following configuration in lombok.config file. lombok.log.custom.declaration = LoggerType LoggerFactoryType.factoryMethod(TYPE|NAME|TOPIC|NULL)(TYPE|NAME|TOPIC|NULL) Example : I am using java.util.logging.Logger with following config for the logging to run a quick test. lombok.log.custom.declaration = java.util.logging.Logger java.util.logging.Logger.getLogger(NAME)(TOPIC) lombok.config : # default value is "log" lombok.log.fieldName = logger # default value is "true" lombok.log.fieldIsStatic = true lombok.log.custom.declaration = java.util.logging.Logger java.util.logging.Logger.getLogger(NAME)(TOPIC) Lomboked LombokSlf4jDemo2.java : @CustomLog public class LombokSlf4jDemo2 { public Double getSurcharge(Double transaction) { logger.info("Surcharge Caliculation begins"); logger.info("Surcharge amount " + "transaction"); if (transaction.isNaN(transaction)) { logger.warning(transaction + " is not a valid amount"); throw new RuntimeException("Invalid Transaction"); } return Math.PI; } } DeLomboked LombokSlf4jDemo2.java : public class LombokSlf4jDemo2 { private static final java.util.logging.Logger logger = java.util.logging.Logger.getLogger(LombokSlf4jDemo2.class.getName()); public Double getSurcharge(Double transaction) { logger.info("Surcharge Caliculation begins"); logger.info("Surcharge amount " + "transaction"); if (transaction.isNaN(transaction)) { logger.warning(transaction + " is not a valid amount"); throw new RuntimeException("Invalid Transaction"); } return Math.PI; } }

, чтобы оставлять комментарии