You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by "Hüseyin Kartal (JIRA)" <ji...@apache.org> on 2015/09/18 13:56:04 UTC

[jira] [Updated] (LOG4J2-1124) LogManager.getLogger()

     [ https://issues.apache.org/jira/browse/LOG4J2-1124?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Hüseyin Kartal updated LOG4J2-1124:
-----------------------------------
    Description: 
As in log4j2 api the LogManager.getLogger() call should return a logger for the calling class.

{code}
    /**
     * Returns a Logger with the name of the calling class.
     * @return The Logger for the calling class.
     * @throws UnsupportedOperationException if the calling class cannot be determined.
     */
    public static Logger getLogger() {
        return getLogger(ReflectionUtil.getCallerClass(2));
    }
{code}


But in the following example the returned logger is get for the declaring AbstractLogger class and not as assumed for the calling Classes InstanceLogger and InstanceLogger2.


{code}
public class LogTest {
    protected class AbstractLogger {
        Logger logger = LogManager.getLogger();
        public AbstractLogger() {
            super();
            logger.info("init");
        }

        void log(String s) {
            logger.info(s);
        }
    }

    protected class InstanceLogger extends AbstractLogger {
        @Override
        void log(String s) {
            super.log(s);
            logger.info(s);
        }
    }

    protected class InstanceLogger2 extends AbstractLogger {
        @Override
        void log(String s) {
            super.log(s);
            logger.info(s);
        }
    }

    @Test
    public final void testLoggerInstance() {
        new InstanceLogger().log("logging");
        new InstanceLogger2().log("logging");
    }
}
{code}


The Output is
{code}
2015-09-18 13:51:21,830  INFO - main                 - d.z.w.l.LogUtilsTest$AbstractLogger      -  [] * init -                                    
2015-09-18 13:51:21,831  INFO - main                 - d.z.w.l.LogUtilsTest$AbstractLogger      -  [] * logging -                                    
2015-09-18 13:51:21,831  INFO - main                 - d.z.w.l.LogUtilsTest$AbstractLogger      -  [] * logging -                                    
2015-09-18 13:51:21,831  INFO - main                 - d.z.w.l.LogUtilsTest$AbstractLogger      -  [] * init -                                    
2015-09-18 13:51:21,831  INFO - main                 - d.z.w.l.LogUtilsTest$AbstractLogger      -  [] * logging -                                    
2015-09-18 13:51:21,831  INFO - main                 - d.z.w.l.LogUtilsTest$AbstractLogger      -  [] * logging -                                    
{code}


A workaround is to call *LogManager.getLogger(getClass())* but i assume that in most cases the logger for the calling class is needed so it would be nice to change the behaviour of LogManager.getLogger().


The output of the workaround is
{code}
2015-09-18 13:53:50,538  INFO - main                 - d.z.w.l.LogUtilsTest$InstanceLogger      -  [] * init -                                    
2015-09-18 13:53:50,538  INFO - main                 - d.z.w.l.LogUtilsTest$InstanceLogger      -  [] * logging -                                    
2015-09-18 13:53:50,538  INFO - main                 - d.z.w.l.LogUtilsTest$InstanceLogger      -  [] * logging -                                    
2015-09-18 13:53:50,543  INFO - main                 - d.z.w.l.LogUtilsTest$InstanceLogger2     -  [] * init -                                    
2015-09-18 13:53:50,543  INFO - main                 - d.z.w.l.LogUtilsTest$InstanceLogger2     -  [] * logging -                                    
2015-09-18 13:53:50,543  INFO - main                 - d.z.w.l.LogUtilsTest$InstanceLogger2     -  [] * logging -                                    
{code}


  was:
As in log4j2 api the LogManager.getLogger() call should return a logger for the calling class.

    /**
     * Returns a Logger with the name of the calling class.
     * @return The Logger for the calling class.
     * @throws UnsupportedOperationException if the calling class cannot be determined.
     */
    public static Logger getLogger() {
        return getLogger(ReflectionUtil.getCallerClass(2));
    }


But in the following example the returned logger is get for the declaring AbstractLogger class and not as assumed for the calling Classes InstanceLogger and InstanceLogger2.


public class LogTest {
    protected class AbstractLogger {
        Logger logger = LogManager.getLogger();
        public AbstractLogger() {
            super();
            logger.info("init");
        }

        void log(String s) {
            logger.info(s);
        }
    }

    protected class InstanceLogger extends AbstractLogger {
        @Override
        void log(String s) {
            super.log(s);
            logger.info(s);
        }
    }

    protected class InstanceLogger2 extends AbstractLogger {
        @Override
        void log(String s) {
            super.log(s);
            logger.info(s);
        }
    }

    @Test
    public final void testLoggerInstance() {
        new InstanceLogger().log("logging");
        new InstanceLogger2().log("logging");
    }
}

A workaround is to call LogManager.getLogger(getClass()) but i assume that in most cases the logger for the calling class is needed so it would be nice to change the behaviour of LogManager.getLogger().


> LogManager.getLogger()
> ----------------------
>
>                 Key: LOG4J2-1124
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-1124
>             Project: Log4j 2
>          Issue Type: Bug
>          Components: API
>    Affects Versions: 2.3
>         Environment: win64 java8 osgi
>            Reporter: Hüseyin Kartal
>             Fix For: 2.4
>
>
> As in log4j2 api the LogManager.getLogger() call should return a logger for the calling class.
> {code}
>     /**
>      * Returns a Logger with the name of the calling class.
>      * @return The Logger for the calling class.
>      * @throws UnsupportedOperationException if the calling class cannot be determined.
>      */
>     public static Logger getLogger() {
>         return getLogger(ReflectionUtil.getCallerClass(2));
>     }
> {code}
> But in the following example the returned logger is get for the declaring AbstractLogger class and not as assumed for the calling Classes InstanceLogger and InstanceLogger2.
> {code}
> public class LogTest {
>     protected class AbstractLogger {
>         Logger logger = LogManager.getLogger();
>         public AbstractLogger() {
>             super();
>             logger.info("init");
>         }
>         void log(String s) {
>             logger.info(s);
>         }
>     }
>     protected class InstanceLogger extends AbstractLogger {
>         @Override
>         void log(String s) {
>             super.log(s);
>             logger.info(s);
>         }
>     }
>     protected class InstanceLogger2 extends AbstractLogger {
>         @Override
>         void log(String s) {
>             super.log(s);
>             logger.info(s);
>         }
>     }
>     @Test
>     public final void testLoggerInstance() {
>         new InstanceLogger().log("logging");
>         new InstanceLogger2().log("logging");
>     }
> }
> {code}
> The Output is
> {code}
> 2015-09-18 13:51:21,830  INFO - main                 - d.z.w.l.LogUtilsTest$AbstractLogger      -  [] * init -                                    
> 2015-09-18 13:51:21,831  INFO - main                 - d.z.w.l.LogUtilsTest$AbstractLogger      -  [] * logging -                                    
> 2015-09-18 13:51:21,831  INFO - main                 - d.z.w.l.LogUtilsTest$AbstractLogger      -  [] * logging -                                    
> 2015-09-18 13:51:21,831  INFO - main                 - d.z.w.l.LogUtilsTest$AbstractLogger      -  [] * init -                                    
> 2015-09-18 13:51:21,831  INFO - main                 - d.z.w.l.LogUtilsTest$AbstractLogger      -  [] * logging -                                    
> 2015-09-18 13:51:21,831  INFO - main                 - d.z.w.l.LogUtilsTest$AbstractLogger      -  [] * logging -                                    
> {code}
> A workaround is to call *LogManager.getLogger(getClass())* but i assume that in most cases the logger for the calling class is needed so it would be nice to change the behaviour of LogManager.getLogger().
> The output of the workaround is
> {code}
> 2015-09-18 13:53:50,538  INFO - main                 - d.z.w.l.LogUtilsTest$InstanceLogger      -  [] * init -                                    
> 2015-09-18 13:53:50,538  INFO - main                 - d.z.w.l.LogUtilsTest$InstanceLogger      -  [] * logging -                                    
> 2015-09-18 13:53:50,538  INFO - main                 - d.z.w.l.LogUtilsTest$InstanceLogger      -  [] * logging -                                    
> 2015-09-18 13:53:50,543  INFO - main                 - d.z.w.l.LogUtilsTest$InstanceLogger2     -  [] * init -                                    
> 2015-09-18 13:53:50,543  INFO - main                 - d.z.w.l.LogUtilsTest$InstanceLogger2     -  [] * logging -                                    
> 2015-09-18 13:53:50,543  INFO - main                 - d.z.w.l.LogUtilsTest$InstanceLogger2     -  [] * logging -                                    
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org