All jobs

Add Log Rotation to a Winston Logger

Yes. An agent adds a dated, size-capped file transport beside your console output, keeps the pm2 and Docker setups working, tests the rotation, and opens a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads where logs go today. In an Express API laid out like hagopj13/node-express-boilerplate, src/config/logger.js builds one winston.createLogger at level debug in development and info otherwise, with a format that writes an error's stack as its message, colorizes only in development and prints level: message, and a single Console transport with stderrLevels: ['error']. src/config/morgan.js streams every request line into that logger, and src/middlewares/error.js logs each error through it in development. ecosystem.config.json runs src/index.js under pm2 with time: true, so pm2 adds the timestamp and captures stdout into its own files; the Dockerfile runs as the node user in /usr/src/node-app; .gitignore ignores node_modules, coverage and .env* but has no logs entry. package.json pins winston at ^3.2.1.

It adds winston-daily-rotate-file as a dependency and a second transport in logger.js: a DailyRotateFile with filename carrying the %DATE% placeholder, a datePattern of one file a day, maxSize so a noisy day still rolls, maxFiles as a number of days so old files are removed, zippedArchive on, and dirname from a new LOG_DIR variable in src/config/config.js, where every other setting is validated with Joi and defaulted. The console transport stays, because pm2 and Docker both read stdout; the file transport is for the host where someone opens a log after the fact. In the test environment the file transport is not added, so yarn test writes nothing to disk.

The file transport gets its own format: the same error enumeration and splat, then timestamp and JSON, because a rotated file is read by tools, not eyes, while the console keeps the short level: message line. The agent also adds a logs/ entry to .gitignore and .dockerignore, creates the directory in the Dockerfile with chown to node so the unprivileged process can write it, and documents LOG_DIR in .env.example.

It proves the rotation in its sandbox rather than describing it. With LOG_DIR pointing at a temp directory and maxSize set low for the trial, it starts the app against a MongoDB installed from MongoDB's own package repository, sends enough requests through morgan to pass the size, and lists the directory: the current file, the rolled one with its suffix, and the audit JSON the transport keeps to know which files it owns. It restores the real settings, runs yarn test and yarn lint, and quotes the directory listing in the pull request.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add log rotation to the Winston logger. Read package.json, src/config/logger.js, src/config/morgan.js, src/config/config.js, src/middlewares/error.js, ecosystem.config.json, Dockerfile, .dockerignore and .gitignore first. Add winston-daily-rotate-file and a DailyRotateFile transport beside the existing Console transport, with a dated filename, a maxSize, maxFiles as days, zipped archives and a dirname from a new LOG_DIR config value validated in config.js with a default. Give the file transport a timestamped JSON format and keep the console format as it is. Do not add the file transport in the test environment. Ignore the log directory in git and Docker and create it writable for the node user in the Dockerfile. Install a MongoDB server in the sandbox, start the app with a small maxSize, send requests until a file rolls, list the directory, restore the real values, run yarn test and yarn lint, and open a pull request with the listing.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

What comes back is a pull request that changes logger.js, config.js, .env.example, .gitignore, .dockerignore and the Dockerfile, and adds one dependency. The description shows the transport's options with a sentence each, the directory listing from the trial rotation with the rolled file and the audit file, and the yarn test and yarn lint output.

The run's record keeps the requests the agent sent and the listing it took after each, so a reviewer can see the file roll instead of trusting that the options are right.

Before merging, a reviewer picks the retention and the size, and decides whether the host also ships stdout somewhere; if it does, the file transport is for local forensics and the retention can be short.

Run it on every service that logs to stdout only

A team with several Node services started from this boilerplate runs one POST /o/{org}/sessions per repository, this prompt as input and the repository in resources. Each run reads its own logger.js, so a service that already writes files gets only the rotation options and the ignore entries, and one that logs JSON already keeps its format.

Send a request_id that is a UUID v5 of the repository's full name so a retried call returns the run already started. A webhook_url on the API key delivers run.completed with the session_url to read the pull request from.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

A file transport without rotation is a disk that fills up on the quietest weekend of the year, and rotation options are easy to set without ever seeing a file roll. The agent runs the app in its own sandbox with a real database behind it, pushes real requests through morgan, and watches the directory, so the pull request carries the evidence and not a guess.

The change is one transport and a config value, so review is quick, and the same run fits every service on the boilerplate at once, each in its own sandbox with its own pull request.

Frequently asked questions

Why keep the console transport if files are written?

Because pm2 and Docker both capture stdout, and the deploy may already ship it to a log service. The file transport adds a local copy that survives a container restart and can be read with grep; it does not replace what the platform collects.

How does the transport know which files to delete?

It keeps an audit JSON file in the log directory listing the files it created, and maxFiles removes only files on that list. The trial listing in the pull request shows that file beside the logs. A file someone copies in by hand is never touched.

Will tests write log files?

No. The file transport is added only when config.env is not test, the same switch src/app.js uses to skip the morgan handlers in tests, so yarn test leaves the disk alone.

Does the Docker image need anything else?

The directory has to exist and be writable by the node user the image switches to before yarn install. The agent adds that to the Dockerfile and suggests mounting the directory as a volume in docker-compose.yml so logs survive a container being replaced, which the pull request leaves as a reviewer's choice.

More Winston jobs

The same job on other stacks

More jobs like this: Hand off a long task

Keep reading