Download the PHP package idearia/php-logger without Composer
On this page you can find all versions of the php package idearia/php-logger. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download idearia/php-logger
More information about idearia/php-logger
Files in idearia/php-logger
Package php-logger
Short Description Simple logger class for PHP with time tracking
License GPL-3.0-or-later
Homepage https://github.com/Idearia/php-logger
Informations about the package php-logger
PHP Logger
Simple logger class to:
- Keep track of log entries.
- Keep track of timings with
time()
andtimeEnd()
methods, Javascript style. - Optionally write log entries in real-time to file or to screen.
- Optionally dump the log to file in one go at any time.
Log entries can be added with any of the following methods:
debug( $message, $title = '' )
> a diagnostic message intended for the developerinfo( $message, $title = '' )
> an informational message intended for the userwarning( $message, $title = '' )
> a warning that something might go wrongerror( $message, $title = '' )
> explain why the program is going to crash
The $title
argument is optional; if present, it will be prepended to the message: "$title => $message".
Quick example
The following code:
will print to STDOUT the following lines:
Timing
You can keep track of elapsed time by using the time()
and timeEnd()
function.
Timing example
will print:
Named timers
If you need to time different processes at the same time, you can use named timers.
For example:
will print:
Options
To customize the logger, you can either:
- extend the class and override the static properties or
- set the static properties at runtime.
In the following examples, we adopt the second approach.
Set the log level
By default, the logger will assume it runs in production and, therefore, will print only error-level messages.
Specify your desired log level in the following way:
Write to file
To also write to file, set:
To customize the log file path:
To overwrite the log file at every run of the script:
Do not print to screen
To prevent printing to STDOUT:
Parallel code caveat
The class uses the static methods and internal flags (e.g. $logger_ready
) to keep its state. We do this to make the class work straight away, without any previous configuration or the need to instantiate it. This however can create race conditions if you are executing parallel code. Please let us know if this is a problem for you, if we receive enough feedback, we will switch to a more class-like approach.
Contributing 🙂
Feel free to fork the repository and make a pull request!
Before sending the request, please make sure your code passes all the tests: