Logging Library
Print() has no concept of level, timestamp or source, so a chart running two or three EAs at once interleaves everything into a single undifferentiated stream, and the only way to quiet routine debug noise is to go back and delete the Print() calls that produce it - which then have to be re-added the next time something breaks. This header adds five levels (DEBUG, INFO, WARNING, ERROR, CRITICAL), a minimum-level floor that suppresses everything below it, and an optional tag prefixed to every line so output from several programs sharing a chart can still be told apart. Every line is timestamped with the server clock (TimeCurrent, not TimeLocal), so logs from EAs on different symbols with different broker GMT offsets stay directly comparable. The level check happens before the message string is built, not after. Constructing a formatted string is not free, and an EA logging at DEBUG on every tick while the floor is set to WARNING should not pay that cost for a result that gets thrown away - the filter is the first line in the write path, not the last. File output is opt-in and append-only. Opening a file with FILE_WRITE alone truncates it, which would silently erase a previous session's log the moment the EA restarts - a genuinely dangerous default for anything meant to survive a terminal restart. This header opens the file with FILE_READ | FILE_WRITE and seeks to the end explicitly, so calling LoggerEnableFileOutput again after a restart continues the same file instead of wiping it. Notes and limitations - File output uses the sandboxed Files folder (FILE_COMMON is not used), so each terminal instance gets its own copy - correct for a single EA on a single terminal, not designed for multiple terminals sharing one log. - The tag and minimum level are global to the compiled program, not scoped per file handle - a single program logging to two different files at two different levels would need to call LoggerSetMinLevel between writes, or keep its own second file handle outside this header. - Timestamps use TimeCurrent() (server time), deliberately, so that logs from EAs on different symbols with different broker GMT offsets stay directly comparable to each other and to trade timestamps elsewhere in the terminal. - FileFlush() is called after every file write so a crash does not lose the last few lines, at the cost of some I/O overhead on very high-frequency logging - if that overhead matters, log at a higher minimum level rather than removing the flush.
Script · MT5 · v1.0 · Free
About Logging Library
Logging Library is a script for MetaTrader 5. It has 1 setting, such as Log file name. MQL source code included.
Files in the download: Logger.mq5.mqh, Logger.txt, LoggerDemo.ex5, LoggerDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Log file name | LoggerDemoTest.log | Sandbox file used to prove file output/append works |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT5 Script · MQL5 Source Code