Setup slf4j to maven project
The Simple Logging Facade for Java (SLF4J) serves as a simple facade or abstraction for various logging frameworks
Add dependency to your Maven POM
you need “slf4j-api.jar” and your preferred binding, like log4j “slf4j-log4j12.jar“. Just declares the dependency in your pom.xml.<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4jVersion}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> <scope>runtime</scope> </dependency>
log4.properties
the properties code put in java/resources# Direct log messages to a log file log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=C:\\myapp.log log4j.appender.file.MaxFileSize=1MB log4j.appender.file.MaxBackupIndex=1 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n # Direct log messages to stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n # Root logger option log4j.rootLogger=INFO, file, stdout # Log everything. Good for troubleshooting log4j.logger.org.hibernate=INFO # Log all JDBC parameters log4j.logger.org.hibernate.type=ALL
Put logging code in your classes
the imports sectionimport org.slf4j.Logger; import org.slf4j.LoggerFactory;Add the following at the top of your class in the global section
static final Logger LOG = LoggerFactory.getLogger(MyClassName.class);The following code how to use SLF4j in an application, as you can see the fatal logging level has not been implemented.
public static void main(String[] args) { Logger logger = LoggerFactory.getLogger(Slf4jHelloWorld.class); logger.trace("Hello World"); logger.debug("Hello World"); logger.info("Hello World"); logger.warn("Hello World"); logger.error("Hello World"); }
thanks
ReplyDelete