Skip to content

What is the ideal way to create a singleton wrapper on top of log4j2 #2243

Answered by ppkarwasz
mehulparmariitr asked this question in Q&A
Discussion options

You must be logged in to vote

@mehulparmariitr,

We discourage the use of wrappers since:

  • they (slightly) decrease performance,
  • they require users to acquire expertise that is not necessary. E.g. you need to learn about location detection and reducing garbage collections,
  • they prevent the usage of an increasing amount of rewrite rules (cf. OpenRewrite Logging).

Loggers use multiple methods to retrieve the location of the caller. The problem with you code is that the detected caller of Logger.traceEntry() is always Log4jLogger. To change this you need to use the SPI ExtendedLogger that allows you to specify which classes to ignore, when looking for the caller.

Your class should look like this:

public class Log4JLogger

Replies: 3 comments 21 replies

Comment options

You must be logged in to vote
14 replies
@ppkarwasz
Comment options

@mehulparmariitr
Comment options

@ppkarwasz
Comment options

@mehulparmariitr
Comment options

@ppkarwasz
Comment options

Answer selected by mehulparmariitr
Comment options

You must be logged in to vote
4 replies
@mehulparmariitr
Comment options

@ppkarwasz
Comment options

@mehulparmariitr
Comment options

@ppkarwasz
Comment options

Comment options

You must be logged in to vote
3 replies
@mehulparmariitr
Comment options

@rgoers
Comment options

rgoers Jan 31, 2024
Collaborator

@ppkarwasz
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants