
i'm working with Logback and i'm sending this concern to you because i can't find any solution for my problem. What i need to know if i can manipulate my objects in the layouts. Ex.: -------------------------------------------------------------------------------- public RenderTest class{ static final Logger logger = LoggerFactory.getLogger("Fred"); public static void main(String...args){ getRenderTest().logging(); } static RenderTest getRenderTest(){ return new RenderTest(); } private void logging(){ xmlFileConfiguration(); Employee employee = getSampleEmployee(); logger.debug("Employee {}", new EmployeeRenderer().getLogMessage(employee, logger)); } private Employee getSampleEmployee(){ Person person = new Person("John", 36, 3124362); Address address = new Address("Columbus", 756, "Cordoba", "X5003","Cordoba"); Salary salary = new Salary(18565.54, "ARS"); Employee employee = new Employee("AA", 324214231, salary, person,address); return employee; } } -------------------------------------------------------------------------------- MyLayout: public class EmployeeLayout extends LayoutBase<ILoggingEvent>{ Context logger = getContext(); public String doLayout(ILoggingEvent event) { StringBuffer sbuf = new StringBuffer(128); sbuf.append(event.getLevel()); sbuf.append(" ["); sbuf.append(event.getThreadName());// sbuf.append(employee.getName()); // How can i get employee object from here? sbuf.append("] "); sbuf.append(event.getLoggerName()); sbuf.append(" - "); sbuf.append(event.getFormattedMessage()); //so far so good, but now I need to get my employee object to render some important information from it. // I tried with event.getArgumentArray()[0], but I can't take for instance a employee.getCompany() return sbuf.toString(); } } -------------------------------------------------------------------------------- My config file: <configuration> <logger name="Fred" level="debug"/> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> <layout class="com.render.EmployeeLayout" /> </encoder> --> </appender> <root level="off"> <appender-ref ref="CONSOLE" /> </root> </configuration> -------------------------------------------------------------------------------- I would like to do something like this from layout to decide what objects will show in the log: sbuf.append(employee.getName()); sbuf.append(employee.getCompany()); Is there any way to do that? Do you see any drawbacks to do that? Thanks a lot,

In your app code call the logger like this: log.info("Employee: {}", myEmployee); Then in the formatter, call event.getArgumentArray() - http://logback.qos.ch/xref/ch/qos/logback/classic/spi/LoggingEvent.html#143 Then look at each arg and check if it is an Employee. David

Thanks David, i understand you point but i'm still can't access to any Employee attribute, this is what i see using getArgumentArray(): sbuf.append(event.getArgumentArray()[0]); Console:INFO [main] Fred ............... com.render.domain.Employee@39f58a0e and traying to get some attributes to this object I only can access to getClass(), there is no getId(), getSalary() or getCompany(). event.getArgumentArray()[0].getClass() Any idea? Thanks Best regards. From: nabble@diroussel.xsmail.com Date: Thu, 25 Jul 2013 21:39:56 +0100 To: logback-user@qos.ch Subject: Re: [logback-user] Object domain from layouts In your app code call the logger like this: log.info("Employee: {}", myEmployee); Then in the formatter, call event.getArgumentArray() - http://logback.qos.ch/xref/ch/qos/logback/classic/spi/LoggingEvent.html#143 Then look at each arg and check if it is an Employee. David _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user

You need to cast it from Object to Employee. Try... Employee e = (Employee) event.getArgumentArray()[0] e.getSalery() But really you should do an instanceof check first. If you don't know what that is, look it up. David
participants (2)
-
David Roussel
-
gaston sponer