NOAA-OWP / wres

Code and scripts for the Water Resources Evaluation Service
Other
2 stars 1 forks source link

As a programmer, I want the pid in every log line #207

Open epag opened 3 weeks ago

epag commented 3 weeks ago

Author Name: Jesse (Jesse) Original Redmine Issue: 62692, https://vlab.noaa.gov/redmine/issues/62692 Original Date: 2019-04-17


Given a run of WRES When I look at the logs Then I should see the process id successfully printed

Easy enough to use the MDC, except for executors, OK it's easy enough to add the MDC in WRESTask, but CompletableFuture requires more work than that.

epag commented 3 weeks ago

Original Redmine Comment Author Name: Jesse (Jesse) Original Date: 2019-04-17T20:23:58Z


(Implicit here is "I do not want any logback-specific code in the codebase, I want to use slf4j's MDC only")

epag commented 3 weeks ago

Original Redmine Comment Author Name: Hank (Hank) Original Date: 2019-07-31T13:27:11Z


Jesse:

Does this apply to this situation?:

https://stackoverflow.com/questions/36875541/process-id-in-logback-logging-pattern

Hank

epag commented 3 weeks ago

Original Redmine Comment Author Name: Jesse (Jesse) Original Date: 2019-07-31T13:39:55Z


Hank, gist of it is already present. So it works fine for the main thread, but the threads created by executorservices do not inherit the MDC without more work on our part (kind of annoying, but there it is).

public class Main {

    private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);

    private static final Version version = new Version();

    /**
     * Executes and times the requested operation with the given parameters
     * @param args Arguments from the command line of the format {@code action <parameter 1, parameter 2, etc>}"
     */
    public static void main(String[] args) {

        String processName = ManagementFactory.getRuntimeMXBean().getName();
        String processId = Strings.extractWord(processName, "\\d+(?=@)");

        MDC.put("pid", processId);

        if (LOGGER.isInfoEnabled())
        {
            LOGGER.info( Main.getVersionDescription() );
            LOGGER.info( Main.getVerboseRuntimeDescription() );
        }
</code>

(yuck, the formatting/tabs/etc need work in that spot)

epag commented 3 weeks ago

Original Redmine Comment Author Name: Jesse (Jesse) Original Date: 2019-07-31T13:41:04Z


If the cost of having the pid is "I now have compile-time dependency on logback" then I don't think it's worth it. There are other means, such as splitting out the log files themselves on launch, etc.

epag commented 3 weeks ago

Original Redmine Comment Author Name: Hank (Hank) Original Date: 2019-07-31T13:43:02Z


So you already basically implemented what I saw in that stack overflow. Once again, I'm just a distraction. :)

Hank

epag commented 3 weeks ago

Original Redmine Comment Author Name: Jesse (Jesse) Original Date: 2019-07-31T13:45:47Z


No big deal. It doesn't hurt to ask, and of course sometimes we won't have seen those posts...