• Overview
  • Reviews
  • Comments (1)

Log4MQL logger

For full product description go to Comments tab.

MetaTrader and its MQL4/5 language is great for developing of automated trading systems. But I was missing some support for logging of custom messages and information during run of some trading system, custom scripts or indicators. One of the possibilities is to use Print function and then read messages in Journal window. Disadvantages of this Journal window are:

  • it is slowly refreshed when you print out many messages
  • it cannot be searched
  • it is deleted after exit of MetaTrader
  • you can not add priorities to you messages and use some highlighting features
That's the reason why I developed this logger for MQL language. Main features and benefits of this logger are:
  • message logging to custom external file that can be archived, sent by email, read by some scripts etc.
  • four message levels DEBUG, INFO, WARN, ERROR
  • log files can be displayed by some third party log parsers and use its highlighting features
  • fast writing to output log files, log messages are immediately written to log files (no useless lags)
  • simple API, simple include to your code and of course simple use


How to use it?

1) Download and install Log4mql

Download and install Log4mql from the Market.

2) Include / import Log4mql to your MQL code

The Log4mql.mqh file contains specification of logger public interface, its methods, class constructor and destructor. Logger itself is implemented in the file Log4mqlImpl.ex4

File Log4mql.mqh needs to be downloaded or manually created (check comments tab for more details)

Code snippet below shows how to include logger to your code

#include "../libraries/Log4mql.mqh"

#import "../libraries/Log4mqlImpl.ex4"

Log4mql *Log4mqlImpl();

Log4mql *Log4mqlImpl(string filename, string level, string delimiter);

#import

3) Declaration and initialization of logger

Instance of logger class can be obtained this way

Log4mql *logger = Log4mqlImpl("test.log","DEBUG",";");

//or you can define only logger variable on some global level and make its initialization later

Log4mql *logger; // global variable

.

.

logger = Log4mqlImpl("test.log","DEBUG",";");

4) Log everything you want ...

logger.debug("This is DEBUG message");

logger.info("This is INFO message");

logger.warn("This is WARN message");

logger.error("This is ERROR message");

Logger levels have these priorities

  • ERROR=2
  • WARN=4
  • INFO=8
  • DEBUG=16

Logger will log all messages with level equal or lower to logger level. When you set logger level to WARN only WARN and ERROR messages will be logged. If you set logger level to INFO only INFO, WARN and ERROR messages will be logged. etc.

All output files will be stored in FILES folder under MetaTrader installation folder for example "[MetaTrader home]\MQL4\Files\"


Examples

1) Logger will log to file example.log, logging level is set to DEBUG and message elements will be delimited by semicolon

#include "../libraries/Log4mql.mqh"

#import "../libraries/Log4mqlImpl.ex4"

Log4mql *Log4mqlImpl();

Log4mql *Log4mqlImpl(string filename, string level, string delimiter);

#import


//+------------------------------------------------------------------+

//| Script program start function |

//+------------------------------------------------------------------+

void OnStart()

{

Log4mql *logger = Log4mqlImpl("example.log","DEBUG",";");

logger.info("Script has been started");

logger.warn("Message with some WARNING");

logger.error("Error during execution of script");

}

//+------------------------------------------------------------------+

Content of example.log

2015.07.08 23:14:23;logger_test_4;INFO;Script has been started

2015.07.08 23:14:23;logger_test_4;WARN;Message with some WARNING

2015.07.08 23:14:23;logger_test_4;ERROR;Error during execution of script


Log file monitoring / highlighting

Log files can be monitored online using some log parsers. This tools can highlight different levels of log messages, can execute external scripts (for example send email when ERROR log message is logged).

No reviews