导入org.slf4j.*包时出现错误。
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Run Code Online (Sandbox Code Playgroud)
你有什么想法它来自哪里?
最近我向github 发布了我的开源java游戏服务器代码,并遇到了"我应该使用哪种日志"的困境.我希望我的库能为最终开发者提供最"易用性".我希望从这个问题中创建一个伪维基,以便面临同一问题的其他人可以有一个参考点.
1)如果您的开源项目是一个简单的Java库,那么可用于记录的选项有哪些.
一个.使用您自己的记录逻辑
b.打印到控制台,错误
c.使用像log4j,slf4j等库这样的库
2)最佳实践是什么?以上哪一项是对开源库的一般偏好?
3)对于一些着名的日志框架,如log4j,slf4j,commons-logging等,在选择它作为日志库时需要考虑的已知问题.
4)我选择的日志框架是否会影响另一个使用我的库?如果我使用特定的库,是否存在不兼容的可能性?
如果你有一些额外的评论,如"仅登录到控制台","不使用日志记录"等,请证明合理.
在org.springframework.util.SystemPropertyUtils.(SystemPropertyUtils.java:49)
依赖是
我有一个基于Swing的应用程序,没有控制台,通过slf4j将所有消息记录到文本文件,并在下面使用logback.
每当记录消息时,我还想将其添加到一个JTextArea供最终用户在屏幕上查看.
为了保持一致性,这些消息的格式应与日志文件的格式相同.
slf4j/logback是否有API允许你为日志消息添加一个监听器,以便我可以将它们复制到JTextArea?
我希望slf4j NOP实现登录到控制台。目前,它只是不记录任何内容,而是有办法让它记录到控制台吗?
我的用例是我有一个库(不是应用程序),它只依赖于slf4j-api. 但是其中的一些类具有用于测试目的的主要方法。当我运行这些主要方法时,我希望所有日志都转到控制台而不必依赖于slf4j-log4j绑定。
谢谢。
我设置了一个基本的 Java 程序,我正在关注本教程并拥有以下确切代码:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HelloWorld {
public static void main(String[] args) {
Logger logger = LoggerFactory.getLogger(HelloWorld.class);
logger.info("Hello World");
}
}
Run Code Online (Sandbox Code Playgroud)
我的构建路径上有 jars slf4j-api-1.7.5.jar 和 slf4j-log4j12-1.7.5 jar。我不明白是什么给出的,getLogger 方法存在于 LoggerFactory 类中,我可以 F3(源代码查找)到它。我对此进行了 Google 搜索,似乎是唯一解决此问题的方法。有任何想法吗?
这是我用于 Eclipse 的 .classpath:
<xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdit..../>
<classpathentry kind="lib" path="/home/Desktop/slf4j-api-1.7.5.jar" sourcepath="/home/Desktop/slf4j-api-1.7.5.jar"/>
<classpathentry kind="lib" path="slf4j-log4j12-1.7.5.jar"/>
<classpathentry kind="lib" path="log4j-1.2.17.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Run Code Online (Sandbox Code Playgroud) 有没有办法以编程方式设置它们?
我想为特定测试类(使用SpringJUnit4ClassRunner和@SpringApplicationConfiguration)设置日志级别,但不是所有测试类都设置日志级别,并且没有针对每个组合的单独属性文件.
我尝试定义一个非懒惰的bean PropertySource来为环境添加一个新的 ; 该方法被调用,但没有效果.
@Bean
@Lazy(false)
public PropertySource testProperties(ConfigurableEnvironment environment) {
PropertySource properties = new MapPropertySource("testProperties", Collections.singletonMap(
"logging.level.org.springframework.security", "DEBUG"
));
environment.getPropertySources().addFirst(properties);
return properties;
}
Run Code Online (Sandbox Code Playgroud) 我在春季靴子默认模式中获取日志。
2017-02-10 15:39:01.111 INFO 24483 --- [ryBean_Worker-1] c.f.dashboard.services.SchedulerService : Hello World!
Run Code Online (Sandbox Code Playgroud)
我想以这种格式获取日志
2017/02/10 11:24:37,771 [INFO] [http-nio-8080-exec-8] myMethod(myClass.java:38) - Hello World!
Run Code Online (Sandbox Code Playgroud)
我尝试使用这种模式
%sn %d{yyyy/MM/dd HH:mm:ss,SSS} %r [%-5p] [%t] %M(%F:%L) - %m%n
Run Code Online (Sandbox Code Playgroud)
但是在日志行中给出了解析错误。
%PARSER_ERROR[sn] 2017/02/10 09:41:25 12018 [INFO ] [schedulerFactoryBean_Worker-1] %PARSER_ERROR[M] - Hello World!
Run Code Online (Sandbox Code Playgroud) 尝试进行MAVEN INSTALL时出现此错误。我尝试排除,但不确定pom文件中的位置。让我在pom文件中应如何以及应使用哪些排除标签。我还将我的pom文件片段附加在其中,以包括排除项SLF4J:类路径包含多个SLF4J绑定。
SLF4J:在[jar:file:/ C:/Users/147188/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/ org / slf4j / impl / StaticLoggerBinder.class]
SLF4J:在[jar:file:/ C:/Users/147188/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.10.0/log4j-slf4j-impl-2.10.0中找到绑定.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J:有关说明,请参见http://www.slf4j.org/codes.html#multiple_bindings。SLF4J:实际绑定的类型为[ch.qos.logback.classic.util.ContextSelectorStaticBinder]
POM文件:
<!-- Start of required part to make log4j work -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- End of required part to make log4j work -->
Run Code Online (Sandbox Code Playgroud)