How to refactor logging statements in sibling classes?

dlogresearcher

As system evolves, the logging statements will be changed to meet the new requirements, and ideally the logging statements which have identical or very similar context should be changed consistently. But in many cases it's hard for developers to remember the existence of all of them. Then they may only change a portion of them, and forget to change the other ones consistently.

Take this Java code snippet as an example, there are two sibling classes (ChildClassA, ChildClassB) which both extend the same superclass (ParentClass), and they have a pair of similar methods which have similar functions and contain the same logging statements.

public class ChildClassA implements ParentClass{
  public void processShellCommand(){
  ...
  logger.error("Error initializing command, field " + field.getName() + " is not accessible.");
  ...
 }

public class ChildClassB implements ParentClass{
  public void processNetworkCommand(){
  ...
  logger.error("Error initializing command, field " + field.getName() + " is not accessible.");
  ...
 }

Is there a solution such as a tool, or some documents, etc. that can help the consistent changing of them?

Jocke

When it comes to logging I think you really should try to avoid putting details in the log.[whatever_leve].([message_text]] statement (at least when in comes to errors), instead you want to create your own Exception classes and put message details in them. Have a filter/interceptor for dealing with logging of unexpected exceptions is also a good practice.

So in you code example that would be that the sub-classes thrown a typed Exception, lets call it InitializingException(...). It is then up the the caller or a filter to deal with it and log it.

You want to care about the logging part of your code base in the same way as you do for business-logic code (one can argue it is part of it), so salute DRY (do-not-repeat-yourself).

Same logic applies for Debug and Trace statements as well, you don't want to copy-paste the same message across the system. So general refactoring should be applied to avoid it. But I generally think the a debug or trace message is likely to change (it is up to the developer).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to refactor these if else statements to a function

From Dev

How to refactor long if/return statements in Ruby?

From Dev

How can I refactor a set of ugly if statements?

From Dev

How to refactor long if/return statements in Ruby?

From Dev

How to refactor consecutive, independent, if statements in this algorithm?

From Dev

How to refactor factory classes using lambda expressions?

From Dev

How to refactor these classes to interact each other?

From Dev

How to refactor try-catch with logging in abstract class?

From Dev

How to correctly refactor this big and with very similar code if/else statements?

From Dev

How do I refactor out GOTO statements in GWBASIC code?

From Dev

Refactor If/Else Statements

From Dev

Is there a way to refactor these if statements?

From Dev

How do these statements turn on qooxdoo logging?

From Dev

How to enable automatic logging of SQL statements with Persistent

From Dev

How to refactor those basic classes (PyQt5)

From Dev

How to refactor helper methods with generics and foreign classes? (Java Android)

From Dev

Refactor multiple gsub statements into 1

From Dev

How to navigate through various classes with following-sibling selenium vba

From Dev

How to add logging to classes in a module at a custom level

From Dev

How to enable logging for SQL statements when using JDBC

From Dev

How to stop logging all debug statements in tomcat server

From Dev

How to refactor hundreds of conditions in chain without using if and switch statements for each cases?

From Dev

What is best way to refactor these series of if statements?

From Dev

Refactor function with for loop and multiple if else statements

From Dev

Java logging not logging for all classes?

From Dev

Refactor if statements in PHP or another solution for if statements (not switch case)

From Dev

logging mysql statements executed by jdbc

From Dev

How to refactor long if statment?

From Dev

How to refactor ActiveRecord query?

Related Related

  1. 1

    How to refactor these if else statements to a function

  2. 2

    How to refactor long if/return statements in Ruby?

  3. 3

    How can I refactor a set of ugly if statements?

  4. 4

    How to refactor long if/return statements in Ruby?

  5. 5

    How to refactor consecutive, independent, if statements in this algorithm?

  6. 6

    How to refactor factory classes using lambda expressions?

  7. 7

    How to refactor these classes to interact each other?

  8. 8

    How to refactor try-catch with logging in abstract class?

  9. 9

    How to correctly refactor this big and with very similar code if/else statements?

  10. 10

    How do I refactor out GOTO statements in GWBASIC code?

  11. 11

    Refactor If/Else Statements

  12. 12

    Is there a way to refactor these if statements?

  13. 13

    How do these statements turn on qooxdoo logging?

  14. 14

    How to enable automatic logging of SQL statements with Persistent

  15. 15

    How to refactor those basic classes (PyQt5)

  16. 16

    How to refactor helper methods with generics and foreign classes? (Java Android)

  17. 17

    Refactor multiple gsub statements into 1

  18. 18

    How to navigate through various classes with following-sibling selenium vba

  19. 19

    How to add logging to classes in a module at a custom level

  20. 20

    How to enable logging for SQL statements when using JDBC

  21. 21

    How to stop logging all debug statements in tomcat server

  22. 22

    How to refactor hundreds of conditions in chain without using if and switch statements for each cases?

  23. 23

    What is best way to refactor these series of if statements?

  24. 24

    Refactor function with for loop and multiple if else statements

  25. 25

    Java logging not logging for all classes?

  26. 26

    Refactor if statements in PHP or another solution for if statements (not switch case)

  27. 27

    logging mysql statements executed by jdbc

  28. 28

    How to refactor long if statment?

  29. 29

    How to refactor ActiveRecord query?

HotTag

Archive