Best practice for logging - calling a log method or writing at the beginning of the target method?

When will we register: before the function call (example A) or at the beginning of the target method (example B)?

Note that this question is about the exact placement of calls to the logger function, not the general best logging methods.

Solution A: the log when calling the function :

function someProcess() {
    log.info("Reading data");
    readDataFromIO();
    log.info("Outputing data");
    outputDataToScreen();
}

// ... other module:

function readDataFromIO() {
    ...
}

function outputDataToScreen() {
    ... 
}

Solution B: log at the start of the method

function someProcess() {
    readDataFromIO();
    outputDataToScreen();
}

// ... other module:

function readDataFromIO() {
    log.info("Reading data");
    ...
}

function outputDataToScreen() {
    log.info("Outputing data");
    ... 
}

A , , , . B , 100%, , , , . ?

+7
7

, , - , , .

- , . . , . , , .

MDC/request/

, , . , , .

. - - Action/JSP/Web- .. → → → Util → library.

( someProcess() ). DEBUG . , .. .

- , , . . , .

+5

, , , .:-) .

, , (, -, ), TRACE . - , , , .

, .

  • ( ),
  • ( )
  • .

, , , , . ( , - .) .

+8

:

  • - / Logger ( ), (DEBUG, INFO ..).
  • LogLevel DEBUG
  • LogLevel INFO (, , )
  • LogLevel WARN
  • LogLevel ERROR

/ . .

: Java log4j, . logLevel, , . , (WARN), , DEBUG , .

, (), , "log4j" .

+1

, , ( , ):

  • ( ) . , . ( ) , . , , Windows. , ..
  • someProcess() try-catch,
  • , -, - , , . , B.
  • , / : , # public static void Log(this Exception ex) {//... logging code... } , catch, : try {... } catch (Exception ex) { ex.Log(); } try {... } catch (Exception ex) { ex.Log(); }
    , public static void Log(this Exception ex, string message="") {... logging code... }, , ex.Log("readDataFromIO() - read error occurred"); ,

, ( , ).

, , ( ), , (.. ), .

+1

, , . , , .

LogEvent ( Java, ):

public class LogEvent {
    private int count;
    private String name;

    public LogEvent(String name) {
        this.name = name;
        count = 1;
    }

    public int count() {
        return this.count;
    }

    public void inc() {
        count++;
    }

    public String name() {
        return this.name;
    }

    public void report() {
        if (count >= 1) {
            Util.info(name + " (x " + count + ")");
        }
        else {
            Util.info(name);
        }
    }
}

?

LogEvents "" LogEventCollection:

import java.util.Map;
import java.util.TreeMap;

public class LogEventCollection {
    private Map<String, LogEvent> events;

    public EventCollection() {
        events = new TreeMap<String, Event>();
    }

    public void register(String name) {
        LogEvent ev;

        if (events.containsKey(name)) {
            ev = events.get(name);

            ev.inc();
        }
        else {
            ev = new LogEvent(name);

            events.put(name, ev);
        }
    }

    public void report(String title, int minCount) {
        Util.info("");

        if (!title.isEmpty()) {
            Util.info(title);
        }

        for (LogEvent ev : events.values()) {       
            if ((minCount < 0) || (ev.count() >= minCount)) {
                ev.report();
            }
        }
        Util.info("");
    }
}

, LogEventCollection report() LogEvents . .

LogEventCollection.

, (= register() ). .

+1

log() IN . , , "someProcess".

0
source

The simplest solution would be to overload your methods so that you can specify when to "register" and when not.

function someProcess() {
    readDataFromIO(true);  //This will make a "log"
    outputDataToScreen();  //This will make no "log"
}

// ... other module:

function readDataFromIO() {
    readDataFromIO(false); 
}

function readDataFromIO(bool makeLog) {
    if(makeLog)
        log.info("Reading data");
    ...
}

function outputDataToScreen() {
    outputDataToScreen(false);
}

function outputDataToScreen(bool makeLog) {
    if(makeLog)
        log.info("Outputing data");
    ... 
}
-1
source

All Articles