简体   繁体   English

在Log4j的日志条目中包含日志文件名

[英]Including log file name in log entry in Log4j

I have a requirement to include the name of the log file in the log entry itself. 我需要在日志条目本身中包含日志文件的名称。

For example say final name of the log file is something like trx_log.2014-09-22-12-42 the log entries I'm printing that log should have that same name. 例如,说日志文件的最终名称类似于trx_log.2014-09-22-12-42我打印的日志条目该日志应具有相同的名称。 Following is a example log entry. 以下是一个示例日志条目。

123456|test value|xyz|trx_log.2014-09-22-12-42

I'm using Log4j DailyRollingFileAppender to print the log at the moment. 我正在使用Log4j DailyRollingFileAppender来打印日志。 Is there a way which I can get this requirement implemented using some log4j/logback configuration. 有没有办法可以使用一些log4j / logback配置实现此要求。

Not that I'm aware of. 不是我知道的。

But a solution does exist nevertheless: write your own custom extension of the DailyRollingFileAppender . 但是仍然存在一种解决方案:编写自己的DailyRollingFileAppender自定义扩展。

Please note though the filename will be available to your custom appender only: in case you want to use such information in another appender (the only use case I can think of this might be of any use) then you need a more convoluted solution using a shared data storage (shared memory, file system, database, whatever) with the simplest solution being a static member of your just made appender. 请注意,虽然文件名只能用于您的自定义appender:如果您想在另一个appender中使用此类信息(唯一的用例我可以想到这可能有用),那么您需要使用更复杂的解决方案共享数据存储(共享内存,文件系统,数据库,等等),最简单的解决方案是刚刚建立的appender的静态成员。 In this case the other appender (lat's say Console) need to be extended as well in order to append the new information to the log statement. 在这种情况下,另一个appender(lat表示Console)也需要扩展,以便将新信息附加到日志语句中。

Use this method logger.getName() 使用此方法logger.getName()

logger.log(Level.SEVERE,"Exception in "+e.getMessage()+logger.getName()); logger.log(Level.SEVERE,“Exception in”+ e.getMessage()+ logger.getName());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM