Download the PHP package sharkydog/logger without Composer
On this page you can find all versions of the php package sharkydog/logger. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package logger
logger
Simple static logger.
The main purpose of this package is to provide simple logging/debugging from any scope. The logger can be easily filtered, adjusted or switched off.
Logging backends and message formatting are TBD and for now everything is printed on stdout. That might change, but the public api will not without a major version bump.
Currently messages look like this:
Usage
Messages are sent to the logger through several static methods that represent different logging levels.
These are (low to high): error
, warning
, info
, debug
, destruct
, memory
.
First four have the same signature:
Prefix (time,level) and suffix (tags) are added to the message after it is filtered and before being printed. Tags are arbitrary strings that can be used to filter out (or in) messages.
Destruct and memory levels are special, they are higher than debug (think verbose) and have different signatures:
Destruct logs when objects are being destroyed, in three ways.
- If
$object
is a string, it is assumed to be a class name and is just logged followed by the text in$msg
argument. - If
$object
is an object and PHP version is 8+, the object is added to a WeakMap and data is a Handle object which when destroyed, callsdestruct()
with the class name of the object being destructed. - If
$object
is an object and PHP version is 7.4, the Handle is set as a dynamic property to the object, namedSharkyDog\Log\Handle(handle_id)
, wherehandle_id
is the id of the Handle object as returned byspl_object_id()
. Caution should be taken when the class of the tracked object implements__set()
.
Memory level logs memory_get_usage()
and memory_get_peak_usage()
.
In the message:
P:
is the peak memory, R:
is real memory (memory_get_usage(true)
), (+ 495.39K)
is the change from previous memory log.
If $real
is true
, only real usage is logged.
Adjust log level
Logging level is set with:
Level constants are available:
Logger::SILENT
,Logger::ERROR
,Logger::WARNING
,Logger::INFO
,Logger::DEBUG
,Logger::DESTRUCT
,Logger::MEMORY
,Logger::ALL
Default is Logger::WARNING
, only errors and warnings.
This will set the level to debug and everything will be logged, except destruct and memory messages.
Filters
Filters are callbacks executed on every message, they are added with:
Filters can be added for a specific level or for all (Logger::ALL
).
They should follow the signature:
Filters are called in order they were added.
Returning boolean false
will stop processing the message, no more filters will be executed and the message is discarded.
You can do whatever you like with the message (and tags) before returning false
, like save it to a file or dump it to a proper logger.
A message will reach filters only if the level is set to accept it.