Java slf4j Logger getLoggerForCurrentClass()

Here you can find the source of getLoggerForCurrentClass()

Description

Create a slf4j logger based on the caller's class name.

License

Apache License

Declaration

public static Logger getLoggerForCurrentClass() 

Method Source Code

//package com.java2s;
/**/*from   ww w .java  2 s .  c o m*/
 * Copyright (c) 2005-2010 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *
 * $Id$
 */

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main {
    /**
     * Create a slf4j logger based on the caller's class name.
     *
     * This method utilizes the current thread's stack trace to locate the caller
     * class name and create a logger based on it.
     * <p>
     * Usage:
     * <pre>
     *   Logger logger = SpringSideLoggerHelper.getLoggerForCurrentClass();
     *   ...
     * </pre>
     * </p>
     * 
     * @return
     */
    public static Logger getLoggerForCurrentClass() {
        Thread currThread = Thread.currentThread();
        StackTraceElement[] trace = currThread.getStackTrace();
        return LoggerFactory.getLogger(trace[2].getClassName());
    }
}

Related

  1. getLogger()
  2. getLogger(Class clazz)
  3. getLogger(Class clazz)
  4. getLogger(Class clazz)
  5. getLogger(String className)
  6. getLoggerInstance(String callerCLass)
  7. getLoggerLevel(Logger logger)
  8. getLong(Properties config, String key, long defaultValue)
  9. getMessage(String message, Object... args)