1

Is my code is correct? Where I have to specify the logback.xml file path ?

Here is my code

package com.sample;

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

public class TestMainclass {
    static Logger logger = LoggerFactory.getLogger(TestMainclass.class);
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.out.println("before");
        logger.debug("hello");
        logger.info("hello");
        System.out.println("after");
        }
}

logback.xml

<property name="DEV_HOME" value="d:/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.abc.log" level="debug"
    additivity="false">
    <appender-ref ref="FILE-AUDIT" />
</logger>

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

Here is the project structure

enter image description here

Output in console:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
Aradhya
  • 182
  • 1
  • 6
  • 17
  • possible duplicate of [SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder"](http://stackoverflow.com/questions/7421612/slf4j-failed-to-load-class-org-slf4j-impl-staticloggerbinder) – Arpit Aggarwal Jul 15 '15 at 12:03
  • are you sure you have the log4j in the classpath and not only in the lib directory? – Ueli Hofstetter Jul 15 '15 at 12:04
  • I can see that you have slf4j-log4j12 in lib folder, but you are configuring logback.xml. That's not coherent : slf4j is just the front-end, and you can have different backends among which logback **or** log4j. But you need one (and only one) backend ! – Serge Ballesta Jul 15 '15 at 14:32

3 Answers3

2

Use this configuration and put into classpath with name of log4j.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"><log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="SYSLOG" class="org.apache.log4j.net.SyslogAppender">
    <param name="Facility" value="LOCAL0"/>
    <param name="FacilityPrinting" value="false"/>
    <param name="SyslogHost" value="localhost"/>
    <param name="Threshold" value="INFO"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{ISO8601} %-5p [WhereRU][%t|%c{1}] %L %M %m\n"/>
    </layout>
</appender>
<appender name="SERVER_DEBUG" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="DatePattern" value="'-'yyyy-MM-dd'.log'"/>
    <param name="File" value="/var/log/testing/testing-debug"/>
    <param name="Threshold" value="DEBUG"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{ISO8601} %-5p [WhereRU][%t|%c{1}] %L %M %m\n"/>
    </layout>
</appender>
<appender name="SERVER_ERROR" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="DatePattern" value="'-'yyyy-MM-dd'.log'"/>
    <param name="File" value="/var/log/testing/testing-error"/>
    <param name="Threshold" value="ERROR"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{ISO8601} %-5p [WhereRU][%t|%c{1}] %L %M %m\n"/>
    </layout>
</appender>
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{ISO8601} %-5p [WhereRU][%t|%c{1}] %L %M %m\n" />
    </layout>
</appender>

<logger name="com.testing" additivity="false">
    <level value="DEBUG"/>
     <appender-ref ref="SYSLOG"/>
     <appender-ref ref="SERVER_DEBUG"/>
     <appender-ref ref="SERVER_ERROR"/>
     <appender-ref ref="STDOUT" />
</logger>

<logger name="org.springframework" additivity="false">
    <level value="ERROR"/>
     <appender-ref ref="SERVER_ERROR"/>
</logger>

<logger name="org.apache" additivity="false">
    <level value="ERROR"/>
     <appender-ref ref="SERVER_ERROR"/>
</logger>

<logger name="org.springframework.integration">
    <level value="info" />
</logger>

<logger name="org.springframework.integration.samples">
    <level value="info" />
</logger>

<!-- Everything else to catalina.out -->
<root>
    <level value="ERROR"/>
    <appender-ref ref="STDOUT" />
    <appender-ref ref="SERVER_DEBUG" />
    <appender-ref ref="SERVER_ERROR" />
</root></log4j:configuration>

Hope this work.

Darshan
  • 2,094
  • 1
  • 15
  • 15
  • Thanks but I don't want log4j. I need only SLF4J due to some constrain the existing system. Please share the xml for SLF4J if you hav an sample. – Aradhya Jul 15 '15 at 12:32
0

slf4j-log4j12-1.7.12.jar provides Binding for log4j version 1.2, add the log4j-1.2.16.jar to your project.

found at SLF4J manual.

Srinu Chinka
  • 1,471
  • 13
  • 19
0

You are using logback logging modern logging framework which is successor of log4j. So you need to add following jar files into classpath

  1. logback-classic-1.1.3.jar
  2. logback-core-1.1.3.jar

    and make some correction into logback.xml file which is following.

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
    <property name="DEV_HOME" value="d:/logs" />
     .....
     .....
     .....
    </configuration>
    

and you can put logback.xml file into `src' folder of project.

Sunil
  • 437
  • 3
  • 11