1

I want to use SLF4j in Spring mvc-hibernate project. i have made logback.xml for logging

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <property name="DEV_HOME" value="c:/logs" />

    <appender name="FILE-AUDIT"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${DEV_HOME}/debug.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} - %msg%n
            </Pattern>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- rollover daily -->
            <fileNamePattern>${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log
                        </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>

    </appender>

    <logger name="com.mkyong.web" level="debug"
        additivity="false">
        <appender-ref ref="FILE-AUDIT" />
    </logger>

    <root level="error">
        <appender-ref ref="FILE-AUDIT" />
    </root>

</configuration>

but i do not know to cofigure logback.xml in spring-mvc.

In which folder in project should i keep my logback.xml?

how do i configure logback.xml in DefaultServlet-servlet.xml

Nazaret K.
  • 3,409
  • 6
  • 22
  • 31
Kunal Batra
  • 821
  • 3
  • 14
  • 35

1 Answers1

2

No need to do anything in Spring configuration. Just add the slf4j and logback dependencies (e.g. in pom.xml if you're using maven), keep logback.xml in the classpath (e.g. src/main/resources) and use the SLF4J API in your spring-mvc Controllers.

You can also see more details here:

https://wiki.base22.com/display/btg/How+to+setup+SLF4J+and+LOGBack+in+a+web+app+-+fast

Nazaret K.
  • 3,409
  • 6
  • 22
  • 31
  • thanx for the reply but i am getting error while creating main/resources in src please check the below link http://stackoverflow.com/questions/36907204/how-to-created-src-main-resources-in-eclipse – Kunal Batra Apr 28 '16 at 06:46
  • The structure src/main/resources is for Maven. If you're not using maven you don't need to create this structure. I strongly recommend you use maven. – Nazaret K. Apr 28 '16 at 06:50
  • Create a pom.xml in the root folder of your project and then import the project into Eclipse by doing Import->Existing Maven Projects. – Nazaret K. Apr 28 '16 at 06:53
  • 1
    Since you're not using Maven, you shouldn't be creating the src/main/resources folder. Just put logback.xml in the src folder. – Nazaret K. Apr 28 '16 at 06:59
  • You may want to read this section 2.3.2. Logging Here https://docs.spring.io/spring/docs/5.0.0.RC3/spring-framework-reference/overview.html if you're having any issues with the code not respecting the Slf4j setup. – granadaCoder Feb 15 '19 at 23:00