在使用logback 的时候 使用的实例
java代码:
package com.eastcom.my.study.first.logback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LogbackDemo {
private static Logger logger = LoggerFactory.getLogger(com.eastcom.my.study.first.logback.LogbackDemo.class);
public static void main(String[] args) {
logger.trace("======trace");
logger.debug("======debug");
logger.info("======info");
logger.warn("======warn");
logger.error("======error");
String name = "Aub";
String message = "3Q";
String[] fruits = { "apple", "banana" };
// logback提供的可以使用变量的打印方式,结果为"Hello,Aub!"
logger.info("Hello,{}!", name);
// 可以有多个参数,结果为“Hello,Aub! 3Q!”
logger.info("Hello,{}! {}!", name, message);
// 可以传入一个数组,结果为"Fruit: apple,banana"
logger.info("Fruit: {},{}", fruits);
System.out.println("hello world ");
}
}
引入的jar包
ch.qos.logback
logback-classic
1.2.3
ch.qos.logback
logback-core
1.2.3
org.slf4j
slf4j-api
1.7.25
总是不能正常输出日志:
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.
hello world
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
或者在网上找到了答案:链接 https://stackoverflow.com/questions/7421612/slf4j-failed-to-load-class-org-slf4j-impl-staticloggerbinder
添加了依赖包
org.slf4j
slf4j-simple
1.7.25
需要去掉
正常情况下输出:
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
over
如果加入logback.xml 文件
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
在代码中需要从新加载配置文件
java代码
package com.eastcom.my.study.first.logback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.StatusPrinter;
public class LogbackDemo {
private static Logger logger = LoggerFactory.getLogger(com.eastcom.my.study.first.logback.LogbackDemo.class);
public static void main(String[] args) {
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(lc);
lc.reset();
try {
configurator.doConfigure("D:/newworkspace/my-study-first/src/logback.xml");
} catch (JoranException e) {
e.printStackTrace();
}
StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
System.out.println("===================");
logger.debug("Hello {}", "debug message");
logger.trace("======trace");
logger.debug("======debug");
logger.info("======info");
logger.warn("======warn");
logger.error("======error");
String name = "Aub";
String message = "3Q";
String[] fruits = { "apple", "banana" };
// logback提供的可以使用变量的打印方式,结果为"Hello,Aub!"
logger.info("Hello,{}!", name);
// 可以有多个参数,结果为“Hello,Aub! 3Q!”
logger.info("Hello,{}! {}!", name, message);
// 可以传入一个数组,结果为"Fruit: apple,banana"
logger.info("Fruit: {},{}", fruits);
System.out.println("hello world ");
}
}
可以看到只获得warn的日志输出
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/softinstall/java/apache-maven-3/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/softinstall/java/apache-maven-3/repository/org/slf4j/slf4j-simple/1.7.25/slf4j-simple-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
===================
13:34:46.762 [main] WARN c.e.m.s.first.logback.LogbackDemo - ======warn
13:34:46.773 [main] ERROR c.e.m.s.first.logback.LogbackDemo - ======error
hello world
over
在Linux下打包运行 指定配置文件的位置,可也使用 -Dlogback.configurationFile=/data/myzjodc-test/conf/logback/logback.xml
java -Dlogback.configurationFile=/data/myzjodc-test/conf/logback/logback.xml -cp .:./my-study-first-0.0.1-SNAPSHOT-jar-with-dependencies.jar com.eastcom.my.study.first.logback.LogbackDemo2