[logback-user] Object domain from layouts

gaston sponer gaston.sponer at hotmail.com
Thu Jul 25 22:02:48 CEST 2013


 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,


 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/logback-user/attachments/20130725/0c7f5da1/attachment.html>


More information about the Logback-user mailing list