search

Home  >  Q&A  >  body text

java - 日志文件句柄如何设计比较好?

写了一个异步服务器,用于处理网站的非实时性请求。目前每秒约写入10-100条日志,高峰时期可能写入约1000条日志。

我能想到的关于日志文件处理方法有下面几种,请问一般是如何实现的呢?

方法一:
每次写日志时获得文件句柄,写入后关闭。

方法二:
句柄作为单例,存在与整个程序生命周期,只有程序结束时才关闭。

方法三:
句柄作为单例,存在与整个程序生命周期,定期关闭重新打开。

阿神阿神2770 days ago745

reply all(3)I'll reply

  • 黄舟

    黄舟2017-04-17 13:25:27

    Method 3 has been actually used in projects. Maybe my needs are slightly different from yours. The writing frequency of my logs generally does not exceed 10 entries/second, but the log files need to be saved on a daily basis and old files are deleted regularly (save logs of the past 7 days, etc.), so regular closing is a must~

    reply
    0
  • 迷茫

    迷茫2017-04-17 13:25:27

    Method 2, try opening the handle again when an error occurs

    reply
    0
  • PHPz

    PHPz2017-04-17 13:25:27

    My question is, under Windows 2008, if the log handle is open, it cannot be copied and deleted.
    This is very annoying. I hope I can view the log handle easily at any time. I don’t know how the first type of opening and closing frequency is very high, and how it affects the program and hard disk IO performance.

    I haven’t used the log4cpp cplus log system yet, I don’t know what their design is!

    reply
    0
  • Cancelreply