Logify/README.md

194 lines
8.1 KiB
Markdown
Raw Permalink Normal View History

2025-08-21 11:59:58 -03:00
# Logify
2025-08-21 12:11:26 -03:00
**Logify** is a logging library for MQL designed to simplify debugging, tracking, and monitoring of EAs and indicators. It provides structured, customizable, and organized logs directly on the chart or in the terminal, with support for log levels, flexible formats, and multiple handlers. A lightweight, elegant solution that is easy to integrate into your MQL projects.
2025-08-21 11:59:58 -03:00
2025-08-21 12:11:26 -03:00
## 📦 Features
2025-08-21 11:59:58 -03:00
2025-08-21 12:11:26 -03:00
- Multiple log levels: DEBUG, INFO, ALERT, ERROR, FATAL
- Display logs directly on graph, terminal, files or even database
- Customizable log format and layout
- Modular architecture with multiple handlers
- Lightweight and easy to integrate and use
2025-08-21 11:59:58 -03:00
2025-08-21 12:11:26 -03:00
## 🚀 Installation
2025-08-21 11:59:58 -03:00
2025-08-27 09:34:46 -03:00
You can install Logify in two ways:
### Option 1: Git Clone (Recommended)
3. Clone the Logify repository in the terminal folder (Usually at `C:\Users\YOUR_USER\AppData\Roaming\MetaQuotes\Terminal\YOUR_ID\MQL5`):
```shell
git clone https://forge.mql5.io/joaopedrodev/Logify
```
4. After cloning, you will have a new `MQL5/Logify` folder with the following structure:
```
MQL5/Logify/
├── Experts/ # Contains EA examples
└── Include/ # Contains the Logify library
```
5. Copy the files to their respective folders:
- Copy the content from `MQL5/Logify/Experts``MQL5/Experts`
- Copy the content from `MQL5/Logify/Include``MQL5/Include`
6. Done! The library is installed and the examples are ready to use.
### Option 2: Manual Download
1. Download the code and copy the downloaded content to their respective folders:
- Copy the content from `59821/mql5/Logify/Experts``MQL5/Experts`
- Copy the content from `59821/mql5/Logify/Include``MQL5/Include`
### Including in your project
After installation (by any of the options above), include Logify in your EA, indicator or script:
2025-08-21 11:59:58 -03:00
2025-08-21 12:11:26 -03:00
``` c++
#include <Logify/Logify.mqh>
2025-08-21 11:59:58 -03:00
```
2025-08-21 12:11:26 -03:00
## 🔧 Quick Start Example
Simple example with default settings:
2025-08-21 11:59:58 -03:00
``` c++
2025-08-21 12:11:26 -03:00
//+------------------------------------------------------------------+
//| Import |
//+------------------------------------------------------------------+
2025-08-21 11:59:58 -03:00
#include <Logify/Logify.mqh>
2025-08-21 12:11:26 -03:00
CLogify Logify;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
2025-08-21 11:59:58 -03:00
int OnInit()
{
2025-08-21 12:11:26 -03:00
//--- Example logs
Logify.Debug("Initialization started");
Logify.Info("Account balance is OK");
Logify.Alert("Take profit reached");
Logify.Error("Failed to send order", "Order", "Reason: No money");
Logify.Fatal("Critical error: Invalid input parameters");
//---
2025-08-21 11:59:58 -03:00
return(INIT_SUCCEEDED);
}
2025-08-21 12:11:26 -03:00
//+------------------------------------------------------------------+
2025-08-21 11:59:58 -03:00
```
2025-08-21 12:11:26 -03:00
Advanced example, with custom handler settings. In this example, we save the log record to files and in the graph comment. Customizing the settings for each of them.
2025-08-21 11:59:58 -03:00
``` c++
2025-08-21 12:11:26 -03:00
//+------------------------------------------------------------------+
//| Import |
//+------------------------------------------------------------------+
2025-08-21 11:59:58 -03:00
#include <Logify/Logify.mqh>
2025-08-21 12:11:26 -03:00
CLogify Logify;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
2025-08-21 11:59:58 -03:00
int OnInit()
{
2025-08-21 12:11:26 -03:00
//--- Configure comment handler
MqlLogifyHandleCommentConfig config_comment;
config_comment.size = 10;
config_comment.frame_style = LOG_FRAME_STYLE_SINGLE;
config_comment.direction = LOG_DIRECTION_UP;
config_comment.title = "My Expert";
//--- Create and configure comment handler
CLogifyHandlerComment *handler_comment = new CLogifyHandlerComment();
handler_comment.SetConfig(config_comment);
handler_comment.SetLevel(LOG_LEVEL_DEBUG);
handler_comment.SetFormatter(new CLogifyFormatter("{date_time} [{levelname}]: {msg}","hh:mm:ss"));
2025-08-21 11:59:58 -03:00
2025-08-21 12:11:26 -03:00
//--- Configure file handler
MqlLogifyHandleFileConfig file_config;
file_config.CreateDateRotationConfig("my_expert","logs",LOG_FILE_EXTENSION_LOG,10,100,CP_UTF8);
//--- Create and configure file handler
2025-08-21 11:59:58 -03:00
CLogifyHandlerFile *handler_file = new CLogifyHandlerFile();
2025-08-21 12:11:26 -03:00
handler_file.SetConfig(file_config);
2025-08-21 11:59:58 -03:00
handler_file.SetLevel(LOG_LEVEL_DEBUG);
2025-08-21 12:11:26 -03:00
handler_file.SetFormatter(new CLogifyFormatter("{date_time} [{levelname}]: {msg} ({filename} | {origin} | {function})","hh:mm:ss"));
2025-08-21 11:59:58 -03:00
2025-08-21 12:11:26 -03:00
//--- Attach handler
Logify.AddHandler(handler_comment);
Logify.AddHandler(handler_file);
//--- Example logs
Logify.Debug("Initialization started");
Logify.Info("Account balance is OK");
Logify.Alert("Take profit reached");
Logify.Error("Failed to send order", "Order", "Reason: No money");
Logify.Fatal("Critical error: Invalid input parameters");
2025-08-21 11:59:58 -03:00
2025-08-21 12:11:26 -03:00
//---
2025-08-21 11:59:58 -03:00
return(INIT_SUCCEEDED);
}
2025-08-21 12:11:26 -03:00
//+------------------------------------------------------------------+
2025-08-21 11:59:58 -03:00
```
2025-08-21 12:11:26 -03:00
To understand each of the settings, I recommend reading the articles below, where I explain each step of the library development:
2025-08-21 11:59:58 -03:00
2025-08-21 12:11:26 -03:00
- [Mastering Log Records (Part 1): Fundamental Concepts and First Steps in MQL5](https://www.mql5.com/en/articles/16447)
- [Mastering Log Records (Part 2): Formatting Logs](https://www.mql5.com/en/articles/16833)
- [Mastering Log Records (Part 3): Exploring Handlers to Save Logs](https://www.mql5.com/en/articles/16866)
- [Mastering Log Records (Part 4): Saving Logs to Files](https://www.mql5.com/en/articles/16986)
- [Mastering Log Records (Part 5): Optimizing the Handler with Cache and Rotation](https://www.mql5.com/en/articles/17137)
- [Mastering Log Records (Part 6): Saving Logs to Database](https://www.mql5.com/en/articles/17709)
- [Mastering Log Records (Part 7): How to Show Logs on Chart](https://www.mql5.com/en/articles/18291)
- [Mastering Log Records (Part 8): Error Records That Translate Themselves](https://www.mql5.com/en/articles/18467)
- [Mastering Log Records (Part 9): Implementing the builder pattern and adding default configurations](https://www.mql5.com/en/articles/18602)
- [Mastering Log Records (Part 10): Avoiding Log Replay by Implementing a Suppression](https://www.mql5.com/en/articles/19014)
2025-08-21 11:59:58 -03:00
2025-08-21 12:11:26 -03:00
## ✔️ Log Levels
2025-08-21 11:59:58 -03:00
2025-08-21 12:11:26 -03:00
| Level | Description |
|---------------|-------------------------------------------|
| DEBUG | Verbose information for debug |
| INFO | General information |
| WARNING | Warnings or important events |
| ERROR | Errors that require attention |
| FATAL | Critical errors, stop execution |
2025-08-21 11:59:58 -03:00
2025-08-21 12:11:26 -03:00
## 🖥️ Handlers Included
2025-08-21 11:59:58 -03:00
2025-08-21 12:11:26 -03:00
Each handler defines where the logs will be displayed or stored.
| Handler | Description |
|-----------|-----------------------------------------------|
| Comment | Display logs directly on the graph (Comment) |
| Console | Show logs in MetaTrader terminal |
| File | Save logs to .txt or .log files |
| Database | Stores logs in local SQLite database |
You can use one or combine several handlers at the same time, like graphic + file + console, for example.
## 🛠️ Log Format
Example of formatting pattern:
``` c++
"{date_time} [{levelname}]: {msg}"
```
Available tokens:
2025-08-21 11:59:58 -03:00
2025-08-21 12:11:26 -03:00
- {levelname}: Level name (DEBUG, INFO, ERROR, etc.)
- {msg}: Main message
- {args}: Additional arguments or details
- {timestamp}: Timestamp in seconds (Unix Time)
- {date_time}: Formatted date and time (hh:mm:ss, etc.)
- {level}: Numeric code for the level (0 = DEBUG, 1 = INFO...)
- {origin}: Origin or context defined in the log call
- {filename}: Source file name (if available)
- {function}: Name of the function from which it was called
- {line}: Line number in the code where the log occurred
2025-08-21 11:59:58 -03:00
2025-08-21 12:11:26 -03:00
## ⚖️ License
2025-08-21 11:59:58 -03:00
2025-08-21 12:11:26 -03:00
MIT License — Free to use for personal and commercial projects.
2025-08-21 11:59:58 -03:00
2025-08-21 12:11:26 -03:00
## 👨‍💻 Author
2025-08-21 11:59:58 -03:00
2025-08-21 12:11:26 -03:00
Developed by [joaopedrodev](https://www.mql5.com/en/users/joaopedrodev), with a focus on making MQL development more professional, organized and efficient.