0

I have just started using jetty-maven-plugin.

I found some strange behavior regarding logs and system output.

When I start jetty with maven

mvn -Djetty.port=8093 -Dlog4j.configuration=file:src/log4j.properties jetty:run

I see bean constructor sysout message.

After, when I request the url http://my.local.host:8093/api/rest/admin/houses, browser shows me a correct page, but nothing similar to house! in console or log file.

Hovewer, some other INFO messages from Jetty, Spring etc. get printed there.

@Controller
public class AppAdminClientJsonController {

    private final static Logger logger = 
         LoggerFactory.getLogger(AppAdminClientJsonController.class);

    public AppAdminClientJsonController() {
         System.out.println("----------AppAdminClientJsonController----------");
    }

    @RequestMapping(value = "/admin/houses", method = RequestMethod.GET)
    public @ResponseBody
    List<House> getHouses() {

        logger.warn("house!");
        System.out.println("house!!");

        return Arrays.asList(new House("somesite", "http://somesite.com"));

    }
}

So generally Jetty does not respect my logger, and overrides system out stream in some funky manner so nothing gets printed after server is up and running.

What can this be?

Plugin's part in pom.xml

<plugin>
    <!-- http://wiki.eclipse.org/Jetty/Feature/Jetty_Maven_Plugin#Quick_Start:_Get_Up_and_Running -->
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>8.1.8.v20121106</version>
    <configuration>
        <webAppSourceDirectory>${basedir}/${webappRootPath}</webAppSourceDirectory>
        <scanIntervalSeconds>10</scanIntervalSeconds>
        <webAppConfig>
            <contextPath>/</contextPath>
            <descriptor>${basedir}/${webappRootPath}/WEB-INF/web.xml</descriptor>
        </webAppConfig>
    </configuration>
</plugin>

UPDATE

I have just tried printing the System.out object itself. I was right, Jetty overrode it:

At spring init phase it was: java.io.PrintStream@175390b7

After server startup it became: java.io.PrintStream@9c783fc

snowindy
  • 3,117
  • 9
  • 40
  • 54

1 Answers1

1

Jetty does not log to log4j.

It has its own logger framework (which incidentally predates log4j).

By default, it will use its own logger framework to log out to System.err. Also, default startup configurations for jetty includes the etc/jetty-logging.xml which routes all System.out and System.err to a rolling log file.

The jetty logging framework, however, will use slf4j if present on the classpath.

If you want to log to log4j, you'll need to do it via slf4j. To enable this you need the following dependency to be present in your jetty-maven-plugin configured dependencies ...

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>***your preferred version here***</version>
</dependency>

Versions of slf4j-log4j12 available: http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.slf4j%22%20AND%20a%3A%22slf4j-log4j12%22

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • I have tried this. I was able to supply Jetty with log4j, and Jetty logs get printed there, but not the app logs. Jetty did actually read my log4j.properties, and did write some logs there, but I was unable see there my System.out messages and app log messages. I use slf4j for logging. I can host my project somewhere so you can check it out and see, should I? – snowindy Feb 10 '13 at 09:47
  • 1
    Several assumptions are wrong here. System.out and System.err are captured by something that is not a logging framework. aka `etc/jetty-logging.xml`. So quit relying on System.out and System.err. Second, there is a classloader isolation between the server and the webapp. If you want the webapp to have log4j, include the log4j library in it, plus your log4j configuration. If you want the webapp to use the server log4j and configuration, you have a completely different question, that has been answered in stackoverflow before. – Joakim Erdfelt Feb 10 '13 at 15:50
  • Wow thank you for this hint on classloaders! I will dig this way. – snowindy Feb 11 '13 at 06:06