Skip to content

Latest commit

 

History

History
314 lines (243 loc) · 11.2 KB

README.md

File metadata and controls

314 lines (243 loc) · 11.2 KB

MultiLog4D

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.

🪄 Installation

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

📝 Usage

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:

💡How to view the Android log?

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.

💡How to view the log on iOS?

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.

⚠️ Attention: to filter only the logs of your application, type in the search, in the top right, the name of the TAG that you defined in Delphi and then press ENTER. A combobox will appear to the left of the search. Select the "Message" option in the combobox. And if you prefer, you can also filter the process. Type the name of the process in the search box (The project name is usually the name of your DPR in Delphi), press ENTER and then filter by "Process" in the combobox.

MultiLog4D Console

💻 Windows

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:

  • loFile: For generation in a file
  • loEventViewer: For generation in the Event Viewer
  • loConsole: For generation in the Console
      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.

    🏆 Additional Resources

    • 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:

  • ecNone
  • ecApplication
  • ecSecurity
  • ecPerformance
  • ecError
  • ecWarning
  • ecDebug
  • ecTransaction
  • ecNetwork

      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:
  • 1000 = System offline
  • 1001 = System online
  • 1010 = Connection error

    If this is your own way of identifying possible errors, use this number in the log.

      TMultiLog4DUtil
        .Logger
          .EventId(1000)
          .LogWriteInformation('Initializing...');

    💻 Linux

    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

    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.


    EnableLog

    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.


    LogWrite Variations

    The library has a total of 05 (Five) Log methods, which are:

  • LogWrite

    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:

  • LogWriteInformation
  • LogWriteWarning
  • LogWriteError
  • LogWriteFatalError

    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;

    ⭕ Horse

    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.
    


    Presentation Watch the video

    Documentation Languages

    English (en)
    Português (ptBR)

    ⚠️ License

    MultiLog4D is free and open-source library licensed under the MIT License.