logga
, (*1)
Logga is a logging library for PHP designed for simplicity and ease of use. Starting from v1.0.0, Logga features support for multiple logging streams and message formatters, providing developers with great flexibility., (*2)
Current version features a file stream and a default formatter. Future releases of Logga will have a Database stream out of the box, as well as a plugin system to allow developers to write custom streams and formatters so that pretty much every logging need is covered., (*3)
Installation
Composer
Since version 2.0.0 Logga can be installed using Composer. Add the following dependency to your composer.json
:, (*4)
{
"require": {
"carlosafonso/logga": "2.*",
}
}
Then run:, (*5)
$ composer update
Don't forget to include Composer's autoload file if you haven't done so already:, (*6)
require 'vendor/autoload.php':
Manual installation
Logga can also be installed by downloading the latest version and unzipping everything into your project folder, or by cloning the repository:, (*7)
$ git clone https://github.com/carlosafonso/logga.git
Remember to include the main library file into your project:, (*8)
require 'logga.php';
Quick start
Set up a quick logger with the following code:, (*9)
$l = new \Logga\Logga();
$l->info('Hello world!');
If you run this code, the following line will be printed on screen:, (*10)
[2014-02-15 17:42:14][INFO ]: Hello world!
Furthermore, a file named something like default_log.log
will appear in the folder you're running your script from. A quick look into it reveals the same content shown above., (*11)
And that's pretty much it! You have set up your logging system with just a couple of lines. From now on you can trace everything you need using any of the eight logging functions provided by Logga (debug()
, info()
, notice()
, warning()
, error()
, critical()
, alert()
and emergency()
)., (*12)
Usage
The above is simple enough for the average developer but you might want to customize Logga a bit in order to fulfill your needs. This section will show you how., (*13)
Understanding streams
In Logga, as in other logging libraries, a stream is an abstraction of a place where log messages are stored. You can use as many streams as you need, so that logging a message just once produces the same output in different places (i.e., a plain text file and a database table)., (*14)
Additionally, you can configure each stream: for example, you might want all messages to be logged to the plain text file but only WARNING messages or above into the database., (*15)
As of version 2.2.0, Logga comes with 3 streams: FileStream
, HttpStream
and StandardOutputStream
(with several others currently in the works, such as DatabaseStream
and MailStream
)., (*16)
Default streams
If you don't specifically provide any stream to Logga's constructor, the library will use both a FileStream
and a StandardOutputStream
by default (the first one logging to a file named default_log.log
)., (*17)
Creating a custom stream
You can create a stream by instantiating any class which extends from LogStream
, optionally passing an array with the desired options:, (*18)
$s = new FileStream(array('file' => 'my_custom_log_file', 'date' => TRUE));
$l = new Logga($s);
$l->info('Hi, custom file!');
The above will produce a file called my_custom_log_file_<datetime>.log
, where datetime
is the current date and time., (*19)
You can use more than one stream. Just call Logga's constructor with an array of streams:, (*20)
$s1 = new FileStream(array('file' => 'my_first_custom_log_file', 'date' => TRUE));
$s2 = new FileStream(array('file' => 'my_second_custom_log_file', 'date' => TRUE));
$l = new Logga(array($s1, $s2));
$l->info('Hi, custom file!');
Streams
Specifying a log level
, (*21)
Enabling and disabling streams
, (*22)
Available stream classes
, (*23)
, (*24)