log4j multiple appenders

Discussion in 'Java' started by Uli Kunkel, Nov 5, 2009.

  1. Uli Kunkel

    Uli Kunkel Guest

    I'm logging hibernate HQL queries to a log file (file1.log).

    The problem is I have a thread which logs to a different file (file2.log).

    So I want to change appenders at runtime so that 2 different threads
    would simultaniously write hibernate loggs to a different file.

    I'm not sure if this will work because I don't know how log4j getLogger
    is implemented (if it is singleton or not).
    The idea is to call the code below as needed.

    Does anyone have any suggestions about this?

    Thanks in advance.



    -------------------------------------------------------
    public static void setHibernateLogAppender(Logger logger) {
    Logger.getLogger("org.hibernate.SQL").removeAllAppenders();

    while(logger.getAllAppenders().hasMoreElements())
    Logger.getLogger("org.hibernate.SQL").addAppender((Appender)logger.getAllAppenders().nextElement());
    }
    ------------------------------------------------------
    Uli Kunkel, Nov 5, 2009
    #1
    1. Advertising

  2. Uli Kunkel

    Lew Guest

    Uli Kunkel wrote:
    > I'm logging hibernate [sic] HQL queries to a log file (file1.log).
    >
    > The problem is I have a thread which logs to a different file (file2.log).
    >
    > So I want to change appenders at runtime so that 2 different threads
    > would simultaniously write hibernate [sic] loggs to a different file.
    >
    > I'm not sure if this will work because I don't know how log4j getLogger
    > is implemented (if it is singleton or not).
    > The idea is to call the code below as needed.
    >
    > Does anyone have any suggestions about this?


    A few minutes with the log4j documentation shows that its loggers are
    identified by the name given them, usually a class name. You configure
    loggers hierarchically - one configured for your Hibernate client can have its
    own destination regardless of thread by the choice of its name.

    --
    Lew
    Lew, Nov 5, 2009
    #2
    1. Advertising

  3. Uli Kunkel

    Arne Vajhøj Guest

    Uli Kunkel wrote:
    > I'm logging hibernate HQL queries to a log file (file1.log).
    >
    > The problem is I have a thread which logs to a different file (file2.log).
    >
    > So I want to change appenders at runtime so that 2 different threads
    > would simultaniously write hibernate loggs to a different file.
    >
    > I'm not sure if this will work because I don't know how log4j getLogger
    > is implemented (if it is singleton or not).
    > The idea is to call the code below as needed.
    >
    > Does anyone have any suggestions about this?


    > -------------------------------------------------------
    > public static void setHibernateLogAppender(Logger logger) {
    > Logger.getLogger("org.hibernate.SQL").removeAllAppenders();
    >
    > while(logger.getAllAppenders().hasMoreElements())
    > Logger.getLogger("org.hibernate.SQL").addAppender((Appender)logger.getAllAppenders().nextElement());
    >
    > }
    > ------------------------------------------------------


    Your problem is not easily solvable.

    Logger.getLogger("org.hibernate.SQL") will under normal circumstances
    return the same logger for both threads.

    If the two threads are not logging at the same time, then
    you may be able to change config at runtime (I have not checked
    whether your code is good or not).

    But it is not a good solution.

    I think a better solution would be a custom appender
    that are able to log to two files depending on threads
    and then at runtime config that appender to match threads
    with files.

    That will allow concurrent usage.

    Arne
    Arne Vajhøj, Nov 6, 2009
    #3
  4. Uli Kunkel

    Uli Kunkel Guest

    Arne Vajhøj wrote:
    > Uli Kunkel wrote:
    >> I'm logging hibernate HQL queries to a log file (file1.log).
    >>
    >> The problem is I have a thread which logs to a different file
    >> (file2.log).
    >>
    >> So I want to change appenders at runtime so that 2 different threads
    >> would simultaniously write hibernate loggs to a different file.
    >>
    >> I'm not sure if this will work because I don't know how log4j
    >> getLogger is implemented (if it is singleton or not).
    >> The idea is to call the code below as needed.
    >>
    >> Does anyone have any suggestions about this?

    >
    >> -------------------------------------------------------
    >> public static void setHibernateLogAppender(Logger logger) {
    >> Logger.getLogger("org.hibernate.SQL").removeAllAppenders();
    >>
    >> while(logger.getAllAppenders().hasMoreElements())
    >> Logger.getLogger("org.hibernate.SQL").addAppender((Appender)logger.getAllAppenders().nextElement());
    >>
    >> }
    >> ------------------------------------------------------

    >
    > Your problem is not easily solvable.
    >
    > Logger.getLogger("org.hibernate.SQL") will under normal circumstances
    > return the same logger for both threads.
    >
    > If the two threads are not logging at the same time, then
    > you may be able to change config at runtime (I have not checked
    > whether your code is good or not).
    >
    > But it is not a good solution.
    >
    > I think a better solution would be a custom appender
    > that are able to log to two files depending on threads
    > and then at runtime config that appender to match threads
    > with files.
    >
    > That will allow concurrent usage.
    >
    > Arne



    I didn't have time to test my idea yet, but as I see it's not gonna
    work. I'll check if the custom appender is relatively easy to implement..
    Uli Kunkel, Nov 6, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexandra Stehman

    eclipse, junit, log4j, & finding log4j.xml

    Alexandra Stehman, Jan 19, 2004, in forum: Java
    Replies:
    0
    Views:
    5,751
    Alexandra Stehman
    Jan 19, 2004
  2. Alex Hunsley
    Replies:
    2
    Views:
    12,909
    =?ISO-8859-15?Q?Fr=E9d=E9ric_G=E9din?=
    May 28, 2004
  3. Albretch

    timing log4j appenders

    Albretch, Jul 11, 2004, in forum: Java
    Replies:
    0
    Views:
    352
    Albretch
    Jul 11, 2004
  4. Replies:
    3
    Views:
    6,396
    Oliver Wong
    May 25, 2006
  5. jaja
    Replies:
    1
    Views:
    955
Loading...

Share This Page