MultiLog4D is a library designed to facilitate and speed up the sending of logs to Android, iOS, Windows, macOS and Linux. With just one line of code, it is possible to send a message that will be seen and monitored on the corresponding platform, such as adb logcat on Android or
syslog on Linux, for example.
Just download the sources from GitHub, unzip them in a folder of your choice and point to this folder in your project's Search Path or, if you prefer, you can use Boss (Delphi's dependency manager) to perform the installation:
boss install github.com/adrianosantostreina/MultiLog4D
There are several ways to use MultiLog4D, we will detail them all below, but the one I like the most is to use the TMultiLog4DUtil class present in the MultiLog4D.Util.pas unit. It is a Singleton class that can be called from any part of your Delphi project.
Declare the unit in the uses clause of your form and call the line below:
uses
MultiLog4D.Util;
procedure TForm1.Button1Click(Sender: TObject);
begin
TMultiLog4DUtil
.Logger
.Tag('MultiLog4D')
.LogWriteInformation('Any log here...')
end;
An important note is that the TAG must be provided for Android and iOS, otherwise you will not be able to filter the logs in the Windows Terminal for Android applications and in the macOS Console for iOS applications. MultiLog4D will not validate whether the tag was entered or not, so you need to remember to call the method. If you do not provide a TAG, MultiLog4D will set the default TAG with the name "MultiLog4D".
The TAG will be used to filter all messages from your application in the Terminal when monitoring is requested:
Using any Terminal window on Windows, you basically need to use adb with the logcat command to view the logs.
Example:
adb logcat <MyTAG>:D *:S
Replace with the tag entered in MultiLog4D, for example:
adb logcat MyAppAndroid:D *:S
✍️ Note: Your Android device must be in Developer Mode, with USB debugging enabled. And if you have more than one device connected to the USB port, you will need the UUID of the device that will monitor the logs. Use the following command to view the UUIDs.
adb devices
This command will show all UUIDs of all devices connected to the USB. Then filter the log using the following command:
adb -s <UUID> logcat MyAppAndroid:D *:S
Replace with the UUID of your device.
On iOS, monitoring the logs must be done through the Console application on macOS. Search for the Console application in the macOS search. When you open the application, the iPhone/iPad device you are using to test your app will appear in the sidebar, just click on it and that's it, the logs for that device will appear in the window.
On Windows we can send logs to the Console, Event Viewer and to a file. For this there is a method to be configured, the Output. It has the following variations:
TMultiLog4DUtil
.Logger
.Output([loConsole, loFile, loEventViewer])
.LogWriteInformation('Inicializando...');
As you can see, it is an array of options and you configure it as you wish.
- Filename
You can configure the folder and the name of the log file that will be generated, otherwise MultiLog4D will automatically create a log directory and a file with a default name. To configure this, simply call the method:
TMultiLog4DUtil
.Logger
.FileName('C:\MyLogs\ExampleLog')
.LogWriteInformation('Initializing...');
The library will append the date and file extension.
ExampleLog_20241001_010720.log
i.e. YYYYDDMM hhmmss.log
- SetLogFormat
You can format the log output:
Default: ${time} ${username} ${eventid} [${log_type}] - ${message}
Possible values: category
TMultiLog4DUtil
.Logger
.SetLogFormat('${time} ${username} ${eventid} [${log_type}] - ${message}')
.LogWriteInformation('Initializing...');
We are evaluating other information that may be part of the log. If you have any suggestions, please send them through ISSUES.
- SetDateTimeFormat
You can customize the DateTime format.
TMultiLog4DUtil
.Logger
.SetDateTimeFormat('YYYY-DD-MM hh:mm:ss')
.LogWriteInformation('Initializing...');
- Category
You can customize the log category to better find errors and information in your project. The category options are provided in the TEventCategory class in the MultiLog4D.Types file.
Possible values are:
TMultiLog4DUtil
.Logger
.Category(ecApplication)
.LogWriteInformation('Initializing...');
- EventId
If you have your own error class and mapped it using a number, you can use that number to show in the log. For example:
If this is your own way of identifying possible errors, use this number in the log.
TMultiLog4DUtil
.Logger
.EventId(1000)
.LogWriteInformation('Initializing...');
On Linux, logs are sent to the operating system's standard output, that is, to syslog. It is not possible to send logs to files, so you can simply monitor the log using the command line below in the Linux terminal:
tail -f /var/log/syslog
On Linux, you can also configure the EventId mentioned in the previous section.
MacOS applications can also be monitored and receive logs directly from Delphi. The monitoring method is exactly the same as on iOS, through the Console. Return to the iOS section to understand how to view the logs. The only difference is that you will see the name of your Mac device in the macOS sidebar.
As with Linux, it is not possible to create logs in a file. If you see the need to also send the log to a file, send your suggestion through ISSUES.
You have the option to disable or enable the log at any time, just use the EnableLog property as shown below:
TMultiLog4DUtil
.Logger
.EnableLog(False);
✍️ Note: The default for this property is True.
The library has a total of 05 (Five) Log methods, which are:
In this method, you need to define in the second parameter which type of log you want to send, that is: Information, Warning, Error or Fatal Error.
TMultiLog4DUtil
.Logger
.LogWrite('Message', lgInformation);
Next you will have the methods:
In these, it is not necessary to inform the log type as it will already be directed internally to the library.
✍️ Note: You can also chain several messages in a single call.
TMultiLog4DUtil
.Logger
.LogWriteInformation('Initializing the system')
.LogWriteInformation('Connecting to the server')
.LogWriteWarning('Validating user status');
✍️ Example of usage in an Exception:
procedure TForm1.Button1Click(Sender: TObject)
begin
try
//your code
except on E:Exception do
begin
TMultiLog4DUtil
.Logger
.LogWriteError(Format('Error: %s | %s', [E.ClassName, E.Message]));
end;
end;
end;
If you are looking to include logs in APIs developed in Horse, know that this is also possible, both for Windows and Linux. The process is the same, just add the library by downloading it or installing it through boss and it will work exactly as explained here.
🤔 Just remember that in Windows we can add logs in the Console, EventViewer and in Files. See a code example:
uses
Horse,
MultiLog4D.Common,
MultiLog4D.Util,
MultiLog4D.Types,
System.IOUtils,
System.SysUtils;
begin
TMultiLog4DUtil
.Logger
.LogWriteInformation('Start Application');
THorse
.Get('/test1',
procedure(Req: THorseRequest; Res: THorseResponse)
begin
Randomize;
TMultiLog4DUtil
.Logger
.LogWriteInformation('Before Test1 - ' + Format('Log test 1 message: %d', [Random(1000)]));
Res.Send('test1');
TMultiLog4DUtil
.Logger
.LogWriteInformation('After Test1 - ' + Format('Log test 1 message: %d', [Random(1000)]));
end;
THorse.Listen(9000);
end.
MultiLog4D
is free and open-source library licensed under the MIT License.