This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Log4j Wrapper and PatternLayout Implementation

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, As per my requirement, I need to use something like MyAppLogger in all my class to get log4j logger. As suggested in log4j Complete manual, I decided to use Wrapper class to get log4j Logger. Please look at my Wrapper class below.




Now, the next requirement is to prefix "Timestamp, threadId, Class name, Method name" to the actual log message. I configured PatternLayout in log4j.xml config file as mentioned below




This is my logger test class:




The output is below:



The result is expected to show up Class name and method name where logging message is created. But what happens here is, as I use logger wrapper, the class name and method name is always com.logger.test.MyAppLogger.debug/info/warn/error. This is not the behavior which I need. How could I correct ? Should I remove wrapper and extend log4j Logger class? What would be the best approach? Please advise me.
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure why you are using wrapper, but looking at the complete log4j manual that you mention...you are not using a critical part of what is suggested.

The mysterious FCQN variable, declared at the start of MyLogger class, helps log4j to obtain the correct localization information...without it log4j will...think that MyLogger is the caller instead of the class which really invoked a MyLogger instance.



Then the usage of the log method with FQCN is shown. Partial Example :


Edit: typo in the code from complete manual, I could not copy from the pdf.
 
Viji San
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much!! I missed a VERY important point.
 
You are HERE! The other map is obviously wrong. Better confirm with this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!