fix(logs): apply log settings from the first line and name the locking variable
The desktop opened its log files with the default limits and wrote its first line, which prunes, before Settings › Advanced › Logs were applied. Every start, and every launch that only hands its file to the running Chronos, deleted logs older than 14 days and cut the folder to 1 GB whatever the user kept. AppLogging.Open now builds the sink from the stored settings before that line; the settings are read again once the launch owns the data folder. A total below one file deleted each finished file as the next one opened. The rule now lives in LogFileOptions.TotalBelowFile: the server keeps refusing it, the desktop raises the total to the file size and its rows no longer offer the combination. The desktop log rows threw on negative sizes from an imported or hand-edited file and showed raw values the files never got; they now show the applied ones. Upgraded Unraid and compose installs keep CHRONOS_LOG_LEVEL=Information, which locks both levels. The lock stays; the server now reports which variable fixes each locked setting (LockedBy), so the panel's tooltip names it exactly and says to remove it to change the value there. The releases README tells upgraders. The former-constructor log test no longer fails across midnight UTC. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 8ba7162bd5926d34c5ec4e40e2a2dad063ee19c3)
This commit is contained in:
1 parent
8d66be709c
commit
ac121acf7c
53 files changed
+254
-92
No files matched your search
@@ -1,4 +1,5 @@
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using Chronos.App.Infrastructure;
|
||||
using Chronos.App.Settings;
|
||||
using Chronos.Core.Diagnostics;
|
||||
@@ -58,6 +59,18 @@ public sealed class LogSettingsTests
|
||||
var odd = new AppSettings { LogLevel = (LogLevel)42, LogRetentionDays = 0, LogMaxFileMb = -3, LogMaxTotalMb = -1, LogRotation = (LogRotation)7, DiagnosticsLogMb = 0 };
|
||||
Assert.Equal(new LogFileOptions { RetentionDays = 1, MaxFileMb = 0, MaxTotalMb = 0 }, AppLogging.Options(odd));
|
||||
Assert.Equal(1L << 20, AppLogging.DiagnosticsTailBytes(odd));
|
||||
var huge = new AppSettings { LogRetentionDays = 99_999, LogMaxFileMb = 99_999, LogMaxTotalMb = int.MaxValue, DiagnosticsLogMb = 5000 };
|
||||
Assert.Equal(new LogFileOptions { RetentionDays = 3650, MaxFileMb = 10_240, MaxTotalMb = 1_048_576 }, AppLogging.Options(huge));
|
||||
Assert.Equal(1024L << 20, AppLogging.DiagnosticsTailBytes(huge));
|
||||
|
||||
// A total below one file would delete each finished file as the next one opens: it is raised to the file (the
|
||||
// server refuses it). Without a file limit any total holds.
|
||||
Assert.True(LogFileOptions.TotalBelowFile(500, 256));
|
||||
Assert.False(LogFileOptions.TotalBelowFile(256, 256) || LogFileOptions.TotalBelowFile(500, 0) || LogFileOptions.TotalBelowFile(0, 256));
|
||||
var below = AppLogging.Options(new AppSettings { LogMaxFileMb = 500, LogMaxTotalMb = 256 });
|
||||
Assert.Equal((500, 500), (below.MaxFileMb, below.MaxTotalMb));
|
||||
var free = AppLogging.Options(new AppSettings { LogMaxFileMb = 0, LogMaxTotalMb = 256 });
|
||||
Assert.Equal((0, 256), (free.MaxFileMb, free.MaxTotalMb));
|
||||
|
||||
var level = Log.MinimumLevel;
|
||||
using var sink = new FileLogSink(_context.CreateTempDirectory(), "chronos");
|
||||
@@ -73,6 +86,33 @@ public sealed class LogSettingsTests
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TheLogFilesStartWithTheSettings()
|
||||
{
|
||||
// App opens its files with the settings: the first line opens a file and prunes, at every start and at every launch
|
||||
// that only hands over to the running Chronos, so the defaults would delete what the user keeps longer.
|
||||
var level = Log.MinimumLevel;
|
||||
try
|
||||
{
|
||||
foreach (var (settings, kept) in new[] { (new AppSettings { LogLevel = LogLevel.Debug, LogRetentionDays = 365, LogMaxTotalMb = 0 }, true), (new AppSettings(), false) })
|
||||
{
|
||||
var dir = _context.CreateTempDirectory();
|
||||
var old = Path.Combine(dir, $"chronos-{DateTime.UtcNow.AddDays(-30):yyyyMMdd}.log");
|
||||
File.WriteAllText(old, "a month ago");
|
||||
File.SetLastWriteTimeUtc(old, DateTime.UtcNow.AddDays(-30));
|
||||
using var sink = AppLogging.Open(dir, settings);
|
||||
Assert.Equal((AppLogging.Options(settings), settings.LogLevel), (sink.Options, Log.MinimumLevel));
|
||||
sink.Write(LogLevel.Info, "app", "Chronos starting", null);
|
||||
Assert.Equal(kept, File.Exists(old), $"retention {settings.LogRetentionDays} days");
|
||||
Assert.FileExists(sink.CurrentPath);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
Log.MinimumLevel = level;
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EachRowStoresAndSavesItsChoice()
|
||||
{
|
||||
@@ -100,7 +140,9 @@ public sealed class LogSettingsTests
|
||||
Assert.Equal((LogLevel.Debug, 90, 0, 5120, LogRotation.Hourly, 16),
|
||||
(settings.LogLevel, settings.LogRetentionDays, settings.LogMaxFileMb, settings.LogMaxTotalMb, settings.LogRotation, settings.DiagnosticsLogMb));
|
||||
Assert.Equal(6, saves);
|
||||
Assert.SequenceEqual(["SelectedLevel", "SelectedRetention", "SelectedFileSize", "SelectedTotalSize", "SelectedRotation", "SelectedDiagnostics"], changed);
|
||||
// A size changes the choices of the other.
|
||||
Assert.SequenceEqual(["SelectedLevel", "SelectedRetention", "SelectedFileSize", "TotalSizeChoices", "SelectedTotalSize", "SelectedTotalSize",
|
||||
"FileSizeChoices", "SelectedFileSize", "SelectedRotation", "SelectedDiagnostics"], changed);
|
||||
|
||||
// The same choice again, or none (a list being rebuilt), changes nothing.
|
||||
rows.SelectedLevel = rows.Levels.Single(l => l.Value == LogLevel.Debug);
|
||||
@@ -122,4 +164,58 @@ public sealed class LogSettingsTests
|
||||
Assert.Equal(string.Empty, changed.Single());
|
||||
Assert.Equal(6, saves);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TheSizeRowsNeverOfferATotalBelowOneFile()
|
||||
{
|
||||
var settings = new AppSettings { LogMaxFileMb = 500 };
|
||||
var saves = 0;
|
||||
var rows = new LogSettingsRows(settings, () => saves++);
|
||||
Assert.SequenceEqual([512, 1024, 2048, 5120, 10240, 0], rows.TotalSizeChoices.Select(c => c.Value));
|
||||
Assert.SequenceEqual([10, 20, 50, 100, 200, 500, 0], rows.FileSizeChoices.Select(c => c.Value));
|
||||
|
||||
rows.SelectedTotalSize = rows.TotalSizeChoices.Single(c => c.Value == 512);
|
||||
rows.SelectedFileSize = rows.FileSizeChoices.Single(c => c.Value == 100);
|
||||
rows.SelectedTotalSize = rows.TotalSizeChoices.Single(c => c.Value == 256);
|
||||
Assert.Equal((100, 256, 3), (settings.LogMaxFileMb, settings.LogMaxTotalMb, saves));
|
||||
Assert.SequenceEqual([10, 20, 50, 100, 200, 0], rows.FileSizeChoices.Select(c => c.Value));
|
||||
// Without a file limit every total is offered, and without a total every file size.
|
||||
rows.SelectedFileSize = rows.FileSizeChoices[^1];
|
||||
Assert.SequenceEqual([256, 512, 1024, 2048, 5120, 10240, 0], rows.TotalSizeChoices.Select(c => c.Value));
|
||||
rows.SelectedTotalSize = rows.TotalSizeChoices[^1];
|
||||
Assert.SequenceEqual([10, 20, 50, 100, 200, 500, 0], rows.FileSizeChoices.Select(c => c.Value));
|
||||
|
||||
// An imported file with a total below its file size shows the total the files get.
|
||||
settings.LogMaxFileMb = 500;
|
||||
settings.LogMaxTotalMb = 256;
|
||||
Assert.Equal(("500 MB", "500 MB"), (rows.SelectedFileSize.Label, rows.SelectedTotalSize.Label));
|
||||
Assert.SequenceEqual([500, 512, 1024, 2048, 5120, 10240, 0], rows.TotalSizeChoices.Select(c => c.Value));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void OutOfRangeValuesShowWhatTheFilesGet()
|
||||
{
|
||||
// A hand-edited or imported settings file: every row shows the value applied, and none fails to find its choice.
|
||||
var settings = new AppSettings { LogLevel = (LogLevel)42, LogRetentionDays = 0, LogMaxFileMb = -3, LogMaxTotalMb = -1, LogRotation = (LogRotation)7, DiagnosticsLogMb = 0 };
|
||||
var saves = 0;
|
||||
var rows = new LogSettingsRows(settings, () => saves++);
|
||||
Assert.Equal(("Information", "1 day", "No limit", "No limit", "Daily", "1 MB"), (rows.SelectedLevel.Label, rows.SelectedRetention.Label,
|
||||
rows.SelectedFileSize.Label, rows.SelectedTotalSize.Label, rows.SelectedRotation.Label, rows.SelectedDiagnostics.Label));
|
||||
Assert.SequenceEqual(LogSettingsRows.RetentionPresets, rows.RetentionChoices.Select(c => c.Value));
|
||||
Assert.SequenceEqual([.. LogSettingsRows.FileSizePresets, 0], rows.FileSizeChoices.Select(c => c.Value));
|
||||
Assert.SequenceEqual(LogSettingsRows.DiagnosticsPresets, rows.DiagnosticsChoices.Select(c => c.Value));
|
||||
|
||||
// Choosing the value shown saves it as applied.
|
||||
var (shownFile, shownRetention) = (rows.SelectedFileSize, rows.SelectedRetention);
|
||||
rows.SelectedFileSize = shownFile;
|
||||
rows.SelectedRetention = shownRetention;
|
||||
Assert.Equal((0, 1, 2), (settings.LogMaxFileMb, settings.LogRetentionDays, saves));
|
||||
|
||||
settings.LogRetentionDays = 99_999;
|
||||
settings.LogMaxFileMb = 99_999;
|
||||
settings.LogMaxTotalMb = int.MaxValue;
|
||||
settings.DiagnosticsLogMb = 5000;
|
||||
Assert.Equal(("3650 days", "10,240 MB", "1,048,576 MB", "1,024 MB"),
|
||||
(rows.SelectedRetention.Label, rows.SelectedFileSize.Label, rows.SelectedTotalSize.Label, rows.SelectedDiagnostics.Label));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user