Writes pod logs to separate files.

This commit is contained in:
benbierens 2023-03-20 15:42:42 +01:00
parent 0e2bd7f897
commit d0faf79f6c
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
2 changed files with 14 additions and 15 deletions

View File

@ -72,7 +72,7 @@ namespace CodexDistTests.TestCore
WaitUntilNamespaceDeleted(client); WaitUntilNamespaceDeleted(client);
} }
public void FetchAllPodsLogs(Action<string, Stream> onLog) public void FetchAllPodsLogs(Action<string, string, Stream> onLog)
{ {
var client = CreateClient(); var client = CreateClient();
foreach (var node in activeNodes.Values) foreach (var node in activeNodes.Values)
@ -81,7 +81,7 @@ namespace CodexDistTests.TestCore
foreach (var podName in node.ActivePodNames) foreach (var podName in node.ActivePodNames)
{ {
var stream = client.ReadNamespacedPodLog(podName, k8sNamespace); var stream = client.ReadNamespacedPodLog(podName, k8sNamespace);
onLog($"{nodeDescription}:{podName}", stream); onLog(node.SelectorName, $"{nodeDescription}:{podName}", stream);
} }
} }
} }

View File

@ -41,7 +41,6 @@ namespace CodexDistTests.TestCore
IncludeFullPodLogging(k8sManager); IncludeFullPodLogging(k8sManager);
} }
LogRaw("");
file = null; file = null;
} }
@ -52,26 +51,26 @@ namespace CodexDistTests.TestCore
return $"{className}.{test.MethodName}"; return $"{className}.{test.MethodName}";
} }
private static void LogRaw(string message) private static void LogRaw(string message, string filename)
{ {
file!.WriteRaw(message); file!.WriteRaw(message, filename);
} }
private static void IncludeFullPodLogging(K8sManager k8sManager) private static void IncludeFullPodLogging(K8sManager k8sManager)
{ {
LogRaw("Full pod logging:"); Log("Full pod logging:");
k8sManager.FetchAllPodsLogs(WritePodLog); k8sManager.FetchAllPodsLogs(WritePodLog);
} }
private static void WritePodLog(string nodeDescription, Stream stream) private static void WritePodLog(string id, string nodeDescription, Stream stream)
{ {
LogRaw("---"); Log($"{nodeDescription} -->> {id}");
LogRaw(nodeDescription); LogRaw(nodeDescription, id);
var reader = new StreamReader(stream); var reader = new StreamReader(stream);
var line = reader.ReadLine(); var line = reader.ReadLine();
while (line != null) while (line != null)
{ {
LogRaw(line); LogRaw(line, id);
line = reader.ReadLine(); line = reader.ReadLine();
} }
} }
@ -79,21 +78,21 @@ namespace CodexDistTests.TestCore
public class LogFile public class LogFile
{ {
private readonly string filepath;
private readonly string filename; private readonly string filename;
public LogFile(string name) public LogFile(string name)
{ {
var now = DateTime.UtcNow; var now = DateTime.UtcNow;
var filepath = Path.Join( filepath = Path.Join(
TestLog.LogRoot, TestLog.LogRoot,
$"{now.Year}-{Pad(now.Month)}", $"{now.Year}-{Pad(now.Month)}",
Pad(now.Day)); Pad(now.Day));
Directory.CreateDirectory(filepath); Directory.CreateDirectory(filepath);
filename = Path.Combine(filepath, filename = Path.Combine(filepath, $"{Pad(now.Hour)}-{Pad(now.Minute)}-{Pad(now.Second)}Z_{name.Replace('.', '-')}");
$"{Pad(now.Hour)}-{Pad(now.Minute)}-{Pad(now.Second)}Z_{name.Replace('.', '-')}.log");
} }
public void Write(string message) public void Write(string message)
@ -101,11 +100,11 @@ namespace CodexDistTests.TestCore
WriteRaw($"{GetTimestamp()} {message}"); WriteRaw($"{GetTimestamp()} {message}");
} }
public void WriteRaw(string message) public void WriteRaw(string message, string subfile = "")
{ {
try try
{ {
File.AppendAllLines(filename, new[] { message }); File.AppendAllLines(filename + subfile + ".log", new[] { message });
} }
catch (Exception ex) catch (Exception ex)
{ {