rotating-file-stream
rotating-file-stream copied to clipboard
While rotating log file gives EPERM: operation not permitted, stat error
I am trying to use rotating file stream in my vue-electron app. My goal is to rotate log file once it reaches 5 MB.
It works fine if the log file size is greater than 5 MB and on app launch it rotates the log file while creating fresh log file seamlessly.
However when the file size is roughly 4.99 MB and during my app's on-going activity the file size (5MB) is reached, it throws error: EPERM: operation not permitted, open error '{path to log file/file.log}' And [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed
My code: package.json rotating-file-stream version: "2.1.3"
Logger.js
const pad = num => (num > 9 ? '' : '0') + num
const generator = (time, index) => {
if (!time) return ${LOG_FILE_NAME}
const month = time.getFullYear() + '' + pad(time.getMonth() + 1)
const day = pad(time.getDate())
const hour = pad(time.getHours())
const minute = pad(time.getMinutes())
let fileName = ${LOG_FILE_NAME}-${month}${day}-${hour}${minute}${index}
return fileName
}
const rfs = require('rotating-file-stream') const stream = rfs.createStream(generator, { maxSize: '5M', maxFiles: 5, size: '5M', path: LOG_PATH, compress: 'gzip' })
stream.on('error', err => console.log('RFS error -->', err) )
const FileLogger = {
log: function (message) {
stream.write(\n${moment().tz(moment.tz.guess()).format('MMM DD YYYY, hh:mm:ss A')} - ${message}
)
}
}
export default FileLogger
Now I understand the ERR_STREAM_DESTROYED because it is destroyed hence further write calls are causing the error. How can this be fixed?
Hi @ankityadav1 ,
I can't say if the error depends on the problem I can see, but for sure your options are so strange:
maxSize: '5M',
size: '5M',
this means "please rotating-file-stream, rotate my log file every 5MB and do not allow more than 5MB for all the rotated files".
I suggest to remove (or increase the value of) maxSize
.
Please let me know if this cleaning action solves your problem, if yes you discovered a strange bug.
Thank you, iCC
Hi iCC,
Thanks for responding. My requirement is to compress the rotated log files and the existing log files are mostly larger in size. Hence the code says maxSize 5MB and for the new log files, I want rotating-file-stream to generate slog files of 5MB max. And it works!! It compresses the log files after rotation and its hardly few bytes after that.
Lastly I tried your suggestion and removed maxSize option but still facing the same issue.
Do you want me to paste any more of my code here? It seems this is a bug.
No thanks, no other code is required, @ankityadav1 . If possible could you please cut/paste the stack trace of the EPERM error?
BTW, are you sure you have full write access on LOG_PATH
?
Thank you
Yes I have full access to the folder.
Here you go!
The below screenshot is when I try to open the log file after the EPERM error:
And this screenshot refers to the ERR_STREAM_DESTROYED message after the log file rotation occurs and my code tries to push data to the log file:
Hi @ankityadav1 ,
thank you but there are not the info I need: the first screenshot gives no info while the stack trace of the ERR_STREAM_DESTROIED
is irrelevant.
I need the stack trace of the EPERM
error. The output of this code should contain the info I need.
stream.on('error', err =>
console.log('RFS error -->', err)
)
The error has no stack trace. It is just: EPERM: operation not permitted, open error '{path to log file/file.log}'
I'm sorry @ankityadav1 , but I'm not able to reproduce (so to investigate) the error. If you could share with me a minimal reproducible example I can try to investigate it.