Overview
editThe unit LazLogger is supplied with Lazarus and provides logging to file or console.
Usage
editAdding logging support to your source code
editBy just including the unit lazlogger you can use the following:
- DebugLn: which works about the same as WriteLn.
- DebugLnEnter/DebugLnExit: same as debugln, but DebugLnEnter increases (and DebugLnExit decreases) the indentation of the written line.
- DbgOut: which works about the same as Write.
All of the above can be called with either:
- A single string, or list of (up to 15) strings: DebugLn('Foo'); DbgOut('a','b');
- An array of const: DebugLn(['Foo=', 1, ' Bar=', anInteger]);
- Same arguments as Format(). String + array of const: DbgOut('a %d',[1]);
Also there are many dbgs functions. Each converts a common type to a string for display. For example dbgs(aRect) converts a variable of type TRect to a string. You can write your own dbgs() functions for type conversions Lazarus does not provide, such as your custom records, which you can then use in DebugLn etc.
Activate/Deactivate LazLogger by using the correct unit
editLazLogger provides 3 units for your "uses" clause
- LazLogger
- Use this unit in your main program only. Using this unit activates the functionality of LazLogger.
- LazLoggerBase
- Use this unit whenever you want to use DebugLn. What LazLoggerBase does depends on whether the unit LazLogger is used in any other unit of your application. If the LazLogger unit is not used, LazLoggerBase installs a "black hole" logger which discards all log messages. If LazLogger is used anywhere, then all units using LazLoggerBase will write the log messages as described under "Logging Output"
- LazLoggerDummy
- This unit provides you with dummy DebugLn calls (and all other LazLogger functions). They are just calls to empty methods. This allows you to disable all logging in a unit, without the need to remove any debugln statement.
Logging output
editStdout/Console
editBy default the output created by DebugLn is written to the stdout device. The output is silently discarded if stdout is not open. On Windows this is the default behavior, if project type "Application" is used to create a Windows GUI application. To get a console alongside your Windows GUI application, you need to uncheck the option "Win32 gui application (-WG)" under Project options / Compiler options / Config and Target / Target specific options. Alternatively you can add {$APPTYPE CONSOLE} at the top of your project .lpr file, or configure multiple "Build Modes" to control the -WG switch.
File
editTo direct debug output to a file, use the DebugLogger.LogName property. All subsequent writing will be directed to the specified file:
DebugLn('default output');
DebugLogger.LogName:='debug1.log';
DebugLn('in debug1.log file');
LazLogger can also automatically read the file name from the command line. This requires the use of the LazLogger unit (not LazLoggerBase), which checks the parameter name specified in DebugLogger.ParamForLogFileName when the application starts (by default, it is --debug-log). For example, to direct debug output from an application (either yours or Lazarus IDE itself) to a file, call it via (or use Main Menu / Run / Run Parameters / Command line parameters):
project1 --debug-log=debug.log
If no command line option is specified, LazLogger checks the xxx_debuglog environment variable, where xxx is the name of the executable (without extension). For example, to direct debug output from the project1 application to file D:\debug.log, set the environment variable:
set project1_debuglog=D:\debug.log
project1
Usually, the file is opened on the first write, and closed when the application terminates. This means that the OS does not update the file on the disk with each write, but does it less often for performance. However, if the application suddenly crashes, the last writes may be lost. To avoid this, the CloseLogFileBetweenWrites property can be used:
DebugLogger.CloseLogFileBetweenWrites := true;
Debug server
editNot available in LazLogger; see DebugServer.
Log Groups
editYou can add log groups.
var MY_LOG_GROUP: PLazLoggerLogGroup;
initialization
MY_LOG_GROUP := DebugLogger.FindOrRegisterLogGroup('fancy_name_for_mylog' {$IFDEF MY_LOG_GROUP_ON_BY_DEFAULT} , True {$ENDIF} );
In your code use
debugln(MY_LOG_GROUP, 'log this text');
debugln(MY_LOG_GROUP, ['a=',a,' b=',b]); // a,b must be basic types: integer, byte, ansistring. See types available for "array of const"
and to enable it call your app with
--debug-enable=fancy_name_for_mylog
--debug-enable= takes a comma separated list
An already enabled group ( DebugLogger.FindOrRegisterLogGroup('grpname', True) ) can be disabled by specifying the group name with a minus.
--debug-enable=-fancy_name_for_mylog
The parameter name can be set via DebugLogger.ParamForEnabledLogGroups='--debug-enable';
Documentation
editFor more features, see the unit itself and the LCL documentation.
Using LazLogger/LazLoggerBase your code can access the DebugLogger instance. This offers some properties to fine tune logging.
You can also write your own logger class and use it with LazLogger/Base.
Multithreading
edit- DebugLn is thread safe, since Lazarus 2.0.
That is once the logger is created, see below
Earlier versions of this text stated "since 1.0", but a further race condition was found and fixed for 2.0.
- Calls to DbgOut, DebugLnEnter, DebugLnExit can be made from threads, but may not always format/indent the output correctly.
- The creation/setup/destruction/configuration of the logger is not thread-safe, and must be done before threads are started.
Creation is usually done with the first "debugln" call, maybe just debugln('') passing an empty string before any multi threaded code. Any other LazLogger access will also trigger creation and, once done, its done for the whole application.
But any properties (eg ParamForLogFileName) should be set before accessing the logger from threads.
- Callback to events like OnDebugLn are made in each thread. The code in the callback must be thread-safe by itself.
Changing the property is NOT thread-safe
See also
edit- TEventLog documentation Built in support for logging in FPC/Lazarus
- DebugServer Allows sending log entries to a viewer via IPC
- MultiLog
- log4delphi
- The package IdeLazLogger, which adds a dialog to the IDE allowing to enable/disable log groups used in the IDE itself.