Open schack opened 13 years ago
+1
This would be an amazing addition, I'm currently having the problem of having huge log files and I have to be rotating them manually.
People ask about this from time to time, and I also think it's a good idea. We would definitely accept a pull request for this!
+1
+1
I'll work on it. Seems like it wouldn't be too bad,BUT I'd rather beef it up to be able to accept a formatted suffix. Easier to bash script to it if there is something like a date string on the end.
FYI for linux users, you should be able to just hook it up without changing forever with the logrotate tool.
I'll work on it and see what they think.
+1
logrotated has this option built-in: copytruncate
anybody can tell my current status of this request?
I need this feature too...
+1
logrotated's copytruncate
will copy the logfile, then truncate the original. copies are not atomic and can therefore result in data loss when the original file is truncated. not a viable solution for some applications
copytruncate doesn't work and creates empty log files....
@fiosca i ended up writing / using https://www.npmjs.org/package/logrotate-stream instead.
@dstokes How are you using logrotate-stream together with forever? Could you post an example?
@mark-veenstra i moved from forever to upstart and used the format at https://github.com/dstokes/logrotate-stream#upstart-woes
+1
+1
+1
+1
+1
+1
+1
+1 most wanted feature
Since I'm interested in having this functionality too I can try to do it if nobody is already working on it. With log rotation i mean a functionality that renames the current log file by appending a progressive number (Eg. error.log.1 ) when the file reach a threshold size and creates a new logfile in place of the old one. Are you interested in only this basic feature or also to the followings?
@giannicic Winston already supports logrotating and forever
uses winston
. Perhaps it's as easy as reading a logrotate key in the config json and forwarding it to winston.
I like compression and deletion too. For that purpose, when logging something else, I use winston
streaming to logrotate-stream which supports compression and deleting. Perhaps forever can incorporate something like this.
In other words, be careful you don't reinvent the wheel. It's not as simple as it looks, because renaming the file and creating a new one causes forever to lose its file handle and the log will be undefined
from there on. You also cannot copy the log and truncate the old one outside of winston because the file cursor will not be reset, causing the truncated file to behave weird.
@Redsandro I've used the word "rename" improperly! :) Looking at the code seems to me that winston is used only for the forever log file. Maybe is not the right place, since is in forver-monitor code, but I'm also interested in rotating the stdout and stderr log files, and the logging of those files don't use winston. You are right about using existing tools rathen that starting from scratch with all the issues mentioned by you ( file handles, etc. ), I was not planning to do that. So, I'll have a look about using logrotate-stream for the forever log file. I'll also try to make the change for stderr and stdout in forever-monitor.
@giannicic interesting. I didn't know about the separate err
and out
logs. Is that something new(ish)? When doing forever list
every node has one single log file.
@Redsandro they are the ones defined by the -o and -e options (don't know if they are new or old) and they are optional. In the list command, as per the code, is showed only the forever logFile (the one that use winston) but is possible to change the conf and show other data like those additional log files.
I must correct myself! The forever logFile is an aggregate of stdout and stderr and doesn't use winston as evinced in lines 461-468 of https://github.com/foreverjs/forever/blob/master/lib/forever.js There is also a comment that explains it.
+1
Would a really simple first step here be to have a SIGHUP
handler that just closes and opens the file again? Then at least logrotate can rename the file then send the signal to forever
, which will then switch to using the new file. That would be a good start.
+1
+1
+1
+1
This must be top priority
+1
+1
+1
+1 :)
+1
+1
+1
Due to logging of console output i thought i may just go in between and override console.log (with winston):
npm install winston winston-daily-rotate-file --save
import * as winston from "winston";
import * as DailyRotate from "winston-daily-rotate-file";
const UniversalLogger = winston.createLogger({
level: "info",
format: winston.format.combine(
winston.format.timestamp(),
winston.format.printf(info => {
const time = new Date(info.timestamp).toLocaleString("en-EN");
return time + ": " + info.message;
})
),
transports: [
new DailyRotate({
filename: './log/%DATE%_server.log',
datePattern: 'DD.MM',
zippedArchive: false,
maxFiles: '7d'
})
]
});
console.log = (command: string, ...args: any[]) => {
UniversalLogger.info(require('util').formatWithOptions({colors: false}, command, ...args));
};
This just rotates 7 files on a daily basis and writes console output (as well as a timestamp) to the file.
Due to logging of console output i thought i may just go in between and override console.log (with winston):
npm install winston winston-daily-rotate-file --save
import * as winston from "winston"; import * as DailyRotate from "winston-daily-rotate-file"; const UniversalLogger = winston.createLogger({ level: "info", format: winston.format.combine( winston.format.timestamp(), winston.format.printf(info => { const time = new Date(info.timestamp).toLocaleString("en-EN"); return time + ": " + info.message; }) ), transports: [ new DailyRotate({ filename: './log/%DATE%_server.log', datePattern: 'DD.MM', zippedArchive: false, maxFiles: '7d' }) ] }); console.log = (command: string, ...args: any[]) => { UniversalLogger.info(require('util').formatWithOptions({colors: false}, command, ...args)); };
This just rotates 7 files on a daily basis and writes console output (as well as a timestamp) to the file.
Thank you - I also added this to morgan to write express.js logs to the same file
app.use( morgan("tiny", { stream: { write: (message) => UniversalLogger.info(message.trim()) }, }) );
Hi
It would be really nice with some sort of built-in logfile rotation feature
Regards Henrik Schack