1

I am trying to use this solution to fetch the process id and print it out in the log

RuntimeMXBean rt = ManagementFactory.getRuntimeMXBean();
String pid = rt.getName();
ThreadContext.put("PID", pid);

Edit : In my logger class I can see we are importing. so I can infer that we are definitely using SL4J facade with most likely log4j2.x as per dependency tree.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

I could see form the dubugger that the code is correctly fetching the process ID, but the log simple isnt showing it and there is no other errors. My log currently looks like this

2023-05-22 10:36:54,487 [] DEBUG beanutils.converters.ArrayConverter  - Setting default value: [Ljava.lang.Short;@7a3a49e5

It is not able to fetch the PID still.This is what I have in my log4j config.

log4j.appender.consoleAppender.layout.ConversionPattern=%d [%X{PID}] %-5p %c{3} %x - %m%n

My dependency tree looks like this. May be Sl4J is interfering?

λ mvn dependency:tree |grep log4
[INFO] +- org.apache.logging.log4j:log4j-api:jar:2.17.2:compile
[INFO] +- org.apache.logging.log4j:log4j-core:jar:2.17.2:compile
[INFO] +- org.apache.logging.log4j:log4j-api:jar:2.17.2:compile
[INFO] +- org.apache.logging.log4j:log4j-core:jar:2.17.2:compile
[INFO] +- org.apache.logging.log4j:log4j-api:jar:2.17.2:compile
[INFO] +- org.apache.logging.log4j:log4j-core:jar:2.17.2:compile
[INFO] |  +- org.apache.logging.log4j:log4j-slf4j-impl:jar:2.17.2:provided
[INFO] |  +- org.apache.logging.log4j:log4j-1.2-api:jar:2.17.2:provided
[INFO] +- org.apache.logging.log4j:log4j-api:jar:2.17.2:compile
[INFO] +- org.apache.logging.log4j:log4j-core:jar:2.17.2:compile

Any ideas as to what i might be doing wrong or what can i do to debug this further? I tried using log4j2.x config. But it completely stopped logging and gave this warning

log4j:WARN Please initialize the log4j system properly.
asb
  • 781
  • 2
  • 5
  • 23
  • Your code uses Log4j 2.x, while your configuration uses the Log4j 1.x format. Are you sure you are not using (the unsupported) Log4j 1.x as logging backend? – Piotr P. Karwasz May 22 '23 at 03:59
  • I tried using "mvn dependency:tree | grep log4j" and i can confirm i am using log4j 2.x. So my problem might be i am using 1.x config as you mentioned. Will try 2.x config. – asb May 22 '23 at 12:14
  • A Log4j 1.x configuration also works with [`ch.qos.reload4j:reload4j`](https://mvnrepository.com/artifact/ch.qos.reload4j/reload4j) (in which case you are not using Log4j 2.x) and [`org.apache.logging.log4j:log4j-1.2-api`](https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-1.2-api) with `log4j1.compatibility = "true"`. – Piotr P. Karwasz May 22 '23 at 13:09
  • I edited the post to update with dependency tree. May be a problem with sl4j? but not sure how to work around it – asb May 22 '23 at 22:30
  • It seems you could trying to convert a string to an hex number with `%X`. Try getting process id this way: `long pid = Long.parseLong(processName.split("@")[0]);` – william xyz May 22 '23 at 23:34
  • I tried something like this -> ThreadContext.put("PID", rt.getName().split("@")[0]); as put expects 2 strings. I checked in the debugger and see that processName.split("@")[0] evaluates to 12276 which is the process id. But still doesnt work. – asb May 23 '23 at 02:28
  • The `log4j-1.2-api` module does **not** issue warnings prefixed by `log4j:WARN`. This message comes from `reload4j` or a shaded `log4j` (version 1.2). Remark that `log4j-1.2-api` and `log4j-slf4j-impl` have a "provided" scope in your project and are not deployed with the app. – Piotr P. Karwasz May 23 '23 at 03:28
  • Sorry, what can i try next? I did try the log4j1.compatibility=true, with 2.x config which didnt work. With the existing 1.x config everything else seems to be in place except it being not able to extract back the PID that i added from code – asb May 23 '23 at 04:25
  • First ensure that the Log4j 2 libraries are in the `runtime` or `compile` Maven scope. You can then run your application with both `-Dlog4j.debug=true` and `-Dlog4j2.debug=true` and add the output to your question. – Piotr P. Karwasz May 24 '23 at 05:57

0 Answers0