From 4ab0b2f8be9f2bc052683786c1080603e7772bf4 Mon Sep 17 00:00:00 2001
From: Bryan Jensen <bryanawjensen@gmail.com>
Date: Mon, 18 Mar 2024 15:20:56 -0700
Subject: [PATCH 1/2] Fix bug with namespace when logs command run before start

---
 lib/API/LogManagement.js       | 10 +---------
 test/e2e/logs/log-namespace.sh |  5 +++++
 2 files changed, 6 insertions(+), 9 deletions(-)

diff --git a/lib/API/LogManagement.js b/lib/API/LogManagement.js
index 55bae4356b..c1c959a8d2 100644
--- a/lib/API/LogManagement.js
+++ b/lib/API/LogManagement.js
@@ -161,7 +161,6 @@ module.exports = function(CLI) {
     // Get the list of all running apps
     that.Client.executeRemote('getMonitorData', {}, function(err, list) {
       var regexList = [];
-      var namespaceList = [];
 
       if (err) {
         Common.printError(err);
@@ -190,9 +189,6 @@ module.exports = function(CLI) {
               type     : 'err'
             });
         } else if(proc.pm2_env && proc.pm2_env.namespace == id) {
-          if(namespaceList.indexOf(proc.pm2_env.name) === -1) {
-            namespaceList.push(proc.pm2_env.name)
-          }
           if (proc.pm2_env.pm_out_log_path && exclusive !== 'err')
             pushIfUnique({
               path     : proc.pm2_env.pm_out_log_path,
@@ -251,12 +247,8 @@ module.exports = function(CLI) {
                 Log.stream(that.Client, id, raw, timestamp, exclusive, highlight);
             })
           }
-          else if(namespaceList.length > 0) {
-            namespaceList.forEach(function(id) {
-                Log.stream(that.Client, id, raw, timestamp, exclusive, highlight);
-            })
-          }
           else {
+            // Internally Log.stream handles `id` being a name, id, or namespace just fine
             Log.stream(that.Client, id, raw, timestamp, exclusive, highlight);
           }
         });
diff --git a/test/e2e/logs/log-namespace.sh b/test/e2e/logs/log-namespace.sh
index 9e2b20357d..ce74de2be5 100644
--- a/test/e2e/logs/log-namespace.sh
+++ b/test/e2e/logs/log-namespace.sh
@@ -10,6 +10,9 @@ LOG_PATH_PREFIX="${SRC}/__log-namespace__"
 rm -rf "${LOG_PATH_PREFIX}"
 mkdir "${LOG_PATH_PREFIX}"
 
+LOG_FILE_PREMATURE="${LOG_PATH_PREFIX}/premature-log-out.log"
+$pm2 logs e2e-test-log-namespace > $LOG_FILE_PREMATURE & # backgrounded - will be stopped by `$pm2 delete all`
+
 $pm2 start echo.js --namespace e2e-test-log-namespace
 
 LOG_FILE_BASELINE="${LOG_PATH_PREFIX}/baseline-out.log"
@@ -20,6 +23,8 @@ sleep 2 # should leave time for ~40 "tick" lines
 # Using -q to avoid spamming, since there will be a fair few "tick" matches
 grep -q "tick" ${LOG_FILE_BASELINE}
 spec "Should have 'tick' in the log file"
+grep -q "tick" ${LOG_FILE_PREMATURE}
+spec "Should have 'tick' in the log file even if 'log' called before 'start'"
 
 LOG_FILE_LINES_ZERO="${LOG_PATH_PREFIX}/lines-zero-out.log"
 $pm2 logs e2e-test-log-namespace --lines 0 > $LOG_FILE_LINES_ZERO &

From 2bb0b9590e49edd6a32dfb119cdbe80186033bb9 Mon Sep 17 00:00:00 2001
From: Bryan Jensen <bryanawjensen@gmail.com>
Date: Mon, 18 Mar 2024 16:43:15 -0700
Subject: [PATCH 2/2] pr update: add in ping to fix multiple daemons

---
 test/e2e/logs/log-namespace.sh | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/test/e2e/logs/log-namespace.sh b/test/e2e/logs/log-namespace.sh
index ce74de2be5..920ccfa6bd 100644
--- a/test/e2e/logs/log-namespace.sh
+++ b/test/e2e/logs/log-namespace.sh
@@ -10,6 +10,9 @@ LOG_PATH_PREFIX="${SRC}/__log-namespace__"
 rm -rf "${LOG_PATH_PREFIX}"
 mkdir "${LOG_PATH_PREFIX}"
 
+# The first operation that starts the daemon needs to be in the foreground, otherwise we risk starting multiple
+$pm2 ping
+
 LOG_FILE_PREMATURE="${LOG_PATH_PREFIX}/premature-log-out.log"
 $pm2 logs e2e-test-log-namespace > $LOG_FILE_PREMATURE & # backgrounded - will be stopped by `$pm2 delete all`