use phosphor-logging

Migrate all instances of std::cerr and std::cout to phosphor-logging
such as lg2::error, lg2::info, lg2::debug, ...

The use of modern logging infrastructure helps with development since
additional logging levels such as `debug` and `warning` are available.

Migrating the remaining logging calls helps to make the code more
readable and uniform.

Tested: Inspection, and on Tyan S8030

Restarted EM with patch applied.
Logs appear as usual.

```
Sep 15 13:52:46 s8030-bmc-30303035c0c1 entity-manager[23480]: Inventory Added: Supermicro PWS 920P SQ 0
Sep 15 13:52:46 s8030-bmc-30303035c0c1 entity-manager[23480]: Inventory Added: Supermicro PWS 920P SQ 1
Sep 15 13:52:46 s8030-bmc-30303035c0c1 entity-manager[23480]: Inventory Added: Tyan S8030 Baseboard
Sep 15 13:52:46 s8030-bmc-30303035c0c1 entity-manager[23480]: Inventory Added: MBX 1.57 Chassis
```

`busctl tree` output appears as before.

Did a power cycle to trigger a few more log prints

```
Sep 15 13:55:14 s8030-bmc-30303035c0c1 entity-manager[23480]: power match triggered
```

Created configuration file with missing fields to trigger error print.
```
{
  "Exposes": [],
  "Type": "error"
}
```

```
Sep 15 13:56:58 s8030-bmc-30303035c0c1 entity-manager[23659]: Finished loading json configuration in 7938ms
Sep 15 13:56:58 s8030-bmc-30303035c0c1 entity-manager[23659]: configuration file missing probe:
                                                               {"Exposes":[],"Type":"error"}
Sep 15 13:56:59 s8030-bmc-30303035c0c1 entity-manager[23659]: configuration file missing probe:
                                                               {"Exposes":[],"Type":"error"}
```

Change-Id: I3452f983c9c14cd02ab9b56451c4b3e4a13c3979
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/src/entity_manager/configuration.cpp b/src/entity_manager/configuration.cpp
index 1e4ceac..b6ece2b 100644
--- a/src/entity_manager/configuration.cpp
+++ b/src/entity_manager/configuration.cpp
@@ -1,10 +1,10 @@
 #include "configuration.hpp"
 
 #include "perform_probe.hpp"
-#include "phosphor-logging/lg2.hpp"
 #include "utils.hpp"
 
 #include <nlohmann/json.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <valijson/adapters/nlohmann_json_adapter.hpp>
 #include <valijson/schema.hpp>
 #include <valijson/schema_parser.hpp>
@@ -12,7 +12,6 @@
 
 #include <filesystem>
 #include <fstream>
-#include <iostream>
 #include <string>
 #include <vector>
 
@@ -33,8 +32,8 @@
                                                hostConfigurationDirectory},
             R"(.*\.json)", jsonPaths))
     {
-        std::cerr << "Unable to find any configuration files in "
-                  << configurationDirectory << "\n";
+        lg2::error("Unable to find any configuration files in {DIR}", "DIR",
+                   configurationDirectory);
         return;
     }
 
@@ -42,8 +41,7 @@
         std::string(schemaDirectory) + "/" + globalSchema);
     if (!schemaStream.good())
     {
-        std::cerr
-            << "Cannot open schema file,  cannot validate JSON, exiting\n\n";
+        lg2::error("Cannot open schema file,  cannot validate JSON, exiting");
         std::exit(EXIT_FAILURE);
         return;
     }
@@ -51,8 +49,8 @@
         nlohmann::json::parse(schemaStream, nullptr, false, true);
     if (schema.is_discarded())
     {
-        std::cerr
-            << "Illegal schema file detected, cannot validate JSON, exiting\n";
+        lg2::error(
+            "Illegal schema file detected, cannot validate JSON, exiting");
         std::exit(EXIT_FAILURE);
         return;
     }
@@ -62,19 +60,19 @@
         std::ifstream jsonStream(jsonPath.c_str());
         if (!jsonStream.good())
         {
-            std::cerr << "unable to open " << jsonPath.string() << "\n";
+            lg2::error("unable to open {PATH}", "PATH", jsonPath.string());
             continue;
         }
         auto data = nlohmann::json::parse(jsonStream, nullptr, false, true);
         if (data.is_discarded())
         {
-            std::cerr << "syntax error in " << jsonPath.string() << "\n";
+            lg2::error("syntax error in {PATH}", "PATH", jsonPath.string());
             continue;
         }
 
         if (ENABLE_RUNTIME_VALIDATE_JSON && !validateJson(schema, data))
         {
-            std::cerr << "Error validating " << jsonPath.string() << "\n";
+            lg2::error("Error validating {PATH}", "PATH", jsonPath.string());
             continue;
         }
 
@@ -137,7 +135,8 @@
         auto findProbe = it->find("Probe");
         if (findProbe == it->end())
         {
-            std::cerr << "configuration file missing probe:\n " << *it << "\n";
+            lg2::error("configuration file missing probe: {PROBE}", "PROBE",
+                       *it);
             it++;
             continue;
         }
@@ -158,7 +157,7 @@
             const std::string* probe = probeJson.get_ptr<const std::string*>();
             if (probe == nullptr)
             {
-                std::cerr << "Probe statement wasn't a string, can't parse";
+                lg2::error("Probe statement wasn't a string, can't parse");
                 continue;
             }
             // Skip it if the probe cmd doesn't contain an interface.
diff --git a/src/entity_manager/dbus_interface.cpp b/src/entity_manager/dbus_interface.cpp
index 1492314..461b27c 100644
--- a/src/entity_manager/dbus_interface.cpp
+++ b/src/entity_manager/dbus_interface.cpp
@@ -5,6 +5,7 @@
 
 #include <boost/algorithm/string/case_conv.hpp>
 #include <boost/container/flat_map.hpp>
+#include <phosphor-logging/lg2.hpp>
 
 #include <fstream>
 #include <regex>
@@ -35,10 +36,10 @@
     }
     catch (std::exception& e)
     {
-        std::cerr << "Unable to initialize dbus interface : " << e.what()
-                  << "\n"
-                  << "object Path : " << iface->get_object_path() << "\n"
-                  << "interface name : " << iface->get_interface_name() << "\n";
+        lg2::error(
+            "Unable to initialize dbus interface : {ERR} object Path : {PATH} interface name : {INTF}",
+            "ERR", e, "PATH", iface->get_object_path(), "INTF",
+            iface->get_interface_name());
     }
 }
 
@@ -95,7 +96,7 @@
 
             if (!writeJsonFiles(systemConfiguration))
             {
-                std::cerr << "error setting json file\n";
+                lg2::error("error setting json file");
                 throw DBusInternalError();
             }
         });
@@ -190,8 +191,9 @@
         }
         default:
         {
-            std::cerr << "Unexpected json type in system configuration " << key
-                      << ": " << value.type_name() << "\n";
+            lg2::error(
+                "Unexpected json type in system configuration {KEY}: {VALUE}",
+                "KEY", key, "VALUE", value.type_name());
             break;
         }
     }
@@ -215,7 +217,7 @@
             type = value[0].type();
             if (!checkArrayElementsSameType(value))
             {
-                std::cerr << "dbus format error" << value << "\n";
+                lg2::error("dbus format error {VALUE}", "VALUE", value);
                 continue;
             }
         }
@@ -326,7 +328,7 @@
                     nlohmann::json::parse(schemaFile, nullptr, false, true);
                 if (schema.is_discarded())
                 {
-                    std::cerr << "Schema not legal" << *type << ".json\n";
+                    lg2::error("Schema not legal: {TYPE}.json", "TYPE", *type);
                     throw DBusInternalError();
                 }
 
@@ -346,7 +348,7 @@
             }
             if (!writeJsonFiles(systemConfiguration))
             {
-                std::cerr << "Error writing json files\n";
+                lg2::error("Error writing json files");
             }
             std::string dbusName = *name;
 
diff --git a/src/entity_manager/dbus_interface.hpp b/src/entity_manager/dbus_interface.hpp
index 9bf223f..9c20d8f 100644
--- a/src/entity_manager/dbus_interface.hpp
+++ b/src/entity_manager/dbus_interface.hpp
@@ -4,10 +4,10 @@
 
 #include <boost/container/flat_map.hpp>
 #include <nlohmann/json.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/asio/connection.hpp>
 #include <sdbusplus/asio/object_server.hpp>
 
-#include <iostream>
 #include <vector>
 
 namespace dbus_interface
@@ -89,12 +89,12 @@
                 if (!setJsonFromPointer(jsonPointerString, val,
                                         systemConfiguration))
                 {
-                    std::cerr << "error setting json field\n";
+                    lg2::error("error setting json field");
                     return -1;
                 }
                 if (!writeJsonFiles(systemConfiguration))
                 {
-                    std::cerr << "error setting json file\n";
+                    lg2::error("error setting json file");
                     return -1;
                 }
                 return 1;
@@ -123,12 +123,12 @@
             if (!setJsonFromPointer(jsonPointerString, val,
                                     systemConfiguration))
             {
-                std::cerr << "error setting json field\n";
+                lg2::error("error setting json field");
                 return -1;
             }
             if (!writeJsonFiles(systemConfiguration))
             {
-                std::cerr << "error setting json file\n";
+                lg2::error("error setting json file");
                 return -1;
             }
             return 1;
diff --git a/src/entity_manager/entity_manager.cpp b/src/entity_manager/entity_manager.cpp
index 22f9a6a..ef4212a 100644
--- a/src/entity_manager/entity_manager.cpp
+++ b/src/entity_manager/entity_manager.cpp
@@ -10,7 +10,6 @@
 #include "log_device_inventory.hpp"
 #include "overlay.hpp"
 #include "perform_scan.hpp"
-#include "phosphor-logging/lg2.hpp"
 #include "topology.hpp"
 #include "utils.hpp"
 
@@ -25,13 +24,13 @@
 #include <boost/container/flat_set.hpp>
 #include <boost/range/iterator_range.hpp>
 #include <nlohmann/json.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/asio/connection.hpp>
 #include <sdbusplus/asio/object_server.hpp>
 
 #include <filesystem>
 #include <fstream>
 #include <functional>
-#include <iostream>
 #include <map>
 #include <regex>
 constexpr const char* tempConfigDir = "/tmp/configuration/";
@@ -126,8 +125,8 @@
     }
     else
     {
-        std::cerr << "Unable to find type for " << boardName
-                  << " reverting to Chassis.\n";
+        lg2::error("Unable to find type for {BOARD} reverting to Chassis.",
+                   "BOARD", boardName);
         boardType = "Chassis";
     }
 
@@ -198,7 +197,7 @@
     auto findName = item.find("Name");
     if (findName == item.end())
     {
-        std::cerr << "cannot find name in field " << item << "\n";
+        lg2::error("cannot find name in field {ITEM}", "ITEM", item);
         return;
     }
     auto findStatus = item.find("Status");
@@ -318,7 +317,7 @@
         }
         if (!isLegal)
         {
-            std::cerr << "dbus format error" << config << "\n";
+            lg2::error("dbus format error {JSON}", "JSON", config);
             return false;
         }
 
@@ -458,7 +457,7 @@
     boost::asio::post(io, [this]() {
         if (!writeJsonFiles(systemConfiguration))
         {
-            std::cerr << "Error writing json files\n";
+            lg2::error("Error writing json files");
         }
     });
 
@@ -489,7 +488,7 @@
             }
             if (ec)
             {
-                std::cerr << "async wait error " << ec << "\n";
+                lg2::error("async wait error {ERR}", "ERR", ec.message());
                 return;
             }
 
@@ -582,8 +581,8 @@
                 auto data = nlohmann::json::parse(jsonStream, nullptr, false);
                 if (data.is_discarded())
                 {
-                    std::cerr
-                        << "syntax error in " << lastConfiguration << "\n";
+                    lg2::error("syntax error in {PATH}", "PATH",
+                               lastConfiguration);
                 }
                 else
                 {
@@ -592,7 +591,7 @@
             }
             else
             {
-                std::cerr << "unable to open " << lastConfiguration << "\n";
+                lg2::error("unable to open {PATH}", "PATH", lastConfiguration);
             }
         }
     }
@@ -600,7 +599,7 @@
     {
         // not an error, just logging at this level to make it in the journal
         std::error_code ec;
-        std::cerr << "Clearing previous configuration\n";
+        lg2::error("Clearing previous configuration");
         std::filesystem::remove(currentConfiguration, ec);
     }
 }
diff --git a/src/entity_manager/expression.cpp b/src/entity_manager/expression.cpp
index 9b878c8..70c01ad 100644
--- a/src/entity_manager/expression.cpp
+++ b/src/entity_manager/expression.cpp
@@ -3,7 +3,8 @@
 
 #include "expression.hpp"
 
-#include <iostream>
+#include <phosphor-logging/lg2.hpp>
+
 #include <stdexcept>
 
 namespace expression
@@ -99,8 +100,8 @@
             }
             catch (const std::invalid_argument&)
             {
-                std::cerr << "Parameter not supported for templates " << *curr
-                          << "\n";
+                lg2::error("Parameter not supported for templates {STR}", "STR",
+                           *curr);
                 continue;
             }
         }
diff --git a/src/entity_manager/overlay.cpp b/src/entity_manager/overlay.cpp
index 635a5b8..534c68f 100644
--- a/src/entity_manager/overlay.cpp
+++ b/src/entity_manager/overlay.cpp
@@ -17,7 +17,6 @@
 #include <filesystem>
 #include <fstream>
 #include <iomanip>
-#include <iostream>
 #include <regex>
 #include <string>
 
@@ -81,8 +80,8 @@
             devDir / ("channel-" + std::to_string(channelIndex));
         if (!is_symlink(channelPath))
         {
-            std::cerr << channelPath << " for mux channel " << channelName
-                      << " doesn't exist!\n";
+            lg2::error("{PATH} for mux channel {CHANNEL} doesn't exist!",
+                       "PATH", channelPath.string(), "CHANNEL", channelName);
             continue;
         }
         std::filesystem::path bus = std::filesystem::read_symlink(channelPath);
@@ -93,8 +92,8 @@
         std::filesystem::create_symlink(fp, link, ec);
         if (ec)
         {
-            std::cerr << "Failure creating symlink for " << fp << " to " << link
-                      << "\n";
+            lg2::error("Failure creating symlink for {PATH} to {LINK}", "PATH",
+                       fp.string(), "LINK", link.string());
         }
     }
 }
@@ -107,7 +106,7 @@
     std::ofstream deviceFile(deviceDestructor);
     if (!deviceFile.good())
     {
-        std::cerr << "Error writing " << deviceDestructor << "\n";
+        lg2::error("Error writing {PATH}", "PATH", deviceDestructor.string());
         return -1;
     }
     deviceFile << std::to_string(address);
@@ -124,7 +123,7 @@
     std::ofstream deviceFile(deviceConstructor);
     if (!deviceFile.good())
     {
-        std::cerr << "Error writing " << deviceConstructor << "\n";
+        lg2::error("Error writing {PATH}", "PATH", deviceConstructor.string());
         return -1;
     }
     deviceFile << parameters;
@@ -183,7 +182,7 @@
                  &io](const boost::system::error_code& ec) mutable {
                     if (ec)
                     {
-                        std::cerr << "Timer error: " << ec << "\n";
+                        lg2::error("Timer error: {ERR}", "ERR", ec.message());
                         return -2;
                     }
                     return buildDevice(name, busPath, parameters, bus, address,
diff --git a/src/entity_manager/perform_probe.cpp b/src/entity_manager/perform_probe.cpp
index 50943c7..f398860 100644
--- a/src/entity_manager/perform_probe.cpp
+++ b/src/entity_manager/perform_probe.cpp
@@ -8,7 +8,6 @@
 #include <boost/algorithm/string/replace.hpp>
 #include <phosphor-logging/lg2.hpp>
 
-#include <iostream>
 #include <regex>
 #include <utility>
 
@@ -110,8 +109,8 @@
                 {
                     if (!std::regex_search(probe, match, command))
                     {
-                        std::cerr
-                            << "found probe syntax error " << probe << "\n";
+                        lg2::error("found probe syntax error {JSON}", "JSON",
+                                   probe);
                         return false;
                     }
                     std::string commandStr = *(match.begin() + 1);
@@ -132,7 +131,7 @@
         {
             if (!std::regex_search(probe, match, command))
             {
-                std::cerr << "dbus probe syntax error " << probe << "\n";
+                lg2::error("dbus probe syntax error {JSON}", "JSON", probe);
                 return false;
             }
             std::string commandStr = *(match.begin() + 1);
@@ -142,7 +141,8 @@
             auto json = nlohmann::json::parse(commandStr, nullptr, false, true);
             if (json.is_discarded())
             {
-                std::cerr << "dbus command syntax error " << commandStr << "\n";
+                lg2::error("dbus command syntax error {STR}", "STR",
+                           commandStr);
                 return false;
             }
             // we can match any (string, variant) property. (string, string)
diff --git a/src/entity_manager/perform_scan.cpp b/src/entity_manager/perform_scan.cpp
index c71fa48..4a3696f 100644
--- a/src/entity_manager/perform_scan.cpp
+++ b/src/entity_manager/perform_scan.cpp
@@ -12,7 +12,6 @@
 #include <phosphor-logging/lg2.hpp>
 
 #include <charconv>
-#include <iostream>
 
 using GetSubTreeType = std::vector<
     std::pair<std::string,
@@ -35,8 +34,9 @@
 {
     if (retries == 0U)
     {
-        std::cerr << "retries exhausted on " << instance.busName << " "
-                  << instance.path << " " << instance.interface << "\n";
+        lg2::error("retries exhausted on {BUSNAME} {PATH} {INTF}", "BUSNAME",
+                   instance.busName, "PATH", instance.path, "INTF",
+                   instance.interface);
         return;
     }
 
@@ -45,9 +45,9 @@
          &io](boost::system::error_code& errc, const DBusInterface& resp) {
             if (errc)
             {
-                std::cerr << "error calling getall on  " << instance.busName
-                          << " " << instance.path << " "
-                          << instance.interface << "\n";
+                lg2::error("error calling getall on {BUSNAME} {PATH} {INTF}",
+                           "BUSNAME", instance.busName, "PATH", instance.path,
+                           "INTF", instance.interface);
 
                 auto timer = std::make_shared<boost::asio::steady_timer>(io);
                 timer->expires_after(std::chrono::seconds(2));
@@ -125,7 +125,7 @@
                 {
                     return; // wasn't found by mapper
                 }
-                std::cerr << "Error communicating to mapper.\n";
+                lg2::error("Error communicating to mapper.");
 
                 if (retries == 0U)
                 {
@@ -220,7 +220,7 @@
     auto nameIt = record.find("Name");
     if (nameIt == record.end())
     {
-        std::cerr << "Last JSON Illegal\n";
+        lg2::error("Last JSON Illegal");
         return;
     }
 
@@ -258,7 +258,7 @@
         {
             if (!value.is_string())
             {
-                std::cerr << "Value is invalid type " << value << "\n";
+                lg2::error("Value is invalid type {VALUE}", "VALUE", value);
                 break;
             }
             matches.emplace_back(value);
@@ -267,7 +267,7 @@
         return true;
     }
 
-    std::cerr << "Value is invalid type " << keyPair.key() << "\n";
+    lg2::error("Value is invalid type {KEY}", "KEY", keyPair.key());
 
     return false;
 }
@@ -369,8 +369,9 @@
 
     if (!matches.empty())
     {
-        std::cerr << "configuration file dependency error, could not find "
-                  << keyPair.key() << " " << keyPair.value() << "\n";
+        lg2::error(
+            "configuration file dependency error, could not find {KEY} {VALUE}",
+            "KEY", keyPair.key(), "VALUE", keyPair.value());
     }
 }
 
@@ -398,9 +399,9 @@
 
     if (replaceStr)
     {
-        std::cerr << "Duplicates found, replacing " << *replaceStr
-                  << " with found device index.\n Consider "
-                     "fixing template to not have duplicates\n";
+        lg2::error(
+            "Duplicates found, replacing {STR} with found device index. Consider fixing template to not have duplicates",
+            "STR", *replaceStr);
     }
 
     return copyIt.value();
@@ -473,7 +474,7 @@
         auto getName = record.find("Name");
         if (getName == record.end())
         {
-            std::cerr << "Record Missing Name! " << record.dump();
+            lg2::error("Record Missing Name! {JSON}", "JSON", record.dump());
             continue; // this should be impossible at this level
         }
 
@@ -532,7 +533,8 @@
         auto findProbe = it->find("Probe");
         if (findProbe == it->end())
         {
-            std::cerr << "configuration file missing probe:\n " << *it << "\n";
+            lg2::error("configuration file missing probe:\n {JSON}", "JSON",
+                       *it);
             it = _configurations.erase(it);
             continue;
         }
@@ -540,7 +542,8 @@
         auto findName = it->find("Name");
         if (findName == it->end())
         {
-            std::cerr << "configuration file missing name:\n " << *it << "\n";
+            lg2::error("configuration file missing name:\n {JSON}", "JSON",
+                       *it);
             it = _configurations.erase(it);
             continue;
         }
@@ -578,7 +581,7 @@
             const std::string* probe = probeJson.get_ptr<const std::string*>();
             if (probe == nullptr)
             {
-                std::cerr << "Probe statement wasn't a string, can't parse";
+                lg2::error("Probe statement wasn't a string, can't parse");
                 continue;
             }
             if (probe::findProbeType(*probe))
diff --git a/src/entity_manager/utils.cpp b/src/entity_manager/utils.cpp
index 28e6d11..14b4f98 100644
--- a/src/entity_manager/utils.cpp
+++ b/src/entity_manager/utils.cpp
@@ -14,7 +14,6 @@
 #include <sdbusplus/bus/match.hpp>
 
 #include <fstream>
-#include <iostream>
 #include <regex>
 
 const std::regex illegalDbusMemberRegex("[^A-Za-z0-9_]");
@@ -225,13 +224,13 @@
         // need at least 1 operation and number
         if (split.size() < 2)
         {
-            std::cerr << "Syntax error on template replacement of " << *strPtr
-                      << "\n";
+            lg2::error("Syntax error on template replacement of {STR}", "STR",
+                       *strPtr);
             for (const std::string& data : split)
             {
-                std::cerr << data << " ";
+                lg2::error("{SPLIT} ", "SPLIT", data);
             }
-            std::cerr << "\n";
+            lg2::error("");
             continue;
         }
 
diff --git a/src/fru_device/fru_device.cpp b/src/fru_device/fru_device.cpp
index 2678f90..1b2de93 100644
--- a/src/fru_device/fru_device.cpp
+++ b/src/fru_device/fru_device.cpp
@@ -26,7 +26,6 @@
 #include <functional>
 #include <future>
 #include <iomanip>
-#include <iostream>
 #include <limits>
 #include <map>
 #include <optional>
@@ -112,7 +111,7 @@
     auto result = lseek(fd, offset, SEEK_SET);
     if (result < 0)
     {
-        std::cerr << "failed to seek\n";
+        lg2::error("failed to seek");
         return -1;
     }
 
@@ -351,7 +350,7 @@
     int file = open(path.c_str(), O_RDONLY);
     if (file < 0)
     {
-        std::cerr << "Unable to open eeprom file: " << path << "\n";
+        lg2::error("Unable to open eeprom file: {PATH}", "PATH", path);
         return {};
     }
 
@@ -383,7 +382,7 @@
     {
         if (ec)
         {
-            std::cerr << "directory_iterator err " << ec.message() << "\n";
+            lg2::error("directory_iterator err {ERR}", "ERR", ec.message());
             break;
         }
         const std::string node = p.path().string();
@@ -397,7 +396,7 @@
         }
         if (m.size() != 2)
         {
-            std::cerr << "regex didn't capture\n";
+            lg2::error("regex didn't capture");
             continue;
         }
 
@@ -510,8 +509,8 @@
             // Set target address
             if (ioctl(file, I2C_SLAVE, ii) < 0)
             {
-                std::cerr << "device at bus " << bus << " address " << ii
-                          << " busy\n";
+                lg2::error("device at bus {BUS} address {ADDR} busy", "BUS",
+                           bus, "ADDR", ii);
                 continue;
             }
             // probe
@@ -543,8 +542,8 @@
             std::optional<bool> is16Bit = isDevice16Bit(file, ii);
             if (!is16Bit.has_value())
             {
-                std::cerr << "failed to read bus " << bus << " address " << ii
-                          << "\n";
+                lg2::error("failed to read bus {BUS} address {ADDR}", "BUS",
+                           bus, "ADDR", ii);
                 if (powerIsOn)
                 {
                     failedItems.insert(ii);
@@ -594,7 +593,7 @@
         future.wait_for(std::chrono::seconds(busTimeoutSeconds));
     if (status == std::future_status::timeout)
     {
-        std::cerr << "Error reading bus " << bus << "\n";
+        lg2::error("Error reading bus {BUS}", "BUS", bus);
         if (powerIsOn)
         {
             busBlocklist[bus] = std::nullopt;
@@ -613,7 +612,7 @@
     if (!blocklistStream.good())
     {
         // File is optional.
-        std::cerr << "Cannot open blocklist file.\n\n";
+        lg2::error("Cannot open blocklist file.\n");
         return;
     }
 
@@ -621,8 +620,8 @@
         nlohmann::json::parse(blocklistStream, nullptr, false);
     if (data.is_discarded())
     {
-        std::cerr << "Illegal blocklist file detected, cannot validate JSON, "
-                     "exiting\n";
+        lg2::error(
+            "Illegal blocklist file detected, cannot validate JSON, exiting");
         std::exit(EXIT_FAILURE);
     }
 
@@ -631,7 +630,7 @@
     // such as specific addresses or ranges.
     if (data.type() != nlohmann::json::value_t::object)
     {
-        std::cerr << "Illegal blocklist, expected to read dictionary\n";
+        lg2::error("Illegal blocklist, expected to read dictionary");
         std::exit(EXIT_FAILURE);
     }
 
@@ -643,7 +642,7 @@
         if (buses.type() != nlohmann::json::value_t::array)
         {
             // Buses field present but invalid, therefore this is an error.
-            std::cerr << "Invalid contents for blocklist buses field\n";
+            lg2::error("Invalid contents for blocklist buses field");
             std::exit(EXIT_FAILURE);
         }
 
@@ -682,7 +681,7 @@
         catch (const nlohmann::detail::type_error& e)
         {
             // Type mis-match is a critical error.
-            std::cerr << "Invalid bus type: " << e.what() << "\n";
+            lg2::error("Invalid bus type: {ERR}", "ERR", e.what());
             std::exit(EXIT_FAILURE);
         }
     }
@@ -698,7 +697,7 @@
 
         if (bus < 0)
         {
-            std::cerr << "Cannot translate " << i2cBus << " to int\n";
+            lg2::error("Cannot translate {BUS} to int", "BUS", i2cBus);
             continue;
         }
         auto busFind = busBlocklist.find(bus);
@@ -722,25 +721,25 @@
         auto file = open(i2cBus.c_str(), O_RDWR);
         if (file < 0)
         {
-            std::cerr << "unable to open i2c device " << i2cBus.string()
-                      << "\n";
+            lg2::error("unable to open i2c device {PATH}", "PATH",
+                       i2cBus.string());
             continue;
         }
         unsigned long funcs = 0;
 
         if (ioctl(file, I2C_FUNCS, &funcs) < 0)
         {
-            std::cerr
-                << "Error: Could not get the adapter functionality matrix bus "
-                << bus << "\n";
+            lg2::error(
+                "Error: Could not get the adapter functionality matrix bus {BUS}",
+                "BUS", bus);
             close(file);
             continue;
         }
         if (((funcs & I2C_FUNC_SMBUS_READ_BYTE) == 0U) ||
             ((I2C_FUNC_SMBUS_READ_I2C_BLOCK) == 0))
         {
-            std::cerr << "Error: Can't use SMBus Receive Byte command bus "
-                      << bus << "\n";
+            lg2::error("Error: Can't use SMBus Receive Byte command bus {BUS}",
+                       "BUS", bus);
             close(file);
             continue;
         }
@@ -800,7 +799,7 @@
         device, formattedFRU, bus, address, unknownBusObjectCount);
     if (!optionalProductName)
     {
-        std::cerr << "getProductName failed. product name is empty.\n";
+        lg2::error("getProductName failed. product name is empty.");
         return;
     }
 
@@ -881,7 +880,7 @@
         }
         else if (!iface->register_property(key, property.second + '\0'))
         {
-            std::cerr << "illegal key: " << key << "\n";
+            lg2::error("illegal key: {KEY}", "KEY", key);
         }
         lg2::debug("parsed FRU property: {FIRST}: {SECOND}", "FIRST",
                    property.first, "SECOND", property.second);
@@ -920,13 +919,13 @@
     boost::container::flat_map<std::string, std::string> tmp;
     if (fru.size() > maxFruSize)
     {
-        std::cerr << "Invalid fru.size() during writeFRU\n";
+        lg2::error("Invalid fru.size() during writeFRU");
         return false;
     }
     // verify legal fru by running it through fru parsing logic
     if (formatIPMIFRU(fru, tmp) != resCodes::resOK)
     {
-        std::cerr << "Invalid fru format during writeFRU\n";
+        lg2::error("Invalid fru format during writeFRU");
         return false;
     }
     // baseboard fru
@@ -935,7 +934,8 @@
         std::ofstream file(baseboardFruLocation, std::ios_base::binary);
         if (!file.good())
         {
-            std::cerr << "Error opening file " << baseboardFruLocation << "\n";
+            lg2::error("Error opening file {PATH}", "PATH",
+                       baseboardFruLocation);
             throw DBusInternalError();
             return false;
         }
@@ -953,7 +953,7 @@
         int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC);
         if (eeprom < 0)
         {
-            std::cerr << "unable to open i2c device " << path << "\n";
+            lg2::error("unable to open i2c device {PATH}", "PATH", path);
             throw DBusInternalError();
             return false;
         }
@@ -973,8 +973,8 @@
 
         if (lseek(eeprom, offset, SEEK_SET) < 0)
         {
-            std::cerr << "Unable to seek to offset " << offset
-                      << " in device: " << path << "\n";
+            lg2::error("Unable to seek to offset {OFFSET} in device: {PATH}",
+                       "OFFSET", offset, "PATH", path);
             close(eeprom);
             throw DBusInternalError();
         }
@@ -982,7 +982,7 @@
         ssize_t writtenBytes = write(eeprom, fru.data(), fru.size());
         if (writtenBytes < 0)
         {
-            std::cerr << "unable to write to i2c device " << path << "\n";
+            lg2::error("unable to write to i2c device {PATH}", "PATH", path);
             close(eeprom);
             throw DBusInternalError();
             return false;
@@ -997,13 +997,13 @@
     int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
     if (file < 0)
     {
-        std::cerr << "unable to open i2c device " << i2cBus << "\n";
+        lg2::error("unable to open i2c device {PATH}", "PATH", i2cBus);
         throw DBusInternalError();
         return false;
     }
     if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
     {
-        std::cerr << "unable to set device address\n";
+        lg2::error("unable to set device address");
         close(file);
         throw DBusInternalError();
         return false;
@@ -1021,7 +1021,7 @@
             // with the third bit being a memory page address bit.
             if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0)
             {
-                std::cerr << "unable to set device address\n";
+                lg2::error("unable to set device address");
                 close(file);
                 throw DBusInternalError();
                 return false;
@@ -1033,7 +1033,7 @@
         {
             if ((retries--) == 0U)
             {
-                std::cerr << "error writing fru: " << strerror(errno) << "\n";
+                lg2::error("error writing fru: {ERR}", "ERR", strerror(errno));
                 close(file);
                 throw DBusInternalError();
                 return false;
@@ -1077,8 +1077,8 @@
     {
         if (dbusCall)
         {
-            std::cerr << "Unable to access i2c bus " << static_cast<int>(busNum)
-                      << "\n";
+            lg2::error("Unable to access i2c bus {BUS}", "BUS",
+                       static_cast<int>(busNum));
             throw std::invalid_argument("Invalid Bus.");
         }
         return;
@@ -1139,7 +1139,7 @@
 
         if (ec)
         {
-            std::cerr << "Error in timer: " << ec.message() << "\n";
+            lg2::error("Error in timer: {ERR}", "ERR", ec.message());
             return;
         }
 
@@ -1149,7 +1149,7 @@
         boost::container::flat_map<size_t, fs::path> busPaths;
         if (!getI2cDevicePaths(devDir, busPaths))
         {
-            std::cerr << "unable to find i2c devices\n";
+            lg2::error("unable to find i2c devices");
             return;
         }
 
@@ -1321,7 +1321,7 @@
 
     if (!findFiles(devDir, matchString, i2cBuses))
     {
-        std::cerr << "unable to find i2c devices\n";
+        lg2::error("unable to find i2c devices");
         return 1;
     }
 
@@ -1408,7 +1408,7 @@
                              std::size_t bytesTransferred) {
             if (ec)
             {
-                std::cout << "Callback Error " << ec << "\n";
+                lg2::info("Callback Error {ERR}", "ERR", ec.message());
                 return;
             }
             size_t index = 0;
@@ -1429,8 +1429,8 @@
                             int bus = busStrToInt(name);
                             if (bus < 0)
                             {
-                                std::cerr
-                                    << "Could not parse bus " << name << "\n";
+                                lg2::error("Could not parse bus {BUS}", "BUS",
+                                           name);
                                 continue;
                             }
                             int rootBus = getRootBus(bus);
diff --git a/src/fru_device/fru_utils.cpp b/src/fru_device/fru_utils.cpp
index 4b01719..385ea72 100644
--- a/src/fru_device/fru_utils.cpp
+++ b/src/fru_device/fru_utils.cpp
@@ -10,7 +10,6 @@
 #include <cstdint>
 #include <filesystem>
 #include <iomanip>
-#include <iostream>
 #include <numeric>
 #include <set>
 #include <sstream>
@@ -95,7 +94,7 @@
     /* we need at least one byte to decode the type/len header */
     if (iter == end)
     {
-        std::cerr << "Truncated FRU data\n";
+        lg2::error("Truncated FRU data");
         return make_pair(DecodeState::err, value);
     }
 
@@ -114,7 +113,7 @@
     /* we should have at least len bytes of data available overall */
     if (iter + len > end)
     {
-        std::cerr << "FRU data field extends past end of FRU area data\n";
+        lg2::error("FRU data field extends past end of FRU area data");
         return make_pair(DecodeState::err, value);
     }
 
@@ -143,7 +142,7 @@
              */
             if (!isLangEng)
             {
-                std::cerr << "Error: Non english string is not supported \n";
+                lg2::error("Error: Non english string is not supported ");
                 return make_pair(DecodeState::err, value);
             }
 
@@ -193,8 +192,7 @@
     // but we don't support that.
     if ((lang != 0U) && lang != 25)
     {
-        std::cerr << "Warning: languages other than English is not "
-                     "supported\n";
+        lg2::error("Warning: languages other than English is not supported");
         // Return language flag as non english
         return false;
     }
@@ -218,21 +216,21 @@
     // check if Fru data has at least 8 byte header
     if (fruBytesSize <= fruBlockSize)
     {
-        std::cerr << "Error: trying to parse empty FRU\n";
+        lg2::error("Error: trying to parse empty FRU");
         return false;
     }
 
     // Check range of passed currentArea value
     if (currentArea > fruAreas::fruAreaMultirecord)
     {
-        std::cerr << "Error: Fru area is out of range\n";
+        lg2::error("Error: Fru area is out of range");
         return false;
     }
 
     unsigned int currentAreaIndex = getHeaderAreaFieldOffset(currentArea);
     if (currentAreaIndex > fruBytesSize)
     {
-        std::cerr << "Error: Fru area index is out of range\n";
+        lg2::error("Error: Fru area index is out of range");
         return false;
     }
 
@@ -252,7 +250,7 @@
         unsigned int areaIndex = getHeaderAreaFieldOffset(area);
         if (areaIndex > fruBytesSize)
         {
-            std::cerr << "Error: Fru area index is out of range\n";
+            lg2::error("Error: Fru area index is out of range");
             return false;
         }
 
@@ -266,9 +264,9 @@
         // check for overlapping of current offset with given areaoffset
         if (areaOffset == start || (areaOffset > start && areaOffset < end))
         {
-            std::cerr << getFruAreaName(currentArea)
-                      << " offset is overlapping with " << getFruAreaName(area)
-                      << " offset\n";
+            lg2::error("{AREA1} offset is overlapping with {AREA2} offset",
+                       "AREA1", getFruAreaName(currentArea), "AREA2",
+                       getFruAreaName(area));
             return false;
         }
     }
@@ -406,13 +404,13 @@
     }
     else if (state == DecodeState::err)
     {
-        std::cerr << "Error while parsing " << name << "\n";
+        lg2::error("Error while parsing {NAME}", "NAME", name);
 
         // Cancel decoding if failed to parse any of mandatory
         // fields
         if (fieldIndex < fruAreaFieldNames.size())
         {
-            std::cerr << "Failed to parse mandatory field \n";
+            lg2::error("Failed to parse mandatory field ");
             return resCodes::resErr;
         }
         return resCodes::resWarn;
@@ -421,8 +419,9 @@
     {
         if (fieldIndex < fruAreaFieldNames.size())
         {
-            std::cerr << "Mandatory fields absent in FRU area "
-                      << getFruAreaName(area) << " after " << name << "\n";
+            lg2::error(
+                "Mandatory fields absent in FRU area {AREA} after {NAME}",
+                "AREA", getFruAreaName(area), "NAME", name);
             return resCodes::resWarn;
         }
     }
@@ -436,7 +435,7 @@
     resCodes ret = resCodes::resOK;
     if (fruBytes.size() <= fruBlockSize)
     {
-        std::cerr << "Error: trying to parse empty FRU \n";
+        lg2::error("Error: trying to parse empty FRU ");
         return resCodes::resErr;
     }
     result["Common_Format_Version"] =
@@ -458,13 +457,14 @@
             fruBytes.begin() + offset;
         if (fruBytesIter + fruBlockSize >= fruBytes.end())
         {
-            std::cerr << "Not enough data to parse \n";
+            lg2::error("Not enough data to parse ");
             return resCodes::resErr;
         }
         // check for format version 1
         if (*fruBytesIter != 0x01)
         {
-            std::cerr << "Unexpected version " << *fruBytesIter << "\n";
+            lg2::error("Unexpected version {VERSION}", "VERSION",
+                       *fruBytesIter);
             return resCodes::resErr;
         }
         ++fruBytesIter;
@@ -493,7 +493,7 @@
                << static_cast<int>(fruComputedChecksum) << "\n";
             ss << "\tThe read checksum: 0x" << std::setw(2)
                << static_cast<int>(*fruBytesIterEndArea) << "\n";
-            std::cerr << ss.str();
+            lg2::error("{ERR}", "ERR", ss.str());
             ret = resCodes::resWarn;
         }
 
@@ -534,7 +534,7 @@
                                            "%Y%m%dT%H%M%SZ", &fruTime);
                 if (bytes == 0)
                 {
-                    std::cerr << "invalid time string encountered\n";
+                    lg2::error("invalid time string encountered");
                     return resCodes::resErr;
                 }
 
@@ -556,8 +556,9 @@
             }
             default:
             {
-                std::cerr << "Internal error: unexpected FRU area index: "
-                          << static_cast<int>(area) << " \n";
+                lg2::error(
+                    "Internal error: unexpected FRU area index: {INDEX} ",
+                    "INDEX", static_cast<int>(area));
                 return resCodes::resErr;
             }
         }
@@ -582,8 +583,8 @@
             uint8_t c = *fruBytesIter;
             if (c != 0U)
             {
-                std::cerr << "Non-zero byte after EndOfFields in FRU area "
-                          << getFruAreaName(area) << "\n";
+                lg2::error("Non-zero byte after EndOfFields in FRU area {AREA}",
+                           "AREA", getFruAreaName(area));
                 ret = resCodes::resWarn;
                 break;
             }
@@ -722,8 +723,8 @@
 {
     if (reader.read(baseOffset, 0x8, blockData.data()) < 0)
     {
-        std::cerr << "failed to read " << errorHelp << " base offset "
-                  << baseOffset << "\n";
+        lg2::error("failed to read {ERR} base offset {OFFSET}", "ERR",
+                   errorHelp, "OFFSET", baseOffset);
         return false;
     }
 
@@ -804,8 +805,8 @@
          */
         if (areaOffset <= prevOffset)
         {
-            std::cerr << "Fru area offsets are not in required order as per "
-                         "Section 17 of Fru specification\n";
+            lg2::error(
+                "Fru area offsets are not in required order as per Section 17 of Fru specification");
             return {{}, true};
         }
         prevOffset = areaOffset;
@@ -822,8 +823,8 @@
 
         if (reader.read(baseOffset + areaOffset, 0x2, blockData.data()) < 0)
         {
-            std::cerr << "failed to read " << errorHelp << " base offset "
-                      << baseOffset << "\n";
+            lg2::error("failed to read {ERR} base offset {OFFSET}", "ERR",
+                       errorHelp, "OFFSET", baseOffset);
             return {{}, true};
         }
 
@@ -852,8 +853,8 @@
             // record has 3 bytes of the header we care about.
             if (reader.read(baseOffset + areaOffset, 0x3, blockData.data()) < 0)
             {
-                std::cerr << "failed to read " << errorHelp << " base offset "
-                          << baseOffset << "\n";
+                lg2::error("failed to read {STR} base offset {OFFSET}", "STR",
+                           errorHelp, "OFFSET", baseOffset);
                 return {{}, true};
             }
 
@@ -884,8 +885,8 @@
         if (reader.read(baseOffset + readOffset, requestLength,
                         blockData.data()) < 0)
         {
-            std::cerr << "failed to read " << errorHelp << " base offset "
-                      << baseOffset << "\n";
+            lg2::error("failed to read {ERR} base offset {OFFSET}", "ERR",
+                       errorHelp, "OFFSET", baseOffset);
             return {{}, true};
         }
 
@@ -1383,8 +1384,8 @@
     auto it = std::find(fruAreaNames.begin(), fruAreaNames.end(), areaName);
     if (it == fruAreaNames.end())
     {
-        std::cerr << "Can't parse area name for property " << propertyName
-                  << " \n";
+        lg2::error("Can't parse area name for property {PROP} ", "PROP",
+                   propertyName);
         return false;
     }
     fruAreas fruAreaToUpdate = static_cast<fruAreas>(it - fruAreaNames.begin());
@@ -1407,12 +1408,12 @@
             fruAreaFieldNames = &productFruAreas;
             break;
         default:
-            std::cerr << "Invalid PropertyName " << propertyName << " \n";
+            lg2::error("Invalid PropertyName {PROP}", "PROP", propertyName);
             return false;
     }
     if (fruAreaOffsetFieldValue == 0)
     {
-        std::cerr << "FRU Area for " << propertyName << " not present \n";
+        lg2::error("FRU Area for {PROP} not present ", "PROP", propertyName);
         return false;
     }
 
@@ -1438,8 +1439,8 @@
         std::size_t pos = propertyName.find(fruCustomFieldName);
         if (pos == std::string::npos)
         {
-            std::cerr << "PropertyName doesn't exist in FRU Area Vectors: "
-                      << propertyName << "\n";
+            lg2::error("PropertyName doesn't exist in FRU Area Vectors: {PROP}",
+                       "PROP", propertyName);
             return false;
         }
         std::string fieldNumStr =
@@ -1447,8 +1448,8 @@
         size_t fieldNum = std::stoi(fieldNumStr);
         if (fieldNum == 0)
         {
-            std::cerr << "PropertyName not recognized: " << propertyName
-                      << "\n";
+            lg2::error("PropertyName not recognized: {PROP}", "PROP",
+                       propertyName);
             return false;
         }
         skipToFRUUpdateField += fieldNum;
@@ -1488,7 +1489,7 @@
     ssize_t fieldLength = getFieldLength(fruData[fieldLoc]);
     if (fieldLength < 0)
     {
-        std::cerr << "Property " << propertyName << " not present \n";
+        lg2::error("Property {PROP} not present ", "PROP", propertyName);
         return false;
     }
 
@@ -1580,14 +1581,15 @@
     resCodes res = formatIPMIFRU(device, formattedFRU);
     if (res == resCodes::resErr)
     {
-        std::cerr << "failed to parse FRU for device at bus " << bus
-                  << " address " << address << "\n";
+        lg2::error("failed to parse FRU for device at bus {BUS} address {ADDR}",
+                   "BUS", bus, "ADDR", address);
         return std::nullopt;
     }
     if (res == resCodes::resWarn)
     {
-        std::cerr << "Warnings while parsing FRU for device at bus " << bus
-                  << " address " << address << "\n";
+        lg2::error(
+            "Warnings while parsing FRU for device at bus {BUS} address {ADDR}",
+            "BUS", bus, "ADDR", address);
     }
 
     auto productNameFind = formattedFRU.find("BOARD_PRODUCT_NAME");
@@ -1622,7 +1624,7 @@
     }
     catch (const std::invalid_argument& e)
     {
-        std::cerr << "Failure getting FRU Info" << e.what() << "\n";
+        lg2::error("Failure getting FRU Info: {ERR}", "ERR", e);
         return false;
     }
 
diff --git a/src/utils.cpp b/src/utils.cpp
index b408aea..4a35756 100644
--- a/src/utils.cpp
+++ b/src/utils.cpp
@@ -9,10 +9,10 @@
 #include <boost/algorithm/string/split.hpp>
 #include <boost/container/flat_map.hpp>
 #include <boost/lexical_cast.hpp>
+#include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/bus/match.hpp>
 
 #include <filesystem>
-#include <iostream>
 #include <map>
 #include <regex>
 
@@ -144,8 +144,9 @@
             }
             catch (const std::regex_error&)
             {
-                std::cerr << "Syntax error in regular expression: " << probe
-                          << " will never match";
+                lg2::error(
+                    "Syntax error in regular expression: {PROBE} will never match",
+                    "PROBE", probe);
             }
         }
 
diff --git a/test/test_gpio_presence.cpp b/test/test_gpio_presence.cpp
index 727bc40..8ab309b 100644
--- a/test/test_gpio_presence.cpp
+++ b/test/test_gpio_presence.cpp
@@ -144,7 +144,7 @@
     {
         // expected, since cable 0 is gone.
         // have to do something here to shut up clang-tidy
-        std::cout << "" << std::endl;
+        lg2::info("");
     }
 
     ctx.request_stop();