[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.

public RenderTest class{

static final Logger logger = LoggerFactory.getLogger("Fred");
public static void main(String...args){
static RenderTest getRenderTest(){
   return new RenderTest();

private void logging(){
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;

public class EmployeeLayout extends LayoutBase<ILoggingEvent>{
Context logger = getContext();

  public String doLayout(ILoggingEvent event) {
    StringBuffer sbuf = new StringBuffer(128);
    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(" - ");

    //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:
<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> -->
<root level="off">
<appender-ref ref="CONSOLE" />


I would like to do something like this from layout to decide what objects will show in the log:


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