我正在使用slf4j,我想对我的代码进行单元测试,以确保在某些条件下生成警告/错误日志消息.我宁愿这些是严格的单元测试,所以我不想从文件中提取日志配置以测试日志消息的生成.我正在使用的模拟框架是Mockito.
我想用SLF4J格式化我的双打并浮动到一定数量的小数位.
基本上,我正在寻找String.format("%.2f", floatValue)
SLF4J中相当于Java的东西.
阅读了SLF4J的文档并在谷歌上搜索我无法找到它是否具有该功能的提示.
我正在使用slf4j-api:1.7.6
和slf4j-jdk14:1.7.6
任何帮助深表感谢.
在这里帮我辩论.. :)
这里的slf4j站点http://www.slf4j.org/faq.html#logging_performance表示由于参数化日志记录,不需要日志记录保护.即不是写作:
if(logger.isDebugEnabled()) {
logger.debug("Entry number: " + i + " is " + String.valueOf(entry[i]));
}
Run Code Online (Sandbox Code Playgroud)
你可以逃脱:
Object entry = new SomeObject();
logger.debug("The entry is {}.", entry);
Run Code Online (Sandbox Code Playgroud)
这真的没问题,或者它是否会产生(虽然更低)创建传递给trace方法的静态字符串的成本..?
我想实现一个自定义记录器,它将所有日志条目记录到数据库中.目前我的应用程序以这种方式记录(slf4j和log4j绑定):
private static final Logger logger = LoggerFactory.getLogger( MyClass.class );
Run Code Online (Sandbox Code Playgroud)
我不知道该怎么办.我的想法是通过实现org.slf4j.Logger接口来实现自定义日志绑定
接下来的步骤是什么?我的目标是不改变当前的代码
我考虑的链接:
我正在尝试使用slf4j + java.util.logging.我知道如何设置Java源代码来通过logger = LoggerFactory.getLogger(...)
和logger.warn('...'
)或其他任何方式.
但是在slf4j中设置配置的文档在哪里?我很困惑...我有log4j手册,熟悉日志适配器的基础知识,但我不知道如何使用slf4j + java.util.logging.
即:
-D
我需要指定哪个.properties文件和/或JVM 命令行参数将其指向我的配置文件?
java.util.logging的配置文件的文档在哪里?
使用slf4j会导致我的配置文件发生任何变化吗?(即我必须以不同方式声明的东西,而不是直接使用java.util.logging或log4j)
我正在使用logback,我试图在我的Java程序中以编程方式设置日志文件名(类似于以编程方式设置Logback Appender路径),我尝试按如下方式调整该解决方案:
在logback-test.xml中:
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>log/${log_file_name}.log</file>
...
Run Code Online (Sandbox Code Playgroud)
然后再在我的Java程序中:
String logFileName = "" + System.currentTimeMillis(); // just for example
System.setProperty("log_file_name", logFileName);
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
ContextInitializer ci = new ContextInitializer(lc);
lc.reset();
try
{
// I prefer autoConfig() over JoranConfigurator.doConfigure() so I
// wouldn't need to find the file myself.
ci.autoConfig();
}
catch (JoranException e)
{
// StatusPrinter will try to log this
e.printStackTrace();
}
StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
Run Code Online (Sandbox Code Playgroud)
然而,结果是两个日志,一个完整并按我想要的名称命名,例如"1319041145343.log",另一个是空的并命名为"log_file_name_IS_UNDEFINED.log".如何阻止创建其他空日志文件?
这个问题以前曾被问过,但不幸的是,似乎没有任何解决方案对我有用.我正面临这个异常(使用删节堆栈跟踪):
java.lang.NoSuchMethodError: org.slf4j.spi.LocationAwareLogger.log(Lorg/slf4j/Marker;Ljava/lang/String;ILjava/lang/String;[Ljava/lang/Object;Ljava/lang/Throwable;)V
at org.apache.commons.logging.impl.SLF4JLocationAwareLog.debug(SLF4JLocationAwareLog.java:133)
at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager$1.getConnection(ThreadSafeClientConnManager.java:221)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:401)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
Run Code Online (Sandbox Code Playgroud)
使用命令行Maven进行编译时以及部署到Tomcat时会发生这种情况.它在IntelliJ IDEA中工作正常.
通常我希望这是由正在使用的SLF4J库的多个版本引起的.但Maven依赖树在单个版本中显示所有slf4j库:
..$ mvn dependency:tree | grep slf4j
[INFO] +- org.slf4j:slf4j-simple:jar:1.6.4:compile
[INFO] | \- org.slf4j:slf4j-api:jar:1.6.4:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.6.4:compile
[INFO] +- org.slf4j:jcl-over-slf4j:jar:1.6.4:compile
[INFO] +- org.slf4j:slf4j-simple:jar:1.6.4:compile
Run Code Online (Sandbox Code Playgroud)
我甚至确保〜/ .m2/repository中没有其他JAR
也没有对commons-logging库的引用(我将它们全部排除在依赖树之外.
我该如何解决这个问题?我的想法已经不多了.
编辑:这里要求完整的依赖项,首先是父POM:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${org.hibernate.validator.version}</version>
<exclusions>
<!-- Exclude SLF4j to avoid version conflicts (we have 1.6.2, this drags in 1.6.1) -->
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>${org.hibernate.validator.version}</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
<dependency> …
Run Code Online (Sandbox Code Playgroud) slf4j ×10
java ×9
logging ×6
dependencies ×1
jar ×1
liquibase ×1
log4j ×1
logback ×1
maven ×1
mocking ×1
mockito ×1
performance ×1
unit-testing ×1