The simplest logger class for MetaTrader 5 - MetaTrader 5 Library - MT4/MT5 Resources


Each programmer has his or her own logger. Inspired by the Python logging module, I wrote my own code for MQL5.
This course is the easiest. There are no hierarchies, rotators or formatters. It's simple and convenient for any project.
#includeUsage
CDKLogger logger; // Step 1. Initialize the Logger logger with "MyLoggerName" name and INFO level.Init ( "My Logger Name" , INFO); // You can change the default logger format "%name%:[%level%] %message%" to your own // Combine loggers using any pattern.Format = "%YYYY%-%MM%-%DD% %hh%:%mm%-%ss% - %name%:[%level%] %message%" ; // Step 2. If you want to filter messages using only clauses, // Fill in FilterInList // 2.1. Add substings to FilterIntList logger.FilterInList.Add( "Includes substring #1" ); logger.FilterInList.Add( "Includes substring #2" ); // 2.2. Split the string delimiter with ";" Add all substrings to FilterInList in one line logger.FilterInFromStringWithSep( "Includes-substring-#3; includes-substring-#4" , ";" ); // Step 3. If you want to filter OUT messages with substrings but keep all other messages, // Populate FilterOutList // 3.1. Add substring to FilterOutList logger.FilterOutList.Add( "exclude-substring-#1" ); logger.FilterOutList.Add( "Exclude-Substring-#2" ); // 3.2. Use ";" to split the string delimiter and add all substrings to the FilterOutList in one line logger.FilterOutFromStringWithSep( "Exclude-Substring-#3; Exclude-Substring-#4" , ";" ); // Use Filter to enter your filter str sep by ;here // Step 4. Logger.debug( "DEBUG: includes substring #1" , false ); // Debug logger without alert.info ( "INFO: includes substring #1" , true ); // Infologger with alert dialog.WARNING ( "WARNING: includes substring #1" ); Logger.Error( "Error: Include substring #1: Exclude substring #1" ); // Being skipped due to FilterOutList Logger.Critical( "Critical: Include substring #1" ); logger.assert( true , "log message if true" , info, // if possible "log message if error" , error, // if failed true ); // also show alertlogger.assert ( true , "True and false information are the same" , Information, // If normal, log level error, // If failed, log level error ); // No alarm
The execution result will output the following message in the log:
I often use classes like this:
logger.debug( string format ( "%s/%d:mymessage:PARAM1=%f" , __function__ , __line__ , my_parameters));
But here we have a problem. The StringFormat function parses the string every time, even if the logging level does not require a message to be output.
If you need to output a lot of debug messages, you have to wrap the output in a conditional:
if (debug >= logger.level) logger.debug( string format ( "%s/%d:mymessage:PARAM1=%f" , __function__ , __line__ , my_parameters));
The best way is to use StringFormat lazily, but unfortunately MQL5 does not support passing a dynamic number of function parameters for debugging, messages, errors, etc.
If you have any ideas on how to do this, I'd love to hear it.
Attachment download
📎 cdklogger.mqh (26.27 KB)
📎 cdklogger_example.mq5 (5.73 KB)
Source: MQL5 #52741