views:

379

answers:

2

I have just started exploring SpringMVC and logback.

This is my controller, (the only one i have so far)

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class IndexController {

    protected final static Logger logger = LoggerFactory.getLogger(IndexController.class);

    @RequestMapping("/index")
    public ModelAndView index() {
        logger.info("Returning index view");
        return new ModelAndView("index");
    }
}

and this is the test code for the above.

import org.junit.Test;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertEquals;

import org.springframework.web.servlet.ModelAndView;

public class IndexControllerTest {

    @Test
    public void index() throws Exception {
        IndexController iController = new IndexController();
        ModelAndView modelAndView = iController.index();

        assertNotNull(modelAndView.getModel());
        assertEquals("index", modelAndView.getViewName());
    }
}

I have logback setup to log into a file with FixedWindowRollingPolicy, the configuration is,

<configuration>
    <appender name="FILE"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>logFile.log</File>
        <RollingPolicy
            class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <FileNamePattern>logFile.%i.log</FileNamePattern>
            <MinIndex>1</MinIndex>
            <MaxIndex>3</MaxIndex>
        </RollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>5MB</MaxFileSize>
        </triggeringPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %-26(%d{HH:mm:ss,SSS} [%thread]) %-5level %logger{32} - %msg%n
            </Pattern>
        </layout>
    </appender>
    <root level="info">
        <appender-ref ref="FILE" />
    </root>
</configuration>

The problem, I have now, is that no entry is created in the logfile while accessing the site from the browser. I would suppose that the controller is called that subsequently returns the view that is presented in the browser, and hence the log method should be called before the view is displayed. But nothing happens.

However, when running the Test, the logging works as expected and i have the specific entry in the logFile for "returning index view".

Any help or guidance regarding the above situation is highly appreciated.

EDIT: Using tomcat6 with apache at the moment.

The logback configuration file, logback.xml is placed directly under src (default package). As I have checked after deploying, it gets copied to WEB-INF/classes/

A: 

Strange as it may sound, it seems that I get the logs just fine, when I use absolute path for the appender.File property!

<File>/home/sujoy/logFile.log</File>

with the above for instance, I get my logs just I would expect.

I have no idea why this happens though. Specifically since while testing the log works fine.

Sujoy
A: 

Does the user running Tomcat instance have permission to write on wherever the log file is specified?

Mamad Purbo
yes permission is not the problem, it seems that on running test netbeans creates the logFile.log directly under the project directory, but tomcat fails to log there and comes up with permission issues, however, if I provide the absolute path to the project directory then both tomcat and the test logs to the same logFile with no problems whatsoever.
Sujoy