The Log sheet was the best decision you made this year. Six months later it has 80,000 rows, the tab takes five seconds to open, and filtering for last Tuesday’s errors feels like a chore. A log nobody opens is just a slower spreadsheet.
You don’t need a logging platform. You need rotation: once a month, the current Log becomes Log 2026-10, and a fresh, empty Log takes its place.
TL;DR
- Monthly trigger (or a row-count check) runs
rotateLog - Rename
Log→Log YYYY-MM(the month it covers, in the sheet’s timezone) - Insert a fresh
Logwith the same headers and frozen header row - The logger keeps writing to the tab named
Log, so no code changes - Optional: delete or export months older than N
This builds on A Log sheet you’ll actually read later. That guide’s logger looks up the tab by name (Log) and trims old rows. Rotation replaces trimming: you keep history, just not all in one tab.
Using the Apps Script editor a lot? NitroGAS drops free themes & snippets right into script.google.com — optional Co-Pilot when you want a boost.
Why rename instead of trimming or clearing?
- Trimming deletes history. Fine for noise, painful when someone asks “what happened in August?”
- Clearing loses everything at once and is easy to run by accident
- Renaming is instant (no data copied), keeps every row, and leaves the current month small and fast
What not to do
- Don’t copy rows into an archive tab; that’s slow for big logs and doubles the data for a while
- Don’t hard-code the new tab name in the logger; keep logging to
Logand let rotation handle the rest - Don’t rotate without a lock; a job logging mid-rotation could recreate
Logwith the wrong headers - Don’t keep rotated months forever in the same file; the workbook has a cell limit (10 million cells)
The rotation
var LOG_SHEET_NAME_ = 'Log';
var LOG_HEADERS_ = ['timestamp', 'level', 'message', 'context'];
function rotateLog() {
var lock = LockService.getScriptLock();
if (!lock.tryLock(30000)) {
console.warn('rotateLog: could not get lock; will try next run');
return;
}
try {
var ss = SpreadsheetApp.getActive();
var log = ss.getSheetByName(LOG_SHEET_NAME_);
if (!log || log.getLastRow() <= 1) return; // nothing to rotate
// Name it after the month of its OLDEST entry, so a late rotation still files correctly
var tz = ss.getSpreadsheetTimeZone();
var firstTs = log.getRange(2, 1).getValue();
var month = Utilities.formatDate(firstTs instanceof Date ? firstTs : new Date(), tz, 'yyyy-MM');
var archiveName = uniqueSheetName_(ss, LOG_SHEET_NAME_ + ' ' + month);
// 1. Rename the current log (instant: no rows copied)
log.setName(archiveName);
// 2. Fresh Log with the same headers, placed where the old one was
var fresh = ss.insertSheet(LOG_SHEET_NAME_, log.getIndex() - 1);
fresh.getRange(1, 1, 1, LOG_HEADERS_.length).setValues([LOG_HEADERS_]).setFontWeight('bold');
fresh.setFrozenRows(1);
// 3. Nudge the archive out of the way
ss.setActiveSheet(fresh);
log.hideSheet(); // optional: archived months stay available but out of sight
console.log('Rotated log → %s', archiveName);
} finally {
lock.releaseLock();
}
}
function uniqueSheetName_(ss, base) {
var name = base, n = 2;
while (ss.getSheetByName(name)) name = base + ' (' + n++ + ')';
return name;
}
Because the logger finds its tab with getSheetByName('Log'), it starts writing into the fresh tab on the very next call. If the logger ever creates Log itself (first run), make sure it uses the same LOG_HEADERS_ so rotated and fresh tabs match.
Have the logger take the same script lock when it writes (with a short tryLock). Then a log line can’t slip in between the rename and the insert, which is the moment Log doesn’t exist.
Monthly or by size?
Pick one, or both:
// Monthly: runs on the 1st around 1am (script timezone)
function installLogRotation() {
deleteTriggersByHandler('rotateLog');
deleteTriggersByHandler('rotateLogIfBig');
ScriptApp.newTrigger('rotateLog').timeBased().onMonthDay(1).atHour(1).create();
}
// Size-based: check daily, rotate only when the tab gets heavy
var LOG_ROTATE_AT_ROWS_ = 20000;
function rotateLogIfBig() {
var log = SpreadsheetApp.getActive().getSheetByName(LOG_SHEET_NAME_);
if (log && log.getLastRow() > LOG_ROTATE_AT_ROWS_) rotateLog();
}
Monthly is easiest to reason about (“August is in Log 2026-08”). Size-based suits bursty jobs. For those, the (2) suffix from uniqueSheetName_ handles two rotations in one month.
Clean up old months
Rotated tabs still count toward the workbook’s cell limit. Keep, say, six months in the file:
var LOG_KEEP_MONTHS_ = 6;
function dropOldLogMonths() {
var ss = SpreadsheetApp.getActive();
var tz = ss.getSpreadsheetTimeZone();
var now = new Date();
var cutoff = Utilities.formatDate(
new Date(now.getFullYear(), now.getMonth() - LOG_KEEP_MONTHS_, 1), tz, 'yyyy-MM');
ss.getSheets().forEach(function (sh) {
var m = sh.getName().match(/^Log (\d{4}-\d{2})/);
if (m && m[1] < cutoff) {
// Export first if you need the history: see the CSV-to-Drive snippets
ss.deleteSheet(sh);
}
});
}
Deleting tabs is permanent unless you restore a version, so export old months to CSV in Drive first if anyone might ask.
Failure modes worth naming
- Rotation while a job is logging: use the same script lock in the logger and in
rotateLog. - Someone renamed
Logby hand: the logger creates a newLogon the next write. Rotation simply finds the new one later. Harmless, just untidy. - Formulas or dashboards pointing at
Log: Sheets formulas follow the renamed tab (they’ll now readLog 2026-10). Point dashboards at the current month withINDIRECT("Log!A:D")if they should always show the live tab. - Very large single month: rotation doesn’t shrink it. Lower the log level, or rotate by size.
- Timezone edge: tab names use the sheet’s timezone, so the trigger’s run time (script timezone) doesn’t mislabel the month.
Soft Co-Pilot note
NitroGAS keeps logging and trigger helpers a search away in the editor. Paste your current logger and Co-Pilot can add the shared lock, rotateLog, and the monthly trigger without changing how your jobs call the logger. You still run one rotation by hand and check the headers. Free core; Co-Pilot is a pass you grab when you want the draft.
Closing checklist
- Logger writes to the tab named
Logwith fixed headers -
rotateLogrenames, inserts a freshLog, freezes headers, under a lock - Monthly (or size-based) trigger installed once
- Old months exported/dropped before the workbook gets heavy
- Dashboards point at the live tab deliberately
Happy Coding!


