8

I am trying to use log4j for the first time and I have configured it correctly, I just wanted to know how can I print the details of the argument passed as second parameter:

LogManager.getLogger(SomeName.class.getName()).info(message, detail);

and my configuration appender is:

<File name="file1" fileName="output.log">
    <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>

I think there should be some switch within the pattern after %msg if I want to log the detail there. But I am not sure what or how.

Ad Infinitum
  • 3,560
  • 27
  • 33
Hassan
  • 930
  • 3
  • 16
  • 35
  • 1
    What is `detail` in this example? Is it just a Java object that you want to log information about? In log4j, the character sequence `{}` in the message is replaced by the arguments, in order, so you might do `logger.info("{} is {} elements long", list, list.size())` – Andrew Rueckert Jun 10 '16 at 18:36
  • I am agree with @AndrewRueckert. You can print any number of data you want in log.info() or in log.debug(). You just need to add {} for each variable and you need to enter value in comma sepated value. e.g. log.info("Today is : {} day of {} month {} year", day, month, year); – sauumum Jun 10 '16 at 18:49
  • Thanks @AndrewRueckert and sauumum it worked :) – Hassan Jun 10 '16 at 19:15

2 Answers2

17

Though I was expecting some better solution, but this seems to be the only one available.

LogManager.getLogger(SomeName.class.getName()).info("Message: {}, Detail: {}", message, detail);
Hassan
  • 930
  • 3
  • 16
  • 35
5
LogManager.getLogger().info(
    "The {} is that {}, there are more {},"
 + " not necessarily in a message + details {}.",
    "reason", "often", "parameters", "relationship");
Remko Popma
  • 35,130
  • 11
  • 92
  • 114