- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想关闭 1 个单元测试的日志记录(不会失败),因此堆栈跟踪不会显示在日志中。该堆栈跟踪应该存在于生产运行中,因为它是一个故障测试。
生产代码如下所示:
boolean failed = false;
for (int i = 0; i < 10; i++) {
try {
// Possible submits on Executor to run on other thread (not that it matters)
runTask(i);
} catch (RuntimeException e) {
// In the unit test, this pollutes the test log // BAD, MY PROBLEM
// In production, it shows up in the log immediately, before other tasks run // GOOD
logger.warn("Task failed. Stacktrace:", e);
failed = true;
}
}
if (failed) {
// The unit test checks if this exception is thrown,
// (it doesn't pollute the test log)
throw new IllegalStateException("at least 1 failed");
// In the real implementation, this also chains the first exception, to avoid eating the stacktrace
}
我有一个单元测试,它测试如果提交给执行程序的 10 个任务中至少有 1 个失败,执行程序会抛出异常。因为执行器在发生内部异常时不会失败,所以它会在抛出外部异常之前先运行其他任务。因此,当它捕获到内部异常时,它会将其记录下来,并在日志中提供堆栈跟踪。对于 QA,这是一种误导,因为测试日志显示堆栈跟踪,尽管所有测试都成功了。我想摆脱测试日志中的堆栈跟踪 (jira)。
最佳答案
我认为没有针对 slf4j 的解决方案,因为支持的框架可能不支持此功能。
如果您将 slf4j 与 log4j 绑定(bind)一起使用,您可以实现它。
// get the logger you want to suppress
Logger log4j = Logger.getLogger("sub.optimal.package");
// keep the current level
Level previousLogLevel = log4j.getLevel();
// set the level to suppress your stacktrace respectively
log4j.setLevel(Level.WARN);
在下面找到一个如何在方法级别配置记录器的示例。您需要配置不同的记录器,以便在生产环境和测试环境中更改记录级别。
假定以下目录结构和文件
./Main.java
./slf4j-api-1.7.10.jar
./slf4j-log4j12-1.7.10.jar
./log4j-1.2.17.jar
./prod/log4j.properties
./testing/log4j.properties
./bin/
主.java
package sub.optimal;
import java.io.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Main {
Logger logger;
Logger loggerSuppressible;
{
logger = LoggerFactory.getLogger(this.getClass());
loggerSuppressible = LoggerFactory.getLogger("loggerSuppressible");
}
public void doSomeFoo() {
logger.info("logging from doSomeFoo");
}
public void doSomeStacktrace() {
try {
new File("..").renameTo(null);
} catch (NullPointerException npe) {
loggerSuppressible.error("something went wrong", npe);
}
}
public static void main(String[] args) {
Main main = new Main();
main.doSomeFoo();
main.doSomeStacktrace();
}
}
./prod/log4j.properties
log4j.rootCategory=info, console
# production setting
log4j.category.loggerSuppressible=info, console
log4j.additivity.loggerSuppressible=false
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold=info
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
./testing/log4j.properties
log4j.rootCategory=info, console
# testing setting
log4j.category.loggerSuppressible=fatal, console
log4j.additivity.loggerSuppressible=false
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold=info
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
编译
javac -cp ${classpath_logger} -d bin/ Main.java
** 在生产环境中运行**
java -cp bin/:prod/:${classpath_logger} sub.optimal.Main
** 在测试中运行**
java -cp bin/:testing/:${classpath_logger} sub.optimal.Main
产出产出
2015-03-05 14:09:50,707 [main] INFO sub.optimal.Main - logging from doSomeFoo
2015-03-05 14:09:50,707 [main] ERROR loggerSuppressible - something went wrong
java.lang.NullPointerException
at java.io.File.renameTo(File.java:1386)
at sub.optimal.Main.doSomeStacktrace(Main.java:22)
at sub.optimal.Main.main(Main.java:32)
输出测试
2015-03-05 14:09:50,847 [main] INFO sub.optimal.Main - logging from doSomeFoo
关于java - Slf4j 或 Logback : Turn off logging for 1 unit test (or 1 thread),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28807545/
不同的 LogCat 方法是: Log.v(); // Verbose Log.d(); // Debug Log.i(); // Info Log.w(); // Warning Log.e();
在android群里,经常会有人问我,android log是怎么用的,今天我就把从网上以及sdk里东拼西凑过来,让大家先一睹为快,希望对大家入门android log有一定的帮助. android
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 4 年前。 社区 12
我正在尝试使用 sonarlint 检查代码质量.上面的问题概要,我不明白为什么它要说要大写。但是 this discussion与上述建议相反。哪一个应该被认为是正确的? 最佳答案 这没有正确答案,
随着 n 变大,log*(log n) 和 log(log* n) 这两个函数会更快吗? 这里,log* 函数是迭代对数,定义如下: 我怀疑它们是相同的,只是写法不同,但它们之间有什么区别吗? 最佳答
作为家庭作业,我被要求在 O(log(n)) 中编写一个算法,我可以计算出我编写的算法的复杂度为 O(log(n) + log(n/2) + log(n/4) + log(n/8) + ... + l
我正在使用 Tomee。日志文件夹包含这样的文件 localhost_access_log.2016-12-02.txt localhost.2016-12-02.log catalina.2016-
Android Log.v、Log.d、Log.i、Log.e 等的 ios 等效项是什么?同样在 android 上,我使用 Android 设备监视器和 logcat 来访问我的手机日志,我需要在
我认为下面的代码是 O(log log n) 因为它里面有 i*i 但我对 log n 感到困惑> 和 log (log n)。 for (i=2; i*i<=number; i++) { if
我正在修改 kvm 模块,并在内核代码中添加了 printk 语句。运行虚拟机后,printk 为我提供了错误地址和有关 guest 操作系统的其他信息。 我需要从这个信息中生成统计信息。当我使用 d
我有一个部署为 Windows Azure Web 角色的 WCF 服务。 我正在使用 Enterprise Library 进行异常处理,并且在我的本地 Development Fabric 中,似
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 9 年前。 Improv
在 Go 的生产中使用 log.SetFlags(log.LstdFlags | log.Lshortfile) 是好的做法(至少是一般做法)吗?我想知道在生产中这样做是否存在性能或安全问题。因为它不
我想知道什么更快: double value = Math.log(a) - Math.log(b); 或 double value = Math.log(a/b); 我计算值的方式是否会对性能产生影
我有数百个子例程使用 log.Println() 写入日志文件 我正在使用 log.Println 写入 error.log 文件。 func main() { e, err := os.Open
我将 Nuxt 与 SSR 一起使用,并希望有类似于 apaches 的 access.log 和 error.log 的东西 我特别感兴趣的是每次调用的响应时间。 我在 nuxt 文档中找不到任何内
我知道以前有人问过这个问题,但我相信这是一个不同的问题。 Nginx 在 www-data 下运行: $ ps -eo "%U %G %a" | grep nginx root root
我在我的日志文件中发现了一个非常奇怪的条目 Jan 29 01:35:30 vs-proj-handy sshd[5316]: Received disconnect from 130.207.203
对于我正在开发的应用程序,我希望在开发过程中和发布时简化故障排除。我希望能够检索到对 Log 的调用,以了解在 USB 调试中没有连接手机的情况下运行应用程序时的调用,以便可以检索并发送给我。例如,当
我试图捕获 panic 并记录错误: func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloRep
我是一名优秀的程序员,十分优秀!