Watch how to download trading robots for free
Find us on Telegram!
Join our fan page
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal
Libraries

Simplest Logger class for MetaTrader 5 - library for MetaTrader 5

Views:
525
Rating:
(3)
Published:
2024.10.13 21:54
Updated:
2024.10.13 22:10
\MQL5\Include\DKStdLib\Logger\
CDKLogger.mqh (26.27 KB) view
\MQL5\Scripts\
MQL5 Freelance Need a robot or indicator based on this code? Order it on Freelance Go to Freelance

Every programmer has their own logger. I wrote my own for MQL5, inspired by the Python logging module.

This class is simplest. No hierarchy, rotators or formatters. It's simple and convenient for any project.

Installation

  1. Copy CDKLogger.mqh to MQL\Include\DKStdLib\Logger folder.
  2. Import CDKLogger class.

#include <DKStdLib\Logger\CDKLogger.mqh

Usage

CDKLogger logger;

// STEP 1. Init Logger with "MyLoggerName" name and INFO level
logger.Init("MyLoggerName", INFO);

// You can change default logger format "%name%:[%level%] %message%" to your own
// Use any pattern combination 
logger.Format = "%YYYY%-%MM%-%DD% %hh%:%mm%-%ss% - %name%:[%level%] %message%"; 

// STEP 2. If you like to filter message only with substings,
//         fill the FilterInList 
// 2.1. Add a substings to FilterIntList
logger.FilterInList.Add("Including-Substring-#1");        
logger.FilterInList.Add("Including-Substring-#2");        

// 2.2. Split string by ";" separator to add all substrings to FilterInList in one line
logger.FilterInFromStringWithSep("Including-Substring-#3;Including-Substring-#4", ";");  

// STEP 3. If you like to filter OUT message with substrings, but leave all others,
//         fill the FilterOutList 
// 3.1. Add a substrings to FilterOutList
logger.FilterOutList.Add("Excluding-Substring-#1");        
logger.FilterOutList.Add("Excluding-Substring-#2");        

// 3.2. Split string by ";" separator to add all substrings to FilterOutList in one line
logger.FilterOutFromStringWithSep("Excluding-Substring-#3;Excluding-Substring-#4", ";");  // use Filter In put your filter str sep by ; here

// STEP 4. Logging
logger.Debug("Debug: Including-Substring-#1", false);                  // Debug with no Alert
logger.Info("Info: Including-Substring-#1", true);                     // Info with Alert dialog
logger.Warn("Warn: Including-Substring-#1"); 
logger.Error("Error: Including-Substring-#1: Excluding-Substring-#1"); // Skipped because of FilterOutList
logger.Critical("Critical: Including-Substring-#1");

logger.Assert(true, 
              "Log msg if true", INFO,   // if ok
              "Log msg if false", ERROR, // if fails
              true);                     // Show Alert as well
logger.Assert(true, 
              "Same msg for true & false", 
              INFO,   // Log level if ok
              ERROR,  // Log level if fails
              false); // No Alert   

The following messages will be output to the log as a result of execution:

Open issues

I often use class this way:

logger.Debug(StringFormat("%s/%d: My message: PARAM1=%f",
                          __FUNCTION__, __LINE__,
                          my_param));

But here we have an issue. StringFormat function parse string every time, even if the logging level does not require the message to be output.

If you need to output debug messages a lot, you'll have to wrap the output in a condition:

if(DEBUG >= logger.Level)                          
  logger.Debug(StringFormat("%s/%d: My message: PARAM1=%f",
                            __FUNCTION__, __LINE__,
                            my_param));      

The best way to do this would be to use StringFormat lazily, but unfortunately MQL5 doesn't support passing a dynamic number of function parameters to the Debug, Info, Error, and so on functions.

If you have any ideas on how this could be done, I'd love to hear them.

TimeServerDaylightSavings TimeServerDaylightSavings

Time-related functions for empirical detection of server time zone and daylight savings mode (DST) from history of quotes

Custom crosshair cursor with synchronization Custom crosshair cursor with synchronization

Synchronized custom crosshair indicator showing price and (server/local) time.

Script to extract Candlesticks data from all time frames to CSV. Script to extract Candlesticks data from all time frames to CSV.

This MQL5 script exports candlestick data for various timeframes into a CSV file, capturing essential market information like open, high, low, and close prices. It analyzes each candlestick's characteristics, including body and wick sizes, while calculating additional metrics such as candle gaps. After processing the latest 21 bars, it notifies the user upon successful data export.

Push Notification for Opened / Closed Trades (Netting) Push Notification for Opened / Closed Trades (Netting)

This code provides a simple function for sending push notifications to your mobile device whenever trades are opened or closed in MetaTrader 5. It's designed for netting accounts (where only one position per symbol is allowed).