Steps to implement logging in core Java
- Eclipse – include libraries ie
logback-classic,core,access,
slf4j-api. - Sample program
package testlogger;
import org.apache.log4j. PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic. LoggerContext;
import ch.qos.logback.classic. PatternLayout;
import ch.qos.logback.classic.spi. LoggingEvent;
import ch.qos.logback.core. FileAppender;
import ch.qos.logback.core.util. StatusPrinter;
public class LoggerMain {
private final Logger logger =
LoggerFactory.getLogger( getClass());
public static void main(String[]
args) {
//
PropertyConfigurator. configure("log4j.properties");
new
ConfigureLogBack();
LoggerMain
lm = new LoggerMain();
lm.putLogger();
}
public void putLogger() {
LoggerContext
lc = (LoggerContext) LoggerFactory.getILoggerFactor y();
StatusPrinter.print(lc);
logger.info("Hello");
}
}
- In the classpath user librabries set the location of logback.xml
<configuration>
<substitutionProperty name="log.dir"
value="D:\\logging\\" />
<substitutionProperty name="log.file"
value="${logback.file.name}" />
<appender name="STDOUT"
class="ch.qos.logback.core. ConsoleAppender">
<layout
class="ch.qos.logback.classic. PatternLayout">
<Pattern>%d{yyyyMMdd-HH:mm:ss. SSSZ} [%thread] %-5level %logger.%method -
%msg%n</Pattern>
</layout>
</appender>
<appender name="STDOUT_inforeach"
class="ch.qos.logback.core. ConsoleAppender">
<layout
class="ch.qos.logback.classic. PatternLayout">
<Pattern>%date{HH:mm:ss.SSS} %level %msg%n</Pattern>
</layout>
</appender>
<appender name="FILE_DATE_ROLL"
class="ch.qos.logback.core. rolling.RollingFileAppender">
<file>${log.dir}/${log.file}. log</file>
<Append>true</Append>
<Encoding>UTF-8</Encoding>
<BufferedIO>false</BufferedIO>
<BufferSize>8192</BufferSize>
<ImmediateFlush>true</ ImmediateFlush>
<rollingPolicy
class="ch.qos.logback.core. rolling. TimeBasedRollingPolicy">
<FileNamePattern>
${log.dir}/bak/${log.file}-%d{ yyyy-MM-dd}.log.zip
</FileNamePattern>
</rollingPolicy>
<layout
class="ch.qos.logback.classic. PatternLayout">
<Pattern>%d{yyyyMMdd-HH:mm:ss. SSSZ} [%thread] %-5level %logger.%method -
%msg%n</Pattern>
</layout>
</appender>
<appender name="FILE_SIZE_ROLL"
class="ch.qos.logback.core. rolling.RollingFileAppender">
<file>${log.dir}/${log.file}. log</file>
<Append>true</Append>
<Encoding>UTF-8</Encoding>
<BufferedIO>false</BufferedIO>
<BufferSize>8192</BufferSize>
<ImmediateFlush>true</ ImmediateFlush>
<rollingPolicy
class="ch.qos.logback.core. rolling. FixedWindowRollingPolicy">
<FileNamePattern>
${log.dir}/bak/${log.file}-%i. log.zip
</FileNamePattern>
<MinIndex>0</MinIndex>
<MaxIndex>9</MaxIndex>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core. rolling. SizeBasedTriggeringPolicy">
<MaxFileSize>50MB</ MaxFileSize>
</triggeringPolicy>
<layout
class="ch.qos.logback.classic. PatternLayout">
<Pattern>%d{yyyyMMdd-HH:mm:ss. SSSZ} [%thread] %-5level %logger.%method -
%msg%n</Pattern>
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE_SIZE_ROLL"
/>
</root>
<logger
name="com.inforeach.util.log. Slf4jLogger">
<level value="DEBUG" />
</logger>
<logger name="org.logicalcobwebs. proxool">
<level value="DEBUG" />
</logger>
<logger name="testlogger.LoggerMain">
<level value="ALL" />
</logger>
</configuration>
- In Eclipse Run Configuration ->Arguments ->VM set
-Dlogback.file.name=Test
5
Run the application it should out put the resut in the out folder mentioned at
the top
Include the below jar files in eclipse project build path
logback-classic-0.9.8.jar
logback-core-0.9.8.jar
slf4j-api-1.4.3.jar
jcl104-over-slf4j-1.4.3.jar
logback-access-0.9.8.jar
logback-core-0.9.8.jar
slf4j-api-1.4.3.jar
jcl104-over-slf4j-1.4.3.jar
logback-access-0.9.8.jar
I became blind after reading the code part.
ReplyDeleteWhy arent you using syntax highlighter?
I agree with Sezin, lousy web page layout.
ReplyDelete