【问题标题】:Append new data from a logfile to another file将新数据从日志文件附加到另一个文件
【发布时间】:2015-10-17 05:38:44
【问题描述】:

如何判断新数据是否写入日志文件以及如何提取这些新数据并将它们写入另一个文件?

我的目标是为调试目的创建一个大日志文件,因为如果文件达到特定大小,当前日志文件总是会删除数据。

我唯一的想法是每隔几分钟从旧日志文件创建一个副本。

【问题讨论】:

  • 如果您需要这些数据,我建议您找出删除数据的过程,而不是考虑复制。通常,日志文件由 logrotate 设置“轮换”,这是一个基于 cron 的计划任务,用于轮换和压缩日志文件。您可以更改其配置。只需查看文件夹/etc/logrotate.conf/etc/logrotate.d。您可以删除删除数据的作业或将其更改为仅旋转而不是删除或您喜欢的任何内容。
  • 我的设备中没有此文件。
  • 那么肯定有除logrotate之外的其他东西负责删除数据。数据不会凭空消失。我会看看那个...

标签: linux windows lua


【解决方案1】:

快速而肮脏的方法是在控制台中键入以下行 - 将“path/to/...”和“other/path/...”替换为实际路径和日志文件:

* * * * * /path/to/small_file.log >> /other/path/to/big_file.log

它不会在每次写入时执行 IO,但它会每分钟执行一次,这可能足以满足您的需求,也可能不够。

编辑:尝试使用 C 找到更好的方法,这是我目前得到的(阅读我在帖子中的评论了解更多信息)。

//Include the full pathname and file for LOG_BIG and LOG_SMALL
#define LOG_BIG "/var/.../log_example_big.txt"
#define LOG_SMALL "/var/.../log_example_small.txt"
//FIXME: change LOG_BIG and LOG_SMALL to match the locations of the logfiles

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <math.h>
#include <sys/types.h>

time_t last_mod_time(const char *path);


int main(int argc, const char * argv[]) {
    char outstr[200];
    time_t t;
    struct tm *tmp;

    t = time(NULL);
    tmp = localtime(&t);

    // check for local time set failure
    if (tmp == NULL) {
        perror("localtime");
        return 0;
    }
    //if the returned size_t for strftime is 0, we exit
    if (strftime(outstr, sizeof(outstr), argv[1], tmp) == 0) {
        fprintf(stderr, "strftime returned 0");
        return 0;
    }
    double diff_log_mod_time;
    // get the difference of last modified time between LOG_BIG and LOG_SMALL
    diff_log_mod_time = difftime(last_mod_time(LOG_BIG),last_mod_time(LOG_SMALL));

    //difference in log modification times should be close to 0 +/- 10 ... I think
    if(fabs(diff_log_mod_time) > 10.0) {

        /* to finish the code, we would need to find the difference between the data in LOG_BIG and LOG_SMALL (assuming that LOG_BIG should contain all of LOG_SMALL and then some) */

    }
    exit(EXIT_SUCCESS);
}

/**
 * last_mod_time - this function finds the last modification time for a filename specified and returns 
 * it in epoch time (seconds lapsed since 1/1/1970)
 */
time_t last_mod_time(const char *path) {
    struct stat statbuf;
    if (stat(path, &statbuf) == -1) {
        perror(path);
        exit(1);
    }
    return statbuf.st_mtime;
}

【讨论】:

  • 谢谢你的回答,我得到-sh: *: not found
  • 对不起,我没有先测试就发布了。在研究了一些之后,我无法找到一个完整的 shell 脚本解决方案,所以我决定用 C 编写它。我成功地获取了两个日志文件的最后修改日期并检查了差异它们之间 - 这有助于确定两个文件之间是否真的存在差异。明天我会提供一个更好的解释,但这是我到目前为止所得到的 - 希望它有所帮助。
  • 似乎他的意思是将其添加到 crontab。见How to redirect output to a file from within cron?我会相应地编辑答案。
【解决方案2】:

实现此目的的另一种快速而肮脏的方法是执行以下操作:

cp /path/to/small_file.log /other/path/to/big_file.log;
nohup tail -f /path/to/small_file.log >> /other/path/to/big_file.log &

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-09
    • 2020-03-19
    • 2019-10-01
    • 2018-12-06
    • 2019-08-15
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多