Skip to content

Repository files navigation

Koded - Logging Library

A simple message logging library that implements PSR-3 with several log processors. It supports multiple log writers that can be set separately and process messages based on the log level.

Latest Stable VersionBuild StatusCode CoverageScrutinizer Code QualityMinimum PHP Version

Installation

Use composer and run

composer require koded/logging

or add it manually in your current composer.json

{
"require": {
"koded/logging": "^3"
}
}

Usage

<?php$settings = [
[
['class' => Cli::class, 'levels' => Log::ERROR],
['class' => File::class, 'levels' => Log::INFO]
]
];
$log = newLog(...$settings);
// This message is processed by Cli and File$log->alert('The message with {variable}', ['variable' => 'useful info']);
// This message won't be processed by Cli // because it's level is below ERROR,// but File will handle it$log->warning("You don't see anything");

Configuration

ParamTypeRequiredDefaultDescription
(processors)arrayyes(empty)An array of log processors. Every processor is defined in array with it's own configuration parameters. See processor directives
dateformatstringno"d/m/Y H:i:s.u"The date format for the log message. Microseconds are prepended by default
timezonestringno"UTC"The desired timezone for the log message

Processor default directives

Every log processor has it's own set of configuration directives.
The table shows log parameters in the classes.

ParamTypeRequiredDefaultDescription
classstringyesThe name of the log processor class
levelsintegerno-1Packed integer for bitwise comparison. See the constants in Logger class

Levels example

The messages are filtered with bitwise operator against the levels value. Every processor will filter out the messages as defined in it's levels directive.

For instance, to log only WARNING, INFO and ERROR messages, set levels to

<?php
[..., ['levels' => Log::WARN | Log::INFO | Log::ERROR, ...]],

Tips:

  • every processor is configured separately
  • if you want to process all log levels, skip the levels value or set it to -1 (by default)
  • if you want to suppress a specific processor, set it's level to 0

Processors

Class nameDescription
ErrorLoguses the error_log() function to send the message to PHP's logger
Cliwrite the messages in the console (with STDERR)
Memorywill store all messages in an array. Useful for unit tests if the logger is involved
SysLogwill open the system logger and send messages using the syslog() function
Filesaves the messages on a disk

Benchmarks and tests

vendor/bin/phpbench run --report=default
vendor/bin/phpunit

License

Software license The code is distributed under the terms of The 3-Clause BSD license.

About

Simple and fast logger package.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages