[logging] Introduced log modules and categories selection (#24189)

* [logging] Introduced log modules and categories selection

Currently it is possible to disable some part of logs based on
their level, but not on their origin module (region). After
introducing specific log modules selection it would be
convenient to make controlling those modules log categories
possible as well.

Summary of changes:
* Added definitions for every existing log module that are by
default set to 1, but can be ovewritten to 0.
* Introduced IsModuleCategoryEnabled macro that returns
category state (0/1) for specific log module.
* To avoid creating dozens of new definitions for every
module-category combination, IsModuleCategoryEnabled macro
by default returns corresponding global category value
(CHIP_DETAIL/PROGRESS/ERROR/AUTOMATION_LOGGING). If any
category value was defined for specific module, it is used
instead of global one.
* Added filtering based on the mentioned definitions, so
ChipInternalLog and ChipInternalLogByteSpan has definitions
provided only if corresponding origin module is enabled and the
specific category for this module is enabled.

nRFConnect:
* Disabled some modules for nrfconnect platform that saved ~11,8k
of flash.
* Switched from using kconfig value to filer out logs levels
to relying on Matter logging layer filtering in Zephyr logging
implementation.

* Fixed logs for Darwin platform

* Fixed Ameba platform
diff --git a/config/nrfconnect/chip-module/Kconfig b/config/nrfconnect/chip-module/Kconfig
index d824781..4b477d3 100644
--- a/config/nrfconnect/chip-module/Kconfig
+++ b/config/nrfconnect/chip-module/Kconfig
@@ -169,3 +169,11 @@
 	default y if CHIP_FACTORY_DATA || CHIP_FACTORY_DATA_CUSTOM_BACKEND
 
 endif
+
+config CHIP_LOG_SIZE_OPTIMIZATION
+	bool "Disable some detailed logs to decrease flash usage"
+	help
+	  Disables some log levels for the specific log modules
+	  providing detailed information that are not used in most cases.
+	  You can find full configuration enabled by this option
+	  in the platform/nrfconnect/CHIPPlatformConfig.h file.
diff --git a/config/nrfconnect/chip-module/Kconfig.defaults b/config/nrfconnect/chip-module/Kconfig.defaults
index f24fc46..9232a46 100644
--- a/config/nrfconnect/chip-module/Kconfig.defaults
+++ b/config/nrfconnect/chip-module/Kconfig.defaults
@@ -38,6 +38,10 @@
     int
     default 2
 
+config CHIP_LOG_SIZE_OPTIMIZATION
+    bool
+    default y
+
 endif
 
 config PRINTK_SYNC
diff --git a/examples/all-clusters-app/nrfconnect/main/ZclDoorLockCallbacks.cpp b/examples/all-clusters-app/nrfconnect/main/ZclDoorLockCallbacks.cpp
index edaa86e..0ab2703 100644
--- a/examples/all-clusters-app/nrfconnect/main/ZclDoorLockCallbacks.cpp
+++ b/examples/all-clusters-app/nrfconnect/main/ZclDoorLockCallbacks.cpp
@@ -26,7 +26,7 @@
 using namespace ::chip::app::Clusters;
 using namespace ::chip::app::Clusters::DoorLock;
 
-LOG_MODULE_DECLARE(app, CONFIG_MATTER_LOG_LEVEL);
+LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL);
 
 // Provided some empty callbacks and replaced feature map
 // to simulate DoorLock endpoint for All-Clusters-App example
diff --git a/examples/chef/nrfconnect/main.cpp b/examples/chef/nrfconnect/main.cpp
index 8c30619..17e207e 100644
--- a/examples/chef/nrfconnect/main.cpp
+++ b/examples/chef/nrfconnect/main.cpp
@@ -43,7 +43,7 @@
 #include "Rpc.h"
 #endif
 
-LOG_MODULE_REGISTER(app, CONFIG_MATTER_LOG_LEVEL);
+LOG_MODULE_REGISTER(app, CONFIG_CHIP_APP_LOG_LEVEL);
 
 using namespace chip;
 using namespace chip::Shell;
diff --git a/examples/lock-app/nrfconnect/main/AppTask.cpp b/examples/lock-app/nrfconnect/main/AppTask.cpp
index f767e5d..77d6899 100644
--- a/examples/lock-app/nrfconnect/main/AppTask.cpp
+++ b/examples/lock-app/nrfconnect/main/AppTask.cpp
@@ -51,7 +51,7 @@
 #include <zephyr/logging/log.h>
 #include <zephyr/zephyr.h>
 
-LOG_MODULE_DECLARE(app, CONFIG_MATTER_LOG_LEVEL);
+LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL);
 
 using namespace ::chip;
 using namespace ::chip::app;
diff --git a/examples/lock-app/nrfconnect/main/ZclCallbacks.cpp b/examples/lock-app/nrfconnect/main/ZclCallbacks.cpp
index 24b6b77..48ab16b 100644
--- a/examples/lock-app/nrfconnect/main/ZclCallbacks.cpp
+++ b/examples/lock-app/nrfconnect/main/ZclCallbacks.cpp
@@ -29,7 +29,7 @@
 using namespace ::chip::app::Clusters;
 using namespace ::chip::app::Clusters::DoorLock;
 
-LOG_MODULE_DECLARE(app, CONFIG_MATTER_LOG_LEVEL);
+LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL);
 
 void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath, uint8_t type, uint16_t size,
                                        uint8_t * value)
diff --git a/examples/lock-app/nrfconnect/main/main.cpp b/examples/lock-app/nrfconnect/main/main.cpp
index 0c3b58e..93a0062 100644
--- a/examples/lock-app/nrfconnect/main/main.cpp
+++ b/examples/lock-app/nrfconnect/main/main.cpp
@@ -21,7 +21,7 @@
 
 #include <zephyr/logging/log.h>
 
-LOG_MODULE_REGISTER(app, CONFIG_MATTER_LOG_LEVEL);
+LOG_MODULE_REGISTER(app, CONFIG_CHIP_APP_LOG_LEVEL);
 
 using namespace ::chip;
 
diff --git a/examples/pigweed-app/nrfconnect/main/main.cpp b/examples/pigweed-app/nrfconnect/main/main.cpp
index 78c6212..9abede4 100644
--- a/examples/pigweed-app/nrfconnect/main/main.cpp
+++ b/examples/pigweed-app/nrfconnect/main/main.cpp
@@ -28,7 +28,7 @@
 #include <dk_buttons_and_leds.h>
 #include <zephyr/logging/log.h>
 
-LOG_MODULE_REGISTER(app, CONFIG_MATTER_LOG_LEVEL);
+LOG_MODULE_REGISTER(app, CONFIG_CHIP_APP_LOG_LEVEL);
 
 namespace {
 LEDWidget sStatusLED;
diff --git a/examples/platform/nrfconnect/Rpc.cpp b/examples/platform/nrfconnect/Rpc.cpp
index c996106..5b76dc3 100644
--- a/examples/platform/nrfconnect/Rpc.cpp
+++ b/examples/platform/nrfconnect/Rpc.cpp
@@ -26,7 +26,7 @@
 
 #include <zephyr/kernel.h>
 
-LOG_MODULE_DECLARE(app, CONFIG_MATTER_LOG_LEVEL);
+LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL);
 
 #if defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
 #include "pigweed/rpc_services/Attributes.h"
diff --git a/examples/platform/nrfconnect/util/PWMDevice.cpp b/examples/platform/nrfconnect/util/PWMDevice.cpp
index b51d33c..2d17d31 100644
--- a/examples/platform/nrfconnect/util/PWMDevice.cpp
+++ b/examples/platform/nrfconnect/util/PWMDevice.cpp
@@ -26,7 +26,7 @@
 #include <zephyr/logging/log.h>
 #include <zephyr/zephyr.h>
 
-LOG_MODULE_DECLARE(app, CONFIG_MATTER_LOG_LEVEL);
+LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL);
 
 int PWMDevice::Init(const pwm_dt_spec * aPWMDevice, uint8_t aMinLevel, uint8_t aMaxLevel, uint8_t aDefaultLevel)
 {
diff --git a/src/lib/support/logging/CHIPLogging.h b/src/lib/support/logging/CHIPLogging.h
index 28bdeb5..1591e68 100644
--- a/src/lib/support/logging/CHIPLogging.h
+++ b/src/lib/support/logging/CHIPLogging.h
@@ -100,7 +100,7 @@
  *   category.
  *
  */
-#define ChipLogError(MOD, MSG, ...) ChipInternalLog(MOD, Error, MSG, ##__VA_ARGS__)
+#define ChipLogError(MOD, MSG, ...) ChipInternalLog(MOD, ERROR, MSG, ##__VA_ARGS__)
 #else // CHIP_ERROR_LOGGING
 #define ChipLogError(MOD, MSG, ...) ((void) 0)
 #endif // CHIP_ERROR_LOGGING
@@ -114,7 +114,7 @@
  *   category.
  *
  */
-#define ChipLogProgress(MOD, MSG, ...) ChipInternalLog(MOD, Progress, MSG, ##__VA_ARGS__)
+#define ChipLogProgress(MOD, MSG, ...) ChipInternalLog(MOD, PROGRESS, MSG, ##__VA_ARGS__)
 #else // CHIP_PROGRESS_LOGGING
 #define ChipLogProgress(MOD, MSG, ...) ((void) 0)
 #endif // CHIP_PROGRESS_LOGGING
@@ -128,7 +128,7 @@
  *   category.
  *
  */
-#define ChipLogDetail(MOD, MSG, ...) ChipInternalLog(MOD, Detail, MSG, ##__VA_ARGS__)
+#define ChipLogDetail(MOD, MSG, ...) ChipInternalLog(MOD, DETAIL, MSG, ##__VA_ARGS__)
 
 /**
  * @def ChipLogByteSpan(MOD, DATA)
@@ -137,7 +137,7 @@
  *   Log a byte span for the specified module in the 'Detail' category.
  *
  */
-#define ChipLogByteSpan(MOD, DATA) ChipInternalLogByteSpan(MOD, Detail, DATA)
+#define ChipLogByteSpan(MOD, DATA) ChipInternalLogByteSpan(MOD, DETAIL, DATA)
 #else // CHP_DETAIL_LOGGING
 #define ChipLogDetail(MOD, MSG, ...) ((void) 0)
 #define ChipLogByteSpan(MOD, DATA) ((void) 0)
@@ -152,7 +152,7 @@
  *   category.
  *
  */
-#define ChipLogAutomation(MSG, ...) ChipInternalLog(Automation, Automation, MSG, ##__VA_ARGS__)
+#define ChipLogAutomation(MSG, ...) ChipInternalLog(Automation, AUTOMATION, MSG, ##__VA_ARGS__)
 #else // CHIP_AUTOMATION_LOGGING
 #define ChipLogAutomation(MOD, MSG, ...) ((void) 0)
 #endif // CHIP_AUTOMATION_LOGGING
@@ -346,6 +346,55 @@
 }
 #endif // CHIP_LOG_FILTERING
 
+/* Internal macros mapping upper case definitions to camel case category constants*/
+#define CHIP_LOG_CATEGORY_DETAIL chip::Logging::kLogCategory_Detail
+#define CHIP_LOG_CATEGORY_PROGRESS chip::Logging::kLogCategory_Progress
+#define CHIP_LOG_CATEGORY_ERROR chip::Logging::kLogCategory_Error
+#define CHIP_LOG_CATEGORY_AUTOMATION chip::Logging::kLogCategory_Automation
+
+/*
+ * CHIP Logging Modules Categories filtering implementation.
+ *
+ * @brief
+ *     Macro for use to check if given category is enabled for given log module.
+ *
+ * Example Usage:
+ *     Let's assume PROGRESS category control for DeviceLayer log module.
+ *
+ *     Default behavior - category is not modified, so macro returns global category value:
+ *     IsModuleCategoryEnabled(DeviceLayer, PROGRESS) returns CHIP_PROGRESS_LOGGING
+ *
+ *     Enabling category - category is enabled for module, the category value is ignored and the global value is used:
+ *     #define CHIP_CONFIG_LOG_MODULE_DeviceLayer_PROGRESS 1
+ *     IsModuleCategoryEnabled(DeviceLayer, PROGRESS) returns CHIP_PROGRESS_LOGGING
+ *
+ *     Disabling category - category is disabled for module, ignoring global category value:
+ *     #define CHIP_CONFIG_LOG_MODULE_DeviceLayer_PROGRESS 0
+ *     IsModuleCategoryEnabled(DeviceLayer, PROGRESS) returns 0
+ *
+ * Algorithm flow:
+ *     1. IsModuleCategoryEnabled(MOD, CAT) uses MOD and CAT to create strings for category module macro and global category macro,
+ * and invokes _IsModuleCategoryEnabled1().
+ *     2. _IsModuleCategoryEnabled1(MOD_CAT, GLOB_CAT) invokes _IsModuleCategoryEnabled2(MOD_CAT, GLOB_CAT) to extract macros
+ * values.
+ *     3. _IsModuleCategoryEnabled2(MOD_CAT, GLOB_CAT) uses MOD_CAT to create string for helper macro.
+ *        - If MOD_CAT is 0 the helper macro containing dummy arg is used.
+ *        - If MOD_CAT doesn't exist, empty is used.
+ *     4. _IsModuleCategoryEnabled3(DUMMY_ARG, GLOB_CAT) invokes _IsModuleCategoryEnabled4() using different number of arguments
+ * depending on DUMMY_ARG.
+ *     5. _IsModuleCategoryEnabled4 output:
+ *        - If category for module was not defined or define 1, the DUMMY_ARG was empty, so returning ARG2 is GLOB_CAT.
+ *        - If category for module was defined 0, the DUMMY_ARG had one argument, so returning ARG2 is 0.
+ *
+ */
+#define IsModuleCategoryEnabled(MOD, CAT) _IsModuleCategoryEnabled1(CHIP_CONFIG_LOG_MODULE_##MOD##_##CAT, CHIP_##CAT##_LOGGING)
+#define _IsModuleCategoryEnabled1(MOD_CAT, GLOB_CAT) _IsModuleCategoryEnabled2(MOD_CAT, GLOB_CAT)
+#define _IsModuleCategoryEnabled2(MOD_CAT, GLOB_CAT)                                                                               \
+    _IsModuleCategoryEnabled3(_IsModuleCategoryEnabled3_DummyArg##MOD_CAT, GLOB_CAT)
+#define _IsModuleCategoryEnabled3_DummyArg0 dummyArg,
+#define _IsModuleCategoryEnabled3(DUMMY_ARG, GLOB_CAT) _IsModuleCategoryEnabled4(DUMMY_ARG 0, GLOB_CAT)
+#define _IsModuleCategoryEnabled4(ARG1, ARG2, ...) (ARG2)
+
 DLL_LOCAL void Log(uint8_t module, uint8_t category, const char * msg, ...) ENFORCE_FORMAT(3, 4);
 DLL_LOCAL void LogByteSpan(uint8_t module, uint8_t category, const ByteSpan & span);
 DLL_LOCAL void LogV(uint8_t module, uint8_t category, const char * msg, va_list args) ENFORCE_FORMAT(3, 0);
@@ -360,28 +409,36 @@
 #define ChipInternalLog(...) ChipPlatformLog(__VA_ARGS__)
 #define ChipInternalLogByteSpan(...) ChipPlatformLogByteSpan(__VA_ARGS__)
 #else // CHIP_SYSTEM_CONFIG_PLATFORM_LOG
-#define ChipInternalLog(MOD, CAT, MSG, ...) ChipInternalLogImpl(MOD, CAT, MSG, ##__VA_ARGS__)
-#define ChipInternalLogByteSpan(MOD, CAT, DATA) ChipInternalLogByteSpanImpl(MOD, CAT, DATA)
+#define ChipInternalLog(MOD, CAT, MSG, ...)                                                                                        \
+    if (CHIP_CONFIG_LOG_MODULE_##MOD && IsModuleCategoryEnabled(MOD, CAT))                                                         \
+    {                                                                                                                              \
+        ChipInternalLogImpl(MOD, CHIP_LOG_CATEGORY_##CAT, MSG, ##__VA_ARGS__);                                                     \
+    }
+
+#define ChipInternalLogByteSpan(MOD, CAT, DATA)                                                                                    \
+    if (CHIP_CONFIG_LOG_MODULE_##MOD && IsModuleCategoryEnabled(MOD, CAT))                                                         \
+    {                                                                                                                              \
+        ChipInternalLogByteSpanImpl(MOD, CHIP_LOG_CATEGORY_##CAT, DATA);                                                           \
+    }
 #endif // CHIP_SYSTEM_CONFIG_PLATFORM_LOG
 
 #if CHIP_PW_TOKENIZER_LOGGING
 #define ChipInternalLogImpl(MOD, CAT, MSG, ...)                                                                                    \
     do                                                                                                                             \
     {                                                                                                                              \
-        if (chip::Logging::IsCategoryEnabled(chip::Logging::kLogCategory_##CAT))                                                   \
+        if (chip::Logging::IsCategoryEnabled(CAT))                                                                                 \
         {                                                                                                                          \
-            PW_TOKENIZE_TO_GLOBAL_HANDLER_WITH_PAYLOAD(                                                                            \
-                (pw_tokenizer_Payload)((chip::Logging::kLogCategory_##CAT << 8) | chip::Logging::kLogModule_##MOD), MSG,           \
-                __VA_ARGS__);                                                                                                      \
+            PW_TOKENIZE_TO_GLOBAL_HANDLER_WITH_PAYLOAD((pw_tokenizer_Payload)((CAT << 8) | chip::Logging::kLogModule_##MOD), MSG,  \
+                                                       __VA_ARGS__);                                                               \
         }                                                                                                                          \
     } while (0)
 #else // CHIP_PW_TOKENIZER_LOGGING
 #define ChipInternalLogImpl(MOD, CAT, MSG, ...)                                                                                    \
     do                                                                                                                             \
     {                                                                                                                              \
-        if (chip::Logging::IsCategoryEnabled(chip::Logging::kLogCategory_##CAT))                                                   \
+        if (chip::Logging::IsCategoryEnabled(CAT))                                                                                 \
         {                                                                                                                          \
-            chip::Logging::Log(chip::Logging::kLogModule_##MOD, chip::Logging::kLogCategory_##CAT, MSG, ##__VA_ARGS__);            \
+            chip::Logging::Log(chip::Logging::kLogModule_##MOD, CAT, MSG, ##__VA_ARGS__);                                          \
         }                                                                                                                          \
     } while (0)
 #endif // CHIP_PW_TOKENIZER_LOGGING
@@ -389,9 +446,9 @@
 #define ChipInternalLogByteSpanImpl(MOD, CAT, DATA)                                                                                \
     do                                                                                                                             \
     {                                                                                                                              \
-        if (chip::Logging::IsCategoryEnabled(chip::Logging::kLogCategory_##CAT))                                                   \
+        if (chip::Logging::IsCategoryEnabled(CAT))                                                                                 \
         {                                                                                                                          \
-            chip::Logging::LogByteSpan(chip::Logging::kLogModule_##MOD, chip::Logging::kLogCategory_##CAT, DATA);                  \
+            chip::Logging::LogByteSpan(chip::Logging::kLogModule_##MOD, CAT, DATA);                                                \
         }                                                                                                                          \
     } while (0)
 
diff --git a/src/lib/support/logging/Constants.h b/src/lib/support/logging/Constants.h
index c91cf1a..5abf073 100644
--- a/src/lib/support/logging/Constants.h
+++ b/src/lib/support/logging/Constants.h
@@ -63,6 +63,169 @@
     kLogModule_Max
 };
 
+/* Log modules enablers. Those definitions can be overwritten with 0 to disable
+   some log regions. */
+
+#ifndef CHIP_CONFIG_LOG_MODULE_NotSpecified
+#define CHIP_CONFIG_LOG_MODULE_NotSpecified 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_Inet
+#define CHIP_CONFIG_LOG_MODULE_Inet 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_Ble
+#define CHIP_CONFIG_LOG_MODULE_Ble 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_MessageLayer
+#define CHIP_CONFIG_LOG_MODULE_MessageLayer 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_SecurityManager
+#define CHIP_CONFIG_LOG_MODULE_SecurityManager 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_ExchangeManager
+#define CHIP_CONFIG_LOG_MODULE_ExchangeManager 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_TLV
+#define CHIP_CONFIG_LOG_MODULE_TLV 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_ASN1
+#define CHIP_CONFIG_LOG_MODULE_ASN1 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_Crypto
+#define CHIP_CONFIG_LOG_MODULE_Crypto 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_Controller
+#define CHIP_CONFIG_LOG_MODULE_Controller 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_Alarm
+#define CHIP_CONFIG_LOG_MODULE_Alarm 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_SecureChannel
+#define CHIP_CONFIG_LOG_MODULE_SecureChannel 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_BDX
+#define CHIP_CONFIG_LOG_MODULE_BDX 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_DataManagement
+#define CHIP_CONFIG_LOG_MODULE_DataManagement 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_DeviceControl
+#define CHIP_CONFIG_LOG_MODULE_DeviceControl 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_DeviceDescription
+#define CHIP_CONFIG_LOG_MODULE_DeviceDescription 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_Echo
+#define CHIP_CONFIG_LOG_MODULE_Echo 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_FabricProvisioning
+#define CHIP_CONFIG_LOG_MODULE_FabricProvisioning 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_NetworkProvisioning
+#define CHIP_CONFIG_LOG_MODULE_NetworkProvisioning 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_ServiceDiscovery
+#define CHIP_CONFIG_LOG_MODULE_ServiceDiscovery 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_ServiceProvisioning
+#define CHIP_CONFIG_LOG_MODULE_ServiceProvisioning 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_SoftwareUpdate
+#define CHIP_CONFIG_LOG_MODULE_SoftwareUpdate 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_FailSafe
+#define CHIP_CONFIG_LOG_MODULE_FailSafe 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_TimeService
+#define CHIP_CONFIG_LOG_MODULE_TimeService 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_Heartbeat
+#define CHIP_CONFIG_LOG_MODULE_Heartbeat 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_chipSystemLayer
+#define CHIP_CONFIG_LOG_MODULE_chipSystemLayer 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_EventLogging
+#define CHIP_CONFIG_LOG_MODULE_EventLogging 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_Support
+#define CHIP_CONFIG_LOG_MODULE_Support 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_chipTool
+#define CHIP_CONFIG_LOG_MODULE_chipTool 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_Zcl
+#define CHIP_CONFIG_LOG_MODULE_Zcl 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_Shell
+#define CHIP_CONFIG_LOG_MODULE_Shell 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_DeviceLayer
+#define CHIP_CONFIG_LOG_MODULE_DeviceLayer 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_SetupPayload
+#define CHIP_CONFIG_LOG_MODULE_SetupPayload 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_AppServer
+#define CHIP_CONFIG_LOG_MODULE_AppServer 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_Discovery
+#define CHIP_CONFIG_LOG_MODULE_Discovery 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_InteractionModel
+#define CHIP_CONFIG_LOG_MODULE_InteractionModel 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_Test
+#define CHIP_CONFIG_LOG_MODULE_Test 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_OperationalSessionSetup
+#define CHIP_CONFIG_LOG_MODULE_OperationalSessionSetup 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_Automation
+#define CHIP_CONFIG_LOG_MODULE_Automation 1
+#endif
+
+#ifndef CHIP_CONFIG_LOG_MODULE_CASESessionManager
+#define CHIP_CONFIG_LOG_MODULE_CASESessionManager 1
+#endif
+
 /**
  *  @enum LogCategory
  *
diff --git a/src/platform/Ameba/AmebaConfig.cpp b/src/platform/Ameba/AmebaConfig.cpp
index f5b1ae9..398de03 100644
--- a/src/platform/Ameba/AmebaConfig.cpp
+++ b/src/platform/Ameba/AmebaConfig.cpp
@@ -104,8 +104,10 @@
 
     success = getPref_bool_new(key.Namespace, key.Name, &intVal);
     if (success != 0)
+    {
         ChipLogProgress(DeviceLayer, "getPref_bool_new: %s/%s failed\n", StringOrNullMarker(key.Namespace),
                         StringOrNullMarker(key.Name));
+    }
 
     val = (intVal != 0);
 
@@ -121,8 +123,10 @@
 
     success = getPref_u32_new(key.Namespace, key.Name, &val);
     if (success != 0)
+    {
         ChipLogProgress(DeviceLayer, "getPref_u32_new: %s/%s failed\n", StringOrNullMarker(key.Namespace),
                         StringOrNullMarker(key.Name));
+    }
 
     if (success == 0)
         return CHIP_NO_ERROR;
@@ -136,8 +140,10 @@
 
     success = getPref_u64_new(key.Namespace, key.Name, &val);
     if (success != 0)
+    {
         ChipLogProgress(DeviceLayer, "getPref_u32_new: %s/%s failed\n", StringOrNullMarker(key.Namespace),
                         StringOrNullMarker(key.Name));
+    }
 
     if (success == 0)
         return CHIP_NO_ERROR;
@@ -151,8 +157,10 @@
 
     success = getPref_str_new(key.Namespace, key.Name, buf, bufSize, &outLen);
     if (success != 0)
+    {
         ChipLogProgress(DeviceLayer, "getPref_str_new: %s/%s failed\n", StringOrNullMarker(key.Namespace),
                         StringOrNullMarker(key.Name));
+    }
 
     if (success == 0)
     {
@@ -172,8 +180,10 @@
 
     success = getPref_bin_new(key.Namespace, key.Name, buf, bufSize, &outLen);
     if (success != 0)
+    {
         ChipLogProgress(DeviceLayer, "getPref_bin_new: %s/%s failed\n", StringOrNullMarker(key.Namespace),
                         StringOrNullMarker(key.Name));
+    }
 
     if (success == 0)
     {
@@ -197,11 +207,15 @@
         value = 0;
     success = setPref_new(key.Namespace, key.Name, &value, 1);
     if (!success)
+    {
         ChipLogError(DeviceLayer, "setPref: %s/%s = %s failed\n", StringOrNullMarker(key.Namespace), StringOrNullMarker(key.Name),
                      value ? "true" : "false");
+    }
     else
+    {
         ChipLogProgress(DeviceLayer, "NVS set: %s/%s = %s", StringOrNullMarker(key.Namespace), StringOrNullMarker(key.Name),
                         val ? "true" : "false");
+    }
 
     return CHIP_NO_ERROR;
 }
@@ -212,11 +226,15 @@
 
     success = setPref_new(key.Namespace, key.Name, (uint8_t *) &val, sizeof(uint32_t));
     if (!success)
+    {
         ChipLogError(DeviceLayer, "setPref: %s/%s = %d(0x%x) failed\n", StringOrNullMarker(key.Namespace),
                      StringOrNullMarker(key.Name), val, val);
+    }
     else
+    {
         ChipLogProgress(DeviceLayer, "NVS set: %s/%s = %" PRIu32 " (0x%" PRIX32 ")", StringOrNullMarker(key.Namespace),
                         StringOrNullMarker(key.Name), val, val);
+    }
 
     return CHIP_NO_ERROR;
 }
@@ -227,11 +245,15 @@
 
     success = setPref_new(key.Namespace, key.Name, (uint8_t *) &val, sizeof(uint64_t));
     if (!success)
+    {
         ChipLogError(DeviceLayer, "setPref: %s/%s = %d(0x%x) failed\n", StringOrNullMarker(key.Namespace),
                      StringOrNullMarker(key.Name), val, val);
+    }
     else
+    {
         ChipLogProgress(DeviceLayer, "NVS set: %s/%s = %" PRIu64 " (0x%" PRIX64 ")", StringOrNullMarker(key.Namespace),
                         StringOrNullMarker(key.Name), val, val);
+    }
 
     return CHIP_NO_ERROR;
 }
@@ -242,11 +264,15 @@
 
     success = setPref_new(key.Namespace, key.Name, (uint8_t *) str, strlen(str) + 1);
     if (!success)
+    {
         ChipLogError(DeviceLayer, "setPref: %s/%s = %s failed\n", StringOrNullMarker(key.Namespace), StringOrNullMarker(key.Name),
                      StringOrNullMarker(str));
+    }
     else
+    {
         ChipLogProgress(DeviceLayer, "NVS set: %s/%s = \"%s\"", StringOrNullMarker(key.Namespace), StringOrNullMarker(key.Name),
                         StringOrNullMarker(str));
+    }
     return CHIP_NO_ERROR;
 }
 
@@ -272,10 +298,14 @@
 
     success = setPref_new(key.Namespace, key.Name, (uint8_t *) data, dataLen);
     if (!success)
+    {
         ChipLogError(DeviceLayer, "setPref: %s/%s failed\n", StringOrNullMarker(key.Namespace), StringOrNullMarker(key.Name));
+    }
     else
+    {
         ChipLogProgress(DeviceLayer, "NVS set: %s/%s = (blob length %" PRId32 ")", StringOrNullMarker(key.Namespace),
                         StringOrNullMarker(key.Name), dataLen);
+    }
 
     return CHIP_NO_ERROR;
 }
@@ -286,10 +316,14 @@
 
     success = deleteKey(key.Namespace, key.Name);
     if (!success)
+    {
         ChipLogProgress(DeviceLayer, "%s : %s/%s failed\n", __FUNCTION__, StringOrNullMarker(key.Namespace),
                         StringOrNullMarker(key.Name));
+    }
     else
+    {
         ChipLogProgress(DeviceLayer, "NVS erase: %s/%s", StringOrNullMarker(key.Namespace), StringOrNullMarker(key.Name));
+    }
 
     return CHIP_NO_ERROR;
 }
diff --git a/src/platform/Darwin/Logging.h b/src/platform/Darwin/Logging.h
index 7fcb82a..30f6c96 100644
--- a/src/platform/Darwin/Logging.h
+++ b/src/platform/Darwin/Logging.h
@@ -34,7 +34,7 @@
         _Pragma("clang diagnostic ignored \"-Wformat\"");                                                                          \
         os_log_with_type(chip::Logging::Platform::LoggerForModule(chip::Logging::kLogModule_##MOD, #MOD),                          \
                          static_cast<os_log_type_t>(chip::Logging::Platform::kOSLogCategory_##CAT), MSG, ##__VA_ARGS__);           \
-        ChipInternalLogImpl(MOD, CAT, MSG, ##__VA_ARGS__);                                                                         \
+        ChipInternalLogImpl(MOD, CHIP_LOG_CATEGORY_##CAT, MSG, ##__VA_ARGS__);                                                     \
         _Pragma("clang diagnostic pop");                                                                                           \
     } while (0)
 
@@ -43,7 +43,7 @@
     {                                                                                                                              \
         chip::Logging::Platform::LogByteSpan(chip::Logging::kLogModule_##MOD, #MOD,                                                \
                                              static_cast<os_log_type_t>(chip::Logging::Platform::kOSLogCategory_##CAT), DATA);     \
-        ChipInternalLogByteSpanImpl(MOD, CAT, DATA);                                                                               \
+        ChipInternalLogByteSpanImpl(MOD, CHIP_LOG_CATEGORY_##CAT, DATA);                                                           \
     } while (0)
 
 namespace chip {
@@ -59,10 +59,10 @@
 // Names align with chip::Logging::LogCategory
 enum OSLogCategory
 {
-    kOSLogCategory_Error      = OS_LOG_TYPE_ERROR,
-    kOSLogCategory_Progress   = OS_LOG_TYPE_DEFAULT,
-    kOSLogCategory_Detail     = OS_LOG_TYPE_INFO,
-    kOSLogCategory_Automation = OS_LOG_TYPE_DEFAULT,
+    kOSLogCategory_ERROR      = OS_LOG_TYPE_ERROR,
+    kOSLogCategory_PROGRESS   = OS_LOG_TYPE_DEFAULT,
+    kOSLogCategory_DETAIL     = OS_LOG_TYPE_INFO,
+    kOSLogCategory_AUTOMATION = OS_LOG_TYPE_DEFAULT,
 };
 
 DLL_LOCAL os_log_t LoggerForModule(chip::Logging::LogModule moduleId, char const * moduleName);
diff --git a/src/platform/Zephyr/Logging.cpp b/src/platform/Zephyr/Logging.cpp
index f8e2015..4f73225 100644
--- a/src/platform/Zephyr/Logging.cpp
+++ b/src/platform/Zephyr/Logging.cpp
@@ -19,7 +19,8 @@
 #define LOG_MESSAGE(msg) (msg)
 #endif
 
-LOG_MODULE_REGISTER(chip, CONFIG_MATTER_LOG_LEVEL);
+/* Assume using always debug level and rely on Matter logger layer filtering */
+LOG_MODULE_REGISTER(chip, LOG_LEVEL_DBG);
 
 namespace chip {
 namespace DeviceLayer {
diff --git a/src/platform/nrfconnect/CHIPPlatformConfig.h b/src/platform/nrfconnect/CHIPPlatformConfig.h
index 32ae780..c4b975b 100644
--- a/src/platform/nrfconnect/CHIPPlatformConfig.h
+++ b/src/platform/nrfconnect/CHIPPlatformConfig.h
@@ -69,3 +69,29 @@
 #ifndef CHIP_CONFIG_MAX_FABRICS
 #define CHIP_CONFIG_MAX_FABRICS 5
 #endif
+
+#if CONFIG_CHIP_LOG_SIZE_OPTIMIZATION
+// Disable some of the too detailed log modules to save flash
+#define CHIP_CONFIG_LOG_MODULE_ExchangeManager_DETAIL 0
+#define CHIP_CONFIG_LOG_MODULE_Crypto_DETAIL 0
+#define CHIP_CONFIG_LOG_MODULE_Crypto_PROGRESS 0
+#define CHIP_CONFIG_LOG_MODULE_BDX_DETAIL 0
+#define CHIP_CONFIG_LOG_MODULE_BDX_PROGRESS 0
+#define CHIP_CONFIG_LOG_MODULE_EventLogging_DETAIL 0
+#define CHIP_CONFIG_LOG_MODULE_EventLogging_PROGRESS 0
+#define CHIP_CONFIG_LOG_MODULE_SetupPayload_DETAIL 0
+#define CHIP_CONFIG_LOG_MODULE_SetupPayload_PROGRESS 0
+#define CHIP_CONFIG_LOG_MODULE_CASESessionManager_DETAIL 0
+#define CHIP_CONFIG_LOG_MODULE_CASESessionManager_PROGRESS 0
+#define CHIP_CONFIG_LOG_MODULE_DataManagement_DETAIL 0
+#define CHIP_CONFIG_LOG_MODULE_FabricProvisioning_DETAIL 0
+#define CHIP_CONFIG_LOG_MODULE_chipSystemLayer_DETAIL 0
+#define CHIP_CONFIG_LOG_MODULE_chipSystemLayer_PROGRESS 0
+#define CHIP_CONFIG_LOG_MODULE_Zcl_DETAIL 0
+#define CHIP_CONFIG_LOG_MODULE_SecureChannel_DETAIL 0
+#define CHIP_CONFIG_LOG_MODULE_Ble_DETAIL 0
+#define CHIP_CONFIG_LOG_MODULE_AppServer_DETAIL 0
+#define CHIP_CONFIG_LOG_MODULE_Support_DETAIL 0
+#define CHIP_CONFIG_LOG_MODULE_Support_PROGRESS 0
+#define CHIP_CONFIG_LOG_MODULE_DeviceLayer_DETAIL 0
+#endif