java - Pass Logger as argument to another class -


public class sessionlogger {      private final string sessionid;      public sessionlogger(string sessionid) {         this.sessionid = sessionid;     }      public void info(log log, string message, object... args) {         log.info(formatmessage(message, args));     }      public void error(log log, string message, throwable t, object... args) {         log.error(formatmessage(message, args), t);     }      private string formatmessage(string message, object... args) {         (int = 0; < args.length; i++) {             message = message.replacefirst("\\{\\}", args[i].tostring());         }         return string.format("sessionid [%s]: %s", sessionid, message);     } } 

what want pass, logger instance sessionlogger class , see class name, logger initialized.

public class {   private static final log log = logfactory.getlog(a.class)   public void doit() {      sessionlogger.info(log, "hello world");   } } 

i expect see class in log message instead of sessionlogger:

2013-10-07 00:29:27,328  info [main] (sessionlogger.java:17) - sessionid [123]: hello world 

i've commons-logging.jar , log4j-1.2.16.jar in classpath. logger instance of org.apache.commons.logging.log

update

just released expected behavior, cause logger logs line of code, log method invoked. should done way somehow

i think solution write custom log4j pattern layout presented in article: http://fw-geekycoder.blogspot.com/2010/07/creating-log4j-custom-patternlayout.html

then not need sessionlogger, simplify code significantly.

layout:

public class mypatternlayout extends patternlayout {      @override     protected patternparser createpatternparser(string pattern) {         return new mypatternparser(pattern);     } } 

pattern parser:

public class mypatternparser extends patternparser {      private static final char username_char = 's';      public mypatternparser(string pattern) {         super(pattern);     }      @override     protected void finalizeconverter(char c) {         switch (c) {             case username_char:                 currentliteral.setlength(0);                 addconverter(new mypatternconverter());                 break;             default:                 super.finalizeconverter(c);         }     } } 

pattern converter:

public class mypatternconverter extends patternconverter {     @override     protected string convert(loggingevent event) {         // retrieve sessionid         return "123";     } } 

log4j configuration:

<?xml version="1.0" encoding="utf-8" ?> <!doctype log4j:configuration system "log4j.dtd">  <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">     <appender name="console" class="org.apache.log4j.consoleappender">         <param name="target" value="system.out"/>         <layout class="mypatternlayout">             <param name="conversionpattern" value="%d{hh:mm:ss,sss} %-5p (%f:%l) - session id:%s %m%n"/>         </layout>     </appender>      <root>         <priority value ="debug" />         <appender-ref ref="console" />     </root>  </log4j:configuration> 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -