Logging V2 for MQL4 and MQL5 - MetaTrader 5 Library - MT4/MT5 Resources
The debug logger class is a powerful and flexible logging utility designed specifically for the MQL4/5 environment. It is an essential tool for developers who need to accurately monitor, debug, and trace application behavior.
In this new version of the CDebugLogger class , I have introduced several improvements to enhance its functionality and versatility. These improvements include debounce mechanisms to prevent excessive logging in event-driven systems such as OnTick, OnTimer, and OnChartEvent, as well as new filtering and muting options to help developers focus on the most relevant log entries.
I decided to release this updated version as a separate codebase so that users can freely choose the implementation that best suits their needs. Whether you prefer the original version or this enhanced version, you can now choose a logging tool that fits your workflow and project requirements.
Below, we'll explore the main features and functionality of this enhancement class.
Here is an example of how to initialize and use the CDebugLogger class:
// Initialize the logger using INFO level logging to file CDebugLogger logger(info, true , "log.txt" , true , TIME_DATE | TIME_MINUTES , false , true , true , true ); // Log a simple message logger.Log(info, "This is an information message" ); // Silence keyword logger.AddSilentKeyword( "password" ); // Log a message that will be silenced logger.Log(info, "User entered password: 1234" ); // Enable file logging logger.Enable file logging ( True , "Debug.Log" , False ); // Remove the silent keyword logger.RemoveSilentKeyword( "password" ); // Log a message after removing the keyword from the silent list Logger.Log(info, "User entered password: 1234" ); // Add keyword filter logger.AddFilterKeyword( "Success" ); // Log a message that will be filtered out . Logger.Log(Information, "Operation failed" ); // Record a message that will pass the filter . Logger.Log(Information, "Operation successful" ); // Remove the keyword from the filter logger.RemoveFilterKeyword( "Success" ); // Use the general Log function for initialization logging.Initialization(Warning, true , "Warning.Log" , true , TIME_SECONDS , true , false , true , true ); // Use the general Log function to record warning logs (warning, "This is a warning message" );
Script example
To use the CDebugLogger class in a script, simply include the necessary libraries at the beginning of the file as follows:
#include <logging.mqh> //+--------------------------------------------------------------------------------+ //|Expert initialization function | //+------------------------------------------------------------------+ Integer initialization () { integer logoptions = line|FUNCSIG; // filename|line|FUNCSIG; or 0 logging.initialize(info, false , "example_log.txt" , true , TIME_DATE | TIME_MINUTES | TIME_SECONDS , false , logoptions, false ); //--- create timer event set millisecond timer ( 1 ); //--- return ( initialization successful ); } //+------------------------------------------------------------------+ //|Expert to initialize function | //+------------------------------------------------------------------+ Blank solution initialization ( constant integer reason) { //--- Destroy timer event to terminate timer (); } Integer counter = 0 ; Date time last time = 0 ; //--- Store counter last update time //+------------------------------------------------------------------+ //|Timer function | //+------------------------------------------------------------------+ blank timer () { logging.BeginEvent(); //--- Start a new event log(message, "Message Example" ); log(message, "another message" ); log(info, "MessageExample" ); //--- Get the current time date time current time = time local (); //--- Check if at least 2 seconds have passed since the last time the counter was updated if (current time - last time >= 2 ) { //--- update counter counter++; //--- Last update last time = current time; //--- Log a message with the new counter value log(message, "Counter value:" + integer to string (counter)); //--- You can also log another message log(message, "Update in 2 seconds" ); } else { //--- log a message indicating that the timer is active but the counter has not changed log(info, "Timer is active but counter has not changed" ); } }
Output MT5 terminal example:
2024.09 . 22 13 : 00 : 29.589 test_logging(EURUSD,H1) 2024.09 . 22 13:00:29 Line : 43 Function : BlankTimer () [Message] Sample message 2024.09 . 22 13 : 00 : 29.589 test_logging(EURUSD,H1) 2024.09 . 22 13:00:29 Line : 44 Function : BlankTimer () [INFO] Another message 2024.09 . 22 13 : 00 : 29.589 test_logging(EURUSD,H1) 2024.09 . 22 13:00:29 Line : 45 Function : BlankTimer () [Message] Sample message 2024.09 . 22 13 : 00 : 29.589 test_logging(EURUSD,H1) 2024.09 . 22 13 : 00 : 29 Line: 55 Function : BlankTimer ( ) [Message] Counter Value: 1 2024.09 . 22 13 : 00 : 29.589 test_logging(EURUSD,H1) 2024.09 . 22 13:00:29 Line : 57 Function : BlankTimer () [INFO] Updated 2 seconds after 2024.09 . 22 13 : 00 : 29.605 test_logging(EURUSD,H1) 2024.09 . 22 13:00:29 Line : 60 Function: BlankTimer () [INFO] The timer has been activated but the counter has not changed 2024.09 . 22 13 : 00 : 31.001 test_logging(EURUSD,H1) 2024.09 . 22 13:00:31 Line : 55 Function : BlankTimer ( ) [Message] Counter Value : 2 2024.09 . 22 13 : 00 : 31.001 test_logging(EURUSD,H1) 2024.09 . 22 13:00:31 Line : 57 Function : BlankTimer () [INFO] Updated 2 seconds after 2024.09 . 22 13 : 00 : 31.017 test_logging(EURUSD,H1) 2024.09 . 22 13:00:31 Line : 60 Function: BlankTimer () [INFO] The timer has been activated but the counter has not changed 2024.09 . 22 13 : 00 : 33.001 test_logging(EURUSD,H1) 2024.09 . 22 13:00:33 Line : 55 Function: BlankTimer () [Message] Counter value : 3 2024.09 . 22 13 : 00 : 33.001 test_logging(EURUSD,H1) 2024.09 . 22 13:00:33 Line : 57 Function : BlankTimer () [INFO] Updated 2 seconds after 2024.09 . 22 13 : 00 : 33.016 test_logging(EURUSD,H1) 2024.09 . 22 13:00:33 Line : 60 Function : BlankTimer ( ) [INFO] The timer has been activated but the counter has not changed
This debug logger class is an invaluable tool for any MQL4/5 developer. With its wide range of customizable features, it accurately logs and monitors applications, promoting easier debugging and better application performance tracking. Whether you need simple message logging or detailed contextual information, the CDebugLogger class provides a reliable and efficient solution for your development needs.
For more information about the CDebugLogger class or to explore other advanced tools and solutions, please visit Baofeng Technology .
Attachment download
📎 logging.mqh (52.65 KB)
📎 test_logging.mq5 (6.25 KB)
📎 logging.mq5 (9.08 KB)
Source: MQL5 #52298
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •