Is it possible, to configure Logging so that log4j messages from other modules, (like Hibernate) are shown directly in the Designer Runtime Log?

asked 02.10.2018 at 04:21

peters's gravatar image

peters
(suspended)
accept rate: 0%


Hi Peter

You could use the following Utility class to forward log message from one Logger to another one. E.e. from the Hibernate Logger to the Runtime Log (ivy.log)

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Priority;
import org.apache.log4j.spi.LoggingEvent;

import ch.ivyteam.ivy.environment.Ivy;
import ch.ivyteam.log.Logger;

public class LogUtil
{
    private static class ForwardAppender extends AppenderSkeleton 
    {
        public static final String NAME = ForwardAppender.class.getSimpleName();
        private Logger to;

        @SuppressWarnings("deprecation")
        public ForwardAppender(Logger to) {
            this.to = to;
            setName(NAME);
        }

        @Override
        public void close() {
        }

        @Override
        public boolean requiresLayout() {
            return false;
        }

        @Override
        protected void append(LoggingEvent event) {
            if (event.getThrowableInformation() != null)
            {
                to.log(event.getLevel(), event.getMessage(), event.getThrowableInformation().getThrowable());
            }
            else
            {
                to.log(event.getLevel(), event.getMessage());
            }
        }
    }

    public static void forwardLoggerToLogger(Logger from, Logger to)
    {
        if (from.getAppender(ForwardAppender.NAME)==null)
        {
            from.addAppender(new ForwardAppender(to));
        }
    }

    public static void main(String[] args) 
    {
        // Example 
        Logger hibernate = Logger.getLogger("org.hibernate");
        Logger runtimeLog = Ivy.log();
        forwardLoggerToLogger(hibernate, runtimeLog);
    }
}
link

answered 02.10.2018 at 09:38

Reto%20Weiss's gravatar image

Reto Weiss ♦♦
4.9k202857
accept rate: 74%

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×78
×28
×9

Asked: 02.10.2018 at 04:21

Seen: 1,889 times

Last updated: 02.10.2018 at 09:38