Some code I'm writing runs in two different environments. In one environment, I'm using log4j. In the other environment, the code calls that environment's logging system. Therefore, I need to subclass log4j's Logger so that I can implement an interface required by the other logging system. I have a solution working with one big drawback. The %F and %L specfied in the Appender's Conversion Pattern always return the same file and line number of my log4j subclass, not the class that originated the logging event.
Is there a better solution to my inheritance dilemma? Or is there some way to pass log4j the file and line number arguments? Thanks! /* The Log interface is specified by the non-log4j environment. The methods have the same name as log4j. */ public class LoggerX extends Logger implements Log { /** * @param arg0 */ public LoggerX( String arg0 ) { super( arg0 ); parent = super.getLogger( arg0 ); } static int logEnvironment = 0; final static int ENVIRONMENT_1 = 0; final static int ENVIRONMENT_2 = 1; // the log4j logger returned by super.getLogger Logger parent = null; public static LoggerX getLoggerForEnv( String arg0 ) { switch ( logEnvironment ) { case ENVIRONMENT_1 : return ( LoggerX ) OtherEnvironment.getLogger( ); case ENVIRONMENT_2 : System.out.println( "creating new ENVIRONMENT_2 logger" ); return new LoggerX( arg0 ); default : System.out.println( "************ UNKNOWN LOGGER TYPE: " + logEnvironment + " *************" ); } return null; } /* this call produces the same file and line number regardless of the caller. I know why, but I don't have a solution. */ public boolean info(String arg0) { parent.info( arg0); } public static void setEnvironmentTo1() { logEnvironment = ENVIRONMENT_1; } public static void setEnvironmentTo2() { logEnvironment = ENVIRONMENT_2; } }