库:Log4J和Log4Net(分别用于Java和.NET)
来自Log4J网站:
在代码中插入日志语句是一种调试它的低技术方法。它也可能是唯一的方法,因为调试器并不总是可用或适用。分布式应用程序通常就是这种情况。
另一方面,有些人认为日志语句会污染源代码并降低可读性。 (我们认为恰恰相反)。在预处理器不可用的Java语言中,即使关闭日志记录,日志语句也会增加代码的大小并降低其速度。鉴于合理大小的应用程序可能包含数千个日志语句,速度特别重要。
使用log4j,可以在运行时启用日志记录而无需修改应用程序二进制文件。 log4j包的设计使得这些语句可以保留在已发布的代码中,而不会产生很高的性能成本。可以通过编辑配置文件来控制日志记录行为,而无需触及应用程序二进制文件。
日志记录为开发人员提供了应用程序故障的详细上下文。另一方面,测试提供了应用程序的质量保证和信心。不应混淆记录和测试。它们是互补的。当明智地使用日志记录时,它可以证明是一个必不可少的工具。
There are few things to consider when you decide your logging philosophy to make it performance conscious. First let us breakdown the resources spent on logging.
- CPU time spent on appending or assembling a particular log line & garbage collection
- Working memory spent on appending or assembling a particular log line
- CPU time spent on writing to the destination (I/O), can be file, console, network etc.
- Wall time spent on waiting or fighting on a shared resource or shared destination. This may be a file on disk or a synchronous method inside the logging framework.
- Disk space or any other storage occupied to persist the log file
Following are the things that you can do to minimize the above.
- Have controls over logging. Have proper log levels so can only INFO,WARN,ERROR will be used in production, and DEBUG, TRACE will be used in development. Most importantly levels should not control writing the logs to the destination, but it should also control assembling log lines as well. To achieve this, use parameterized logging ( https://www.slf4j.org/faq.html#logging_performance ) or simply check the log level is enabled before assembling the log line. Using a lambda is an alternative as well.
- Avoid costly formatting. If pretty printing is needed write a log extraction tool convert raw log lines to pretty log lines, which will happen offline, and on demand. There are lot of useful things that can be easily done if the logs are forwarded a tool like Splunk or ElasticSearch.
- Minimize log size. Use shorthand codes to minimize the size of a log lines while preserving the readability.
- Minimize log frequency. When deciding to log something, roughly estimate on how much log size/transaction or how many log lines/ transaction. If it is more than 1:100, then that is too much logging, well it depends.
- Try asynchronous logging. In other words do not write to the destination (I/O) in the transaction thread, rather add it to a buffer and tackle it in the background. RandomAccessFileAppender is such facility provided by Log4J
- Divide and conquer. Have the logs segmented. Later in production, this can be used to spread the load on destinations (I/O) by having different destinations (files) for different logs