0

I have a log4j2 logger configuration that basically write all logging of the root logger to a basic logging file. I always use Logger.getRootLogger here.

For some specific events I'd like to log do a different file. How could I configure such a logger in the properties file (eg give it a name that I can then reference from code)?

davioooh
  • 23,742
  • 39
  • 159
  • 250
membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • http://logging.apache.org/log4j/2.x/manual/configuration.html Did you try? You only need to define rot level O_o for each file. – xild Jan 22 '14 at 09:51
  • Down-vote from me - really not clear what you researched before asking this question. It strikes me as being basic functionality that I would imagine the manuals cover in detail? – Duncan Jones Jan 22 '14 at 09:52
  • I don't want to log ALL logs of a specific level into a certain file. Please read carefully. I want to log SOME statements of the same level to a specific file, while others of the SAME level should go to global file using rootlogger. – membersound Jan 22 '14 at 09:52

2 Answers2

2

You can do something like this.

    log4j.appender.transaction=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.transaction.DatePattern='.'yyyy-MM-dd
    log4j.appender.transaction.File=logs/transaction.log
    log4j.appender.transaction.layout=org.apache.log4j.PatternLayout
    log4j.appender.transaction.layout.ConversionPattern=%d{dd MMM yyyy HH\:mm\:ss} %-5p %c{2}\:%L - %m%n
    log4j.appender.transaction.threshold=info

    log4j.logger.transaction=INFO, transaction
peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • And how is this to be used in code? I don't want all `INFO` events to be logged with this logger. Only specific info events. How do I call this logger? – membersound Jan 22 '14 at 09:50
  • 2
    `Logger transactionLog = Logger.getLogger("transaction");` I guess. – Fildor Jan 22 '14 at 09:55
  • 1
    Actually in log4j2 it is `LogManager.getLogger("transaction")`. More details here: http://logging.apache.org/log4j/2.x/manual/migration.html – peter.petrov Jan 22 '14 at 09:59
0

I finally found out that Markers are the way to route messages:

What is markers in Java Logging frameworks and that is a reason to use them?

Community
  • 1
  • 1
membersound
  • 81,582
  • 193
  • 585
  • 1,120