首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是否有任何理由让log4j错误RollingFile日志成为RollingRandomAccessFile?

是否有任何理由让log4j错误RollingFile日志成为RollingRandomAccessFile?
EN

Stack Overflow用户
提问于 2020-01-15 11:34:33
回答 1查看 143关注 0票数 1

我读过几个问题,也读过文档,似乎唯一的区别是性能。

相关问题比较RollingFileRollingRandomAccessFile

在这个问题中,答案是关于unix logrotate utility的,但我不确定这是否与我的问题有关。

来自Apache文档

“RollingRandomAccessFileAppender与标准RollingFileAppender相似,只是总是缓冲(不能关闭),内部使用ByteBuffer + RandomAccessFile而不是BufferedOutputStream。与RollingFileAppender相比,性能提高了20-200%……”

如果不将错误日志处理为RollingRandomAccessFile而不是如下所示,会有什么重大原因吗?

代码语言:javascript
运行
复制
<RollingFile name="error" atr="atrib-val" >
  <PatternLayout>
    <Pattern>%d{date}</Pattern>
  </PatternLayout>
</RollingFile>

对于普通日志,错误处理是否需要与Appender不同?错误日志不需要常量缓冲区吗?

EN

回答 1

Stack Overflow用户

发布于 2022-09-20 02:48:24

Log4j的页上的异步记录器解释了这种权衡:

代码语言:javascript
运行
复制
Benefits:
    - Higher peak throughput. With an asynchronous logger your application can log messages at 6 - 68 times the rate of a synchronous logger.
      This is especially interesting for applications that occasionally need to log bursts of messages. Async logging can help prevent or dampen latency spikes by shortening the wait time until the next message can be logged. If the queue size is configured large enough to handle the burst, asynchronous logging will help prevent your application from falling behind (as much) during a sudden increase of activity.
    - Lower logging response time latency. Response time latency is the time it takes for a call to Logger.log to return under a given workload. Asynchronous Loggers have consistently lower latency than synchronous loggers or even queue-based asynchronous appenders.

Drawbacks:
    - Error handling. If a problem happens during the logging process and an exception is thrown, it is less easy for an asynchronous logger or appender to signal this problem to the application. This can partly be alleviated by configuring an ExceptionHandler, but this may still not cover all cases. For this reason, if logging is part of your business logic, for example if you are using Log4j as an audit logging framework, we would recommend to synchronously log those audit messages. (Note that you can still combine them and use asynchronous logging for debug/trace logging in addition to synchronous logging for the audit trail.)
    - In some rare cases, care must be taken with mutable messages. Most of the time you don't need to worry about this. Log4 will ensure that log messages like logger.debug("My object is {}", myObject) will use the state of the myObject parameter at the time of the call to logger.debug(). The log message will not change even if myObject is modified later. It is safe to asynchronously log mutable objects because most Message implementations built-in to Log4j take a snapshot of the parameters. There are some exceptions however: MapMessage and StructuredDataMessage are mutable by design: fields can be added to these messages after the message object was created. These messages should not be modified after they are logged with asynchronous loggers or asynchronous appenders; you may or may not see the modifications in the resulting log output. Similarly, custom Message implementations should be designed with asynchronous use in mind, and either take a snapshot of their parameters at construction time, or document their thread-safety characteristics.
    - If your application is running in an environment where CPU resources are scarce, like a machine with one CPU with a single core, starting another thread is not likely to give better performance.
    - If the sustained rate at which your application is logging messages is faster than the maximum sustained throughput of the underlying appender, the queue will fill up and the application will end up logging at the speed of the slowest appender. If this happens, consider selecting a faster appender, or logging less. If neither of these is an option, you may get better throughput and fewer latency spikes by logging synchronously.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59750606

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档