以下 3 篇文章提供了有关如何使用中间日志记录助手并仍然让底层记录器从客户端方法向该日志记录助手报告(而不是将日志记录助手方法作为源报告)的答案:
Java Logging: show the source line number of the caller (not the logging helper method)
Calling log4j's log methods indirectly (from a helper method)
Printing the "source" class in a log statement with a log4j wrapper
但似乎只提供 Log4j 1.2 的答案,它提供了现已解散的:
Category.log(String callerFQCN, Priority level, Object message, Throwable t).
Logger in the log4J 2.5 API 似乎没有明显的等价物.
任何人都可以提供与直接使用 Log4J 2.x 兼容的答案吗?
最佳答案
对于 Log4j2,答案完全是通过使用记录器包装器提供的,如 Log4j2 手册中描述的 Example Usage of a Generated Logger Wrapper .可以简单地生成(使用此处所示的 org.apache.logging.log4j.core.tools.Generate$ExtendedLogger 工具)具有单个 STUB 级别的记录器包装器,然后对其进行调整以创建自定义日志记录方法,模仿 logIfEnabled 的使用(FQCN、LEVEL、Marker、message、Throwable)——可能忽略 STUB 级别并使用常规级别——然后如果需要,删除或注释掉 STUB 级别及其方法)。为此,FormattedMessage 可能会有所帮助。
例子:
java -cp log4j-core-2.5.jar org.apache.logging.log4j.core.tools.Generate\$ExtendedLogger com.mycomp.ExtLogger STUB=350 > com/mycomp/ExtLogger.java
然后适配生成的类(省略大部分支持方法):
public final class ExtLogger extends ExtendedLoggerWrapper {
...
private final ExtendedLoggerWrapper logger;
private static final String FQCN = ExtLogger.class.getName();
private static final Level STUB = Level.forName("STUB", 350);
//Delete this afterwards if level not used.
private ExtLogger(final Logger logger) {
super((AbstractLogger) logger, logger.getName(), logger.getMessageFactory());
this.logger = this;
}
/**
* Returns a custom Logger with the name of the calling class.
*
* @return The custom Logger for the calling class.
*/
public static ExtLogger create() {
final Logger wrapped = LogManager.getLogger();
return new ExtLogger(wrapped);
}
/**
* Returns a custom Logger using the fully qualified name of the Class as
* the Logger name.
*
* @param loggerName The Class whose name should be used as the Logger name.
* If null it will default to the calling class.
* @return The custom Logger.
*/
public static ExtLogger create(final Class<?> loggerName) {
final Logger wrapped = LogManager.getLogger(loggerName);
return new ExtLogger(wrapped);
}
...
/**
* Logs a message object with the {@code STUB} level.
*
* @param message the message object to log.
*/
public void stub(final String message) {
logger.logIfEnabled(FQCN, STUB, null, message, (Throwable) null);
}
/**
* Example: Adapt with custom formatting.
* Here DEBUG level is used just as an example.
*
* @param name
* @param value
*/
public void echo(final String name, Object value) {
Message m = new FormattedMessage("echo: %s(%s)",name,value);
logger.logIfEnabled(FQCN, Level.DEBUG, null, m, (Throwable) null);
}
...
}
然后在客户端类中,它现在将通过记录器的辅助方法正确地“代表”该客户端进行记录,在本例中为格式化示例 echo(name,value):
public class TestLog4j {
private static final ExtLogger extLogger = ExtLogger.create(TestLog4j.class);
public static void elseWhere() {
extLogger.echo("aVariableName", 4);
}
public static void main(String[] args) {
extLogger.echo("aStringVariableName","from main");
elseWhere();
}
}
简单的图案布局:
<PatternLayout pattern=" %-5level [%C{1}::%M(%L)] %logger{36} - %msg%n"/>
输出:
DEBUG [TestLog4j::main(63)] testlogging.TestLog4j - echo: aStringVariableName(from main)
DEBUG [TestLog4j::elseWhere(42)] testlogging.TestLog4j - echo: aVariableName(4)
一旦掌握了将 logger.logIfEnabled(FQCN,...) 与 FQCN(log4j 在堆栈跟踪中搜索的)一起使用的窍门,您可能希望删除或注释掉 stub(..) 方法和 STUB 级别(如果您不使用其他级别)。
关于Java 日志记录 : Log4j Version2. x:显示终端客户端调用方的方法(不是中间日志记录帮助程序方法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35360887/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
我想用ruby编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序