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/