Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i want to delete log files myself, what should i do? #1111

Closed
iguxiaobei opened this issue Dec 23, 2021 · 8 comments
Closed

i want to delete log files myself, what should i do? #1111

iguxiaobei opened this issue Dec 23, 2021 · 8 comments
Labels
question Further information is requested windows Bugs that seem specific to windows

Comments

@iguxiaobei
Copy link

I want to delete log files myself, What should i do?

I use electron, and I want to start a intervel task to delete the log files myself, but It seems like that i have no permission to do this. first time, I can delete old logs, but second time, I can't delete new files.

@lamweili lamweili added the question Further information is requested label Feb 6, 2022
@lamweili
Copy link
Contributor

lamweili commented Feb 6, 2022

  1. How are you deleting the log files? Do you have the code for it?
  2. Which OS?
  3. You mean you can delete old log files but not the current (hot) log file?

@iguxiaobei
Copy link
Author

  1. How are you deleting the log files? Do you have the code for it?
  2. Which OS?
  3. You mean you can delete old log files but not the current (hot) log file?
  1. just fs.unlinkSync(path);
  2. windows 7
  3. yes

eg:

  1. I start a program (electron program), this program generates 3 files. 2022-01-01.log 2022-01-02.log 2022-01-03.log
  2. I need delete 2022-01-01.log and 2022-01-02.log.
  3. first time, I can delete this two files, but, 2022-01-01.log file still exists, and it can't be open, when I exit this electron program, 2022-01-01.log disapear...
  4. before I exit this electron program, I can read 2022-01-01.log file in directory, but when i use fs.stat(path[, options], callback) to get 2022-01-01.log file's infomation, I got an error...

@lamweili
Copy link
Contributor

lamweili commented Feb 24, 2022

  1. What is your log4js version?
  2. Do you have the code snippet of your log4js.configure()?
    I would like to check the appender pattern so that I can understand the rolling.

@lamweili lamweili added the windows Bugs that seem specific to windows label Mar 5, 2022
@lamweili
Copy link
Contributor

lamweili commented Mar 5, 2022

This seems to be similar to #711 (comment).
In Windows, when a file is deleted, if there are existing handlers, the file will remain in a "zombie-state" till the handlers are closed or when NodeJS exits.

Is there a use-case as to why you would need to manually delete those files when log4js can be configured for rollover?


In any case, log4js does not hold onto the previous log files once they are rolled over.
Thus, you should not be experiencing this issue. I successfully deleted in my code snippet below:

const log4js = require('log4js');
log4js.configure({
  appenders: {
    'out': { type: 'stdout' },
    'everything': { 
      type: 'dateFile', 
      filename: 'logs/trace.log',
      pattern: 'yyyy-MM-dd-hhmmss',
      keepFileExt: true,
      numBackups: 30 // total 31 files (1 hot + 30 backups)
    }
  },
  categories: {
    default: { appenders: [ 'out', 'everything' ], level: 'debug'}
  }
});

const logger = log4js.getLogger();
let count = 1;
let timer = setInterval(() => {
  logger.warn("testing", count);

  let dirList = fs.readdirSync("logs");
  console.log("[current] dirList in logs:", dirList);

  if (count >= 5) { // spam 5 times
   clearInterval(timer);

   // existing files
   logger.warn("testing", count, "- deleting");
   const fileToDelete = path.join("logs", dirList[0]);
   console.log("file to delete:", fileToDelete);

   // delete 1 old file
   fs.unlinkSync(path.join("logs", dirList[0]));
   dirList = fs.readdirSync("logs");
   console.log("[deleted] dirList in logs:", dirList);
  }

  count++;
}, 1000);

image

@iguxiaobei
Copy link
Author

This seems to be similar to #711 (comment). In Windows, when a file is deleted, if there are existing handlers, the file will remain in a "zombie-state" till the handlers are closed or when NodeJS exits.

Is there a use-case as to why you would need to manually delete those files when log4js can be configured for rollover?

In any case, log4js does not hold onto the previous log files once they are rolled over. Thus, you should not be experiencing this issue. I successfully deleted in my code snippet below:

const log4js = require('log4js');
log4js.configure({
  appenders: {
    'out': { type: 'stdout' },
    'everything': { 
      type: 'dateFile', 
      filename: 'logs/trace.log',
      pattern: 'yyyy-MM-dd-hhmmss',
      keepFileExt: true,
      numBackups: 30 // total 31 files (1 hot + 30 backups)
    }
  },
  categories: {
    default: { appenders: [ 'out', 'everything' ], level: 'debug'}
  }
});

const logger = log4js.getLogger();
let count = 1;
let timer = setInterval(() => {
  logger.warn("testing", count);

  let dirList = fs.readdirSync("logs");
  console.log("[current] dirList in logs:", dirList);

  if (count >= 5) { // spam 5 times
   clearInterval(timer);

   // existing files
   logger.warn("testing", count, "- deleting");
   const fileToDelete = path.join("logs", dirList[0]);
   console.log("file to delete:", fileToDelete);

   // delete 1 old file
   fs.unlinkSync(path.join("logs", dirList[0]));
   dirList = fs.readdirSync("logs");
   console.log("[deleted] dirList in logs:", dirList);
  }

  count++;
}, 1000);

image

i think it's the same problem...

i want put the log file to server, and the server read log file content,and save content to elk.so i want delete files when the files have put to server...

@lamweili
Copy link
Contributor

lamweili commented Mar 8, 2022

@iguxiaobei Which version of log4js are you using? Please test with minimally log4js@^6.4.0.

This issue would also occur (on Windows) in log4js <= 6.3.0 if log4js.configure() is called multiple times. In those versions, when log4js.configure() is called multiple times, multiple handlers are created for the same files without closing. When the last configuration attempts a rollover on the file, it will be blocked as the file handlers from previous configurations are still holding onto the file. In that case, the only effective way would be to exit NodeJS. That was patched in PR #1113 from log4js@^6.4.0.


To delete the files programmatically via NodeJS on Windows, you have to ensure all file handlers are closed.

If you are using log4js and on Windows,

  • for the old log files or files that have been rolled over (log.1, log.2, etc.), there are no opened handlers.
    • no problem using a custom code to read, save to elk and delete.
  • for the hot log file, there is currently an open handler.
    • no problem using custom code to read, save to elk.
    • for delete, after executing unlink or similar, Windows will not delete the file due to the opened handler.
      • Windows will only delete the file after the handler has been closed.
        • log4js will always hold onto the hot file (to write logs) until log4js.shutdown() or NodeJS exits.
      • This is a Windows-specific behaviour that does not happen on Linux.

If you really want to, albeit destructive, you can call log4js.shutdown(() => {}) with your read/save/delete code in the callback and re-configure log4js once done.

log4js.shutdown(() => {
  // read/save/delete code here
  log4js.configure(options);
});

@lamweili
Copy link
Contributor

@iguxiaobei, does the comment above resolve your issue?

@iguxiaobei
Copy link
Author

@iguxiaobei, does the comment above resolve your issue?

i think it has resolved,thank you,i will close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested windows Bugs that seem specific to windows
Projects
None yet
Development

No branches or pull requests

2 participants