foreversd / forever

A simple CLI tool for ensuring that a given script runs continuously (i.e. forever)
http://github.com/foreverjs/forever
MIT License
13.87k stars 946 forks source link

Built in logrotation #128

Open schack opened 13 years ago

schack commented 13 years ago

Hi

It would be really nice with some sort of built-in logfile rotation feature

Regards Henrik Schack

ivolo commented 13 years ago

+1

diegovar commented 12 years ago

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.

AvianFlu commented 12 years ago

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!

giacecco commented 12 years ago

+1

kof commented 12 years ago

+1

gpatmore commented 12 years ago

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.

frankvm04 commented 12 years ago

+1

jadell commented 11 years ago

logrotated has this option built-in: copytruncate

vinnitu commented 11 years ago

anybody can tell my current status of this request?

I need this feature too...

barbogast commented 11 years ago

+1

dstokes commented 10 years ago

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

fiosca commented 9 years ago

copytruncate doesn't work and creates empty log files....

dstokes commented 9 years ago

@fiosca i ended up writing / using https://www.npmjs.org/package/logrotate-stream instead.

mark-veenstra commented 9 years ago

@dstokes How are you using logrotate-stream together with forever? Could you post an example?

dstokes commented 9 years ago

@mark-veenstra i moved from forever to upstart and used the format at https://github.com/dstokes/logrotate-stream#upstart-woes

jamesongithub commented 9 years ago

+1

giobero commented 8 years ago

+1

nleush commented 8 years ago

+1

marcocarvalho commented 8 years ago

+1

jeffmcmahan commented 8 years ago

+1

cvillamor commented 8 years ago

+1

clodio commented 8 years ago

+1

ChuanyuWang commented 8 years ago

+1 most wanted feature

Redsandro commented 8 years ago

Any news?

http://stackoverflow.com/questions/15231968/nodejs-forever-archive-logs#15232276

giannicic commented 8 years ago

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?

Redsandro commented 8 years ago

@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.

giannicic commented 8 years ago

@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.

Redsandro commented 8 years ago

@giannicic interesting. I didn't know about the separate err and out logs. Is that something new(ish)? When doing forever listevery node has one single log file.

giannicic commented 8 years ago

@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.

giannicic commented 8 years ago

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.

jony89 commented 7 years ago

+1

leonerd commented 7 years ago

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.

zlodes commented 7 years ago

+1

jp1987 commented 7 years ago

+1

rejhgadellaa commented 7 years ago

+1

cuongv commented 7 years ago

+1

HariharanManoharan commented 6 years ago

This must be top priority

abdulmoizeng commented 6 years ago

+1

artakvg commented 6 years ago

+1

Robert-Ernst commented 6 years ago

+1

lisbakke commented 6 years ago

+1 :)

osehgol commented 6 years ago

+1

wave2 commented 6 years ago

+1

giltsl commented 5 years ago

+1

Joniras commented 4 years ago

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.

wvoelcker commented 4 years ago

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()) }, }) );