Thanks. We need to add that example to a wiki page or add it to some examples on the web site.
Ralph On May 28, 2013, at 8:08 AM, Lucie Boubée wrote: > Thanks, I implements MessageFactory and it's works > > > 2013/5/27 Ralph Goers <ralph.go...@dslextreme.com> > >> You could use a custom MessageFactory. >> >> public class MyMessageFactory extends ParameterizedMessageFactory { >> >> private final String objId; >> >> public MyMessageFactory(Object obj) { >> this.objId = obj.toString(); >> } >> >> @Override >> public Message newMessage(final String message, final Object... >> params) { >> return new ParameterizedMessage("{}: " + message, objId, >> params); >> } >> >> } >> >> >> >> >> then just do >> >> public class MyClass { >> >> private String id = "512"; >> >> private Logger logger = >> LogManager.getLogger("com.mycorp.mycomponent.MyClass", new >> MyMessageFactory(this)); >> >> public doWork() { >> logger.error("this is an error"); >> } >> >> public String toString() { >> return this.id; >> } >> } >> >> When the error is logged you should see "512: this is an error" as the >> message. >> >> >> Ralph >> >> On May 27, 2013, at 8:15 AM, Lucie Boubée wrote: >> >>> 2013/5/27 Remko Popma <rem...@yahoo.com> >>> >>>> >>>> On 2013/05/27, at 23:50, Lucie Boubée <lbou...@gmail.com> wrote: >>>> >>>>> Hi, >>>>> >>>>> I use log4j2 beta 6 and I would like to add a prefix to all my >> messages. >>>>> This prefix is passed to the constructor parameter and it depends on >> the >>>>> instance of the class. So we're at the object level (not class or >>>> thread). >>>>> >>>>> For example, I have an A class instantiated like new A(152), so when I >>>> use >>>>> log.error("message") on this class, 152: is written just before the >>>>> message. For new A(155),155: will be displayed instead. >>>> >>>> Do you mean that the call to log.error("message") is inside class A? >>>> In that case, how about simply using >>>> log.error("{} message", myPrefix); >>>> Would that work? >>>> >>> >>> Yes, log.error("message") is calling inside class A. >>> log.error('{} message", myPrefix) work, but I would like to avoid to add >>> the prefix every time I read a log error, like with ThreadContext. >>> >>>> >>>>> >>>>> I try to use ThreadContext.put on the constructor but, I don't know >> when >>>>> logger wiil be call consequently the prefix is set to the last >>>>> ThreadContext called but that does not necessarily correspond to the >>>>> correct class instance >>>>> I ask to stackoverflow ( >>>>> >>>> >> http://stackoverflow.com/questions/16723016/how-do-i-add-a-prefix-to-log4j-messages-at-the-object-level >>>> ) >>>>> but >>>>> it has not solved my problem. >>>>> >>>>> -- >>>>> Lucie >>>> >>>> --------------------------------------------------------------------- >>>> To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org >>>> For additional commands, e-mail: log4j-user-h...@logging.apache.org >>>> >>>> >>> >>> >>> -- >>> Lucie BOUBÉE >> >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org >> For additional commands, e-mail: log4j-user-h...@logging.apache.org >> >> > > > -- > Lucie BOUBÉE --------------------------------------------------------------------- To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org For additional commands, e-mail: log4j-user-h...@logging.apache.org