PHP Classes

ITE Logger: Log messages to different storage PSR-3 compliant

Recommend this page to a friend!
  Info   View files Example   View files View files (147)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 152 All time: 9,014 This week: 246Up
Version License PHP version Categories
ite-logger 1.2.3BSD License5.6PHP 5, Logging, Stream wrappers, Traits, P...
Description 

Author

This package can log messages to different storage containers compliant with PSR-3.

It provides classes to log messages to files and abstract classes for sending emails with logs and writing them to database.

It also has classes using the php function mail() for sending logs and PDO for storing log messages to database tables.

This package includes a LoggerAggregator class and trait that allows using of multiple nested loggers for all or different log levels.

It also provides a stream namespace with a trait, classes and interfaces that can be useful to write logs with stream functions, like fopen('log://<logger-type>/<log-level>', 'w').

Recommendations

Create a logger to an application
I need a logger class to insert in file or database.

Innovation Award
PHP Programming Innovation award nominee
September 2016
Number 7


Prize: One ebook of choice by Packt
PSR-3 is a standards recommendation that defines a common interface to implement logger classes.

This package implements the PSR-3 logger interface using several types of classes, as well a class and a trait that allows using multiple nested loggers.

It also provides a stream wrapper that allows developers to write to logs using regular file access functions.

Manuel Lemos
Picture of Kiril Savchev
  Performance   Level  
Name: Kiril Savchev <contact>
Classes: 6 packages by
Country: Bulgaria Bulgaria
Innovation award
Innovation award
Nominee: 5x

Details

If-Then-Else Logger package

Description

Set of classes the can be used for logging messages.

It provides simple file logging and abstract classes for sending emails with logs and writing them to database. It also has simple classes using internal php function mail() for sending logs and \PDO for writing into database tables. This packages includes LoggerAggregator class and trait that allow using of multiple nested loggers for all or different levels.

All the loggers implements PSR-3 logger standards. See http://www.php-fig.org/psr/psr-3/ for more information.

Usage

First run 'composer install' to install psr/log package. You can see the following examples in the 'example' folder

FileLogger example:

<?php

require_once '../vendor/autoload.php';

// Create the file logger with concrete log fils for 'alert' and 'error' levels:
$logger = new \Ite\Logger\FileLogger([
        'error' => '../data/logs/errors.log',
        'alert' => '../data/logs/errors.log'
]);
// the rest of levels will use the default log file

// log 10 info messages with fake context:
for ($i=0; $i<10; $i++) {
        $logger->info("Test info {$i}",['a' => 'test context', 'qwe' => 'alabala', 'q' => [1, 2, 4]]);
}
// log 10 error messages with exception:
for ($i=0; $i<10; $i++) {
        $logger->error("Test error {$i}", ['exception' => new Exception("Tesitng exceptions")]);
}
// log 10 alert messages with exception and fake context:
for ($i=0; $i<10; $i++) {
        $logger->alert("Test alert {$i}", ['exception' => new Exception("Tesitng exceptions"), 'a' => 'test context', 'qwe' => 'alabala', 'q' => [1, 2, 4]]);
}

PhpMailLogger example:

<?php

require_once '../vendor/autoload.php';

use Ite\Logger\PhpMailLogger;

// Change the email addres to yours:
$logger = new PhpMailLogger('me@xample.com');

// Log simple info message with fake content:
$logger->info("Test info",['a' => 'test context', 'nested_context' => [1, 2, 4]]);

PdoMysqlLogger example:

<?php

require_once '../vendor/autoload.php';

$dbConfig = [
        'host' => 'localhost',
        'name' => '{{DATABASE_NAME}}', // set your own here
        'username' => '{{USERNAME}}', // set your own here
        'password' => '{{PASSWORD}}', // set your own here
        'options' => [
                \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8"
        ]
];

/*
 *
 * You need to execute this query to use this logger:

CREATE TABLE logs(
        id INT(11) NOT NULL AUTO_INCREMENT,
        message VARCHAR(255) NOT NULL,
        level VARCHAR(10) NOT NULL,
        date DATETIME NOT NULL,
        context TEXT,
        PRIMARY KEY(ID)
);

*/

$logger = new Ite\Logger\PdoMysqlLogger($dbConfig);
$logger->info("Testing database", ['key1' => 'value1']);
$logger->alert("Testing database", ['alert' => 'alerting']);

LoggerAggregator example:

<?php

require_once '../vendor/autoload.php';

use Ite\Logger\FileLogger;
use Ite\Logger\LoggerAggregator;
use Ite\Logger\PhpMailLogger;

// create some simple loggers:
$logger = new FileLogger(['error' => '../data/logs/errors.log', 'alert' => '../data/logs/errors.log']);
$logger2 = new FileLogger();
$logger3 = new FileLogger();
$logger3->setDefaultLog('../data/logs/all.log');
// Change the email addres to yours:
$emailLogger = new PhpMailLogger('me@xample.com');

// create Logger Aggregator with a nested logger for 'error' and 'alert' level:
$loggerAggregator = new LoggerAggregator(['error' => $logger, 'alert' => $logger]);
// attach logger for 'info', 'debug' and 'error' levels:
$loggerAggregator->attachLogger($logger2, ['info', 'debug', 'error']);
// attach logger for all log levels:
$loggerAggregator->attachLogger($logger3);
// remove a logger for 'debug' level:
$loggerAggregator->detachLogger($logger2, 'debug');
// add email logger for critical messages:
$loggerAggregator->attachLogger($emailLogger, 'critical');

// fire some loggings:
for ($i=0; $i<10; $i++) {
        $loggerAggregator->info("Test info {$i}",['a' => 'test context', 'qwe' => 'alabala']);
}
for ($i=0; $i<10; $i++) {
        $loggerAggregator->error("Test error {$i}", ['exception' => new Exception("Tesitng exceptions")]);
}
for ($i=0; $i<10; $i++) {
        $loggerAggregator->alert("Test alert {$i}", ['exception' => new Exception("Tesitng exceptions"),'a' => 'test context', 'qwe' => 'alabala']);
}
for ($i=0; $i<10; $i++) {
        $loggerAggregator->debug("Test debug {$i}");
}
// Fire criticial log message (this should send email):
$loggerAggregator->critical("Critical message");

Stream logging:

<?php

require_once '../vendor/autoload.php';

use Ite\Logger\Stream\Strategy\AbstractDatabaseLoggerStrategy;
use Ite\Logger\Stream\Strategy\AbstractEmailLoggerStrategy;
use Ite\Logger\Stream\StreamLoggerTrait;
use Ite\Logger\Stream\FileLogger;
use Ite\Logger\Stream\PhpMailLogger;
use Ite\Logger\Stream\PdoMysqlLogger;
use Psr\Log\LoggerInterface;

# Test database logger:
//stream_register_wrapper('log', PdoMysqlLogger::class);
//$stream = fopen('log://db/info', 'w');

# Test email logger:
//stream_register_wrapper('log', PhpMailLogger::class);
//$stream = fopen('log://email/info?to=k.savchev@gmail.com', 'w');

# Test file logger:
stream_register_wrapper('log', FileLogger::class);
$stream = fopen('log://file/info?log=../data/logs/errors.log', 'w', STREAM_REPORT_ERRORS);

// Write log:
fwrite($stream, serialize(["Testing stream...", [1, 2, 3]]));
  Files folder image Files  
File Role Description
Files folder imagedata (1 directory)
Files folder imagedocs (2 files, 9 directories)
Files folder imageexamples (5 files)
Files folder imagesrc (10 files, 2 directories)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file license Lic. License text
Accessible without login Plain text file README.md Data Auxiliary data

  Files folder image Files  /  data  
File Role Description
Files folder imagelogs (3 files)

  Files folder image Files  /  data  /  logs  
File Role Description
  Accessible without login Plain text file all.log Data Auxiliary data
  Accessible without login Plain text file errors.log Data Auxiliary data
  Accessible without login Plain text file logger.log Data Auxiliary data

  Files folder image Files  /  docs  
File Role Description
Files folder imageclasses (20 files)
Files folder imagecss (5 files, 1 directory)
Files folder imagefiles (29 files, 2 directories)
Files folder imagefont (1 file)
Files folder imagegraphs (2 files)
Files folder imageimages (12 files, 1 directory)
Files folder imagejs (10 files, 1 directory)
Files folder imagenamespaces (6 files)
Files folder imagereports (3 files)
  Accessible without login Plain text file .htaccess Data Auxiliary data
  Accessible without login HTML file index.html Doc. Documentation

  Files folder image Files  /  docs  /  classes  
File Role Description
  Accessible without login HTML file Ite.Logger.AbstractDatabaseLogger.html Doc. Documentation
  Accessible without login HTML file Ite.Logger.AbstractEmailLogger.html Doc. Documentation
  Accessible without login HTML file Ite.Logger.Excepti...umentException.html Doc. Documentation
  Accessible without login HTML file Ite.Logger.Excepti...ntimeException.html Doc. Documentation
  Accessible without login HTML file Ite.Logger.FileLogger.html Doc. Documentation
  Accessible without login HTML file Ite.Logger.LoggerAggregator.html Doc. Documentation
  Accessible without login HTML file Ite.Logger.LoggerA...atorInterfrace.html Doc. Documentation
  Accessible without login HTML file Ite.Logger.LoggerAggregatorTrait.html Doc. Documentation
  Accessible without login HTML file Ite.Logger.MessagePrepareTrait.html Doc. Documentation
  Accessible without login HTML file Ite.Logger.PdoMysqlLogger.html Doc. Documentation
  Accessible without login HTML file Ite.Logger.PhpMailLogger.html Doc. Documentation
  Accessible without login HTML file Ite.Logger.Stream.FileLogger.html Doc. Documentation
  Accessible without login HTML file Ite.Logger.Stream.PdoMysqlLogger.html Doc. Documentation
  Accessible without login HTML file Ite.Logger.Stream.PhpMailLogger.html Doc. Documentation
  Accessible without login HTML file Ite.Logger.Stream....LoggerStrategy.html Doc. Documentation
  Accessible without login HTML file Ite.Logger.Stream....LoggerStrategy.html Doc. Documentation
  Accessible without login HTML file Ite.Logger.Stream....LoggerStrategy.html Doc. Documentation
  Accessible without login HTML file Ite.Logger.Stream....LoggerStrategy.html Doc. Documentation
  Accessible without login HTML file Ite.Logger.Stream....ategyInterface.html Doc. Documentation
  Accessible without login HTML file Ite.Logger.Stream.StreamLoggerTrait.html Doc. Documentation

  Files folder image Files  /  docs  /  css  
File Role Description
Files folder imagephpdocumentor-clean-icons (2 files, 1 directory)
  Accessible without login Plain text file bootstrap-combined.no-icons.min.css Data Auxiliary data
  Accessible without login Plain text file font-awesome.min.css Data Auxiliary data
  Accessible without login Plain text file jquery.iviewer.css Data Auxiliary data
  Accessible without login Plain text file prism.css Data Auxiliary data
  Accessible without login Plain text file template.css Data Auxiliary data

  Files folder image Files  /  docs  /  css  /  phpdocumentor-clean-icons  
File Role Description
Files folder imagefonts (2 files)
  Accessible without login Plain text file lte-ie7.js Data Auxiliary data
  Accessible without login Plain text file style.css Data Auxiliary data

  Files folder image Files  /  docs  /  css  /  phpdocumentor-clean-icons  /  fonts  
File Role Description
  Accessible without login Plain text file phpdocumentor-clean-icons.dev.svg Data Auxiliary data
  Accessible without login Plain text file phpdocumentor-clean-icons.svg Data Auxiliary data

  Files folder image Files  /  docs  /  files  
File Role Description
Files folder imageException (2 files)
Files folder imageStream (4 files, 1 directory)
  Accessible without login HTML file AbstractDatabaseLogger.html Doc. Documentation
  Accessible without login Plain text file AbstractDatabaseLogger.php.txt Doc. Documentation
  Accessible without login HTML file AbstractEmailLogger.html Doc. Documentation
  Accessible without login Plain text file AbstractEmailLogger.php.txt Doc. Documentation
  Accessible without login HTML file Exception.InvalidArgumentException.html Doc. Documentation
  Accessible without login HTML file Exception.RuntimeException.html Doc. Documentation
  Accessible without login HTML file FileLogger.html Doc. Documentation
  Accessible without login Plain text file FileLogger.php.txt Doc. Documentation
  Accessible without login HTML file LoggerAggregator.html Doc. Documentation
  Accessible without login Plain text file LoggerAggregator.php.txt Doc. Documentation
  Accessible without login HTML file LoggerAggregatorInterfrace.html Doc. Documentation
  Accessible without login Plain text file LoggerAggregatorInterfrace.php.txt Doc. Documentation
  Accessible without login HTML file LoggerAggregatorTrait.html Doc. Documentation
  Accessible without login Plain text file LoggerAggregatorTrait.php.txt Doc. Documentation
  Accessible without login HTML file MessagePrepareTrait.html Doc. Documentation
  Accessible without login Plain text file MessagePrepareTrait.php.txt Doc. Documentation
  Accessible without login HTML file PdoMysqlLogger.html Doc. Documentation
  Accessible without login Plain text file PdoMysqlLogger.php.txt Doc. Documentation
  Accessible without login HTML file PhpMailLogger.html Doc. Documentation
  Accessible without login Plain text file PhpMailLogger.php.txt Doc. Documentation
  Accessible without login HTML file Stream.FileLogger.html Doc. Documentation
  Accessible without login HTML file Stream.PdoMysqlLogger.html Doc. Documentation
  Accessible without login HTML file Stream.PhpMailLogger.html Doc. Documentation
  Accessible without login HTML file Stream.Strategy.Ab...LoggerStrategy.html Doc. Documentation
  Accessible without login HTML file Stream.Strategy.Ab...LoggerStrategy.html Doc. Documentation
  Accessible without login HTML file Stream.Strategy.FileLoggerStrategy.html Doc. Documentation
  Accessible without login HTML file Stream.Strategy.Pd...LoggerStrategy.html Doc. Documentation
  Accessible without login HTML file Stream.Strategy.St...ategyInterface.html Doc. Documentation
  Accessible without login HTML file Stream.StreamLoggerTrait.html Doc. Documentation

  Files folder image Files  /  docs  /  files  /  Exception  
File Role Description
  Accessible without login Plain text file InvalidArgumentException.php.txt Doc. Documentation
  Accessible without login Plain text file RuntimeException.php.txt Doc. Documentation

  Files folder image Files  /  docs  /  files  /  Stream  
File Role Description
Files folder imageStrategy (5 files)
  Accessible without login Plain text file FileLogger.php.txt Doc. Documentation
  Accessible without login Plain text file PdoMysqlLogger.php.txt Doc. Documentation
  Accessible without login Plain text file PhpMailLogger.php.txt Doc. Documentation
  Accessible without login Plain text file StreamLoggerTrait.php.txt Doc. Documentation

  Files folder image Files  /  docs  /  files  /  Stream  /  Strategy  
File Role Description
  Accessible without login Plain text file AbstractDatabaseLoggerStrategy.php.txt Doc. Documentation
  Accessible without login Plain text file AbstractEmailLoggerStrategy.php.txt Doc. Documentation
  Accessible without login Plain text file FileLoggerStrategy.php.txt Doc. Documentation
  Accessible without login Plain text file PdoMysqlLoggerStrategy.php.txt Doc. Documentation
  Accessible without login Plain text file StreamLoggerStrategyInterface.php.txt Doc. Documentation

  Files folder image Files  /  docs  /  font  
File Role Description
  Accessible without login Plain text file fontawesome-webfont.svg Data Auxiliary data

  Files folder image Files  /  docs  /  graphs  
File Role Description
  Accessible without login HTML file class.html Doc. Documentation
  Accessible without login Plain text file classes.svg Data Auxiliary data

  Files folder image Files  /  docs  /  images  
File Role Description
Files folder imageiviewer (8 files)
  Accessible without login Image file apple-touch-icon-114x114.png Icon Icon image
  Accessible without login Image file apple-touch-icon-72x72.png Icon Icon image
  Accessible without login Image file apple-touch-icon.png Icon Icon image
  Accessible without login Plain text file custom-icons.svg Data Auxiliary data
  Accessible without login Image file favicon.ico Data Auxiliary data
  Accessible without login Image file hierarchy-item.png Icon Icon image
  Accessible without login Image file icon-class-13x13.png Icon Icon image
  Accessible without login Plain text file icon-class.svg Data Auxiliary data
  Accessible without login Image file icon-interface-13x13.png Icon Icon image
  Accessible without login Plain text file icon-interface.svg Data Auxiliary data
  Accessible without login Image file icon-trait-13x13.png Icon Icon image
  Accessible without login Plain text file icon-trait.svg Data Auxiliary data

  Files folder image Files  /  docs  /  images  /  iviewer  
File Role Description
  Accessible without login Image file grab.cur Data Auxiliary data
  Accessible without login Image file hand.cur Data Auxiliary data
  Accessible without login Image file iviewer.rotate_left.png Icon Icon image
  Accessible without login Image file iviewer.rotate_right.png Icon Icon image
  Accessible without login Image file iviewer.zoom_fit.png Icon Icon image
  Accessible without login Image file iviewer.zoom_in.png Icon Icon image
  Accessible without login Image file iviewer.zoom_out.png Icon Icon image
  Accessible without login Image file iviewer.zoom_zero.png Icon Icon image

  Files folder image Files  /  docs  /  js  
File Role Description
Files folder imageui (1 directory)
  Accessible without login Plain text file bootstrap.min.js Data Auxiliary data
  Accessible without login Plain text file html5.js Data Auxiliary data
  Accessible without login Plain text file jquery-1.11.0.min.js Data Auxiliary data
  Accessible without login Plain text file jquery.dotdotdot-1.5.9.js Data Auxiliary data
  Accessible without login Plain text file jquery.dotdotdot-1.5.9.min.js Data Auxiliary data
  Accessible without login Plain text file jquery.iviewer.js Data Auxiliary data
  Accessible without login Plain text file jquery.iviewer.min.js Data Auxiliary data
  Accessible without login Plain text file jquery.mousewheel.js Data Auxiliary data
  Accessible without login Plain text file jquery.smooth-scroll.js Data Auxiliary data
  Accessible without login Plain text file prism.min.js Data Auxiliary data

  Files folder image Files  /  docs  /  js  /  ui  
File Role Description
Files folder image1.10.4 (1 file)

  Files folder image Files  /  docs  /  js  /  ui  /  1.10.4  
File Role Description
  Accessible without login Plain text file jquery-ui.min.js Data Auxiliary data

  Files folder image Files  /  docs  /  namespaces  
File Role Description
  Accessible without login HTML file default.html Doc. Documentation
  Accessible without login HTML file Ite.html Doc. Documentation
  Accessible without login HTML file Ite.Logger.Exception.html Doc. Documentation
  Accessible without login HTML file Ite.Logger.html Doc. Documentation
  Accessible without login HTML file Ite.Logger.Stream.html Doc. Documentation
  Accessible without login HTML file Ite.Logger.Stream.Strategy.html Doc. Documentation

  Files folder image Files  /  docs  /  reports  
File Role Description
  Accessible without login HTML file deprecated.html Doc. Documentation
  Accessible without login HTML file errors.html Doc. Documentation
  Accessible without login HTML file markers.html Doc. Documentation

  Files folder image Files  /  examples  
File Role Description
  Accessible without login Plain text file database_example.php Example Example script
  Accessible without login Plain text file file_logger.php Example Example script
  Accessible without login Plain text file logger_aggregator.php Example Example script
  Accessible without login Plain text file mail_example.php Example Example script
  Plain text file stream_logger.php Class Class source

  Files folder image Files  /  src  
File Role Description
Files folder imageException (2 files)
Files folder imageStream (4 files, 1 directory)
  Plain text file AbstractDatabaseLogger.php Class Class source
  Plain text file AbstractEmailLogger.php Class Class source
  Plain text file FileLogger.php Class Class source
  Plain text file LoggerAggregator.php Class Class source
  Plain text file LoggerAggregatorInterfrace.php Class Class source
  Plain text file LoggerAggregatorTrait.php Class Class source
  Plain text file MessagePrepareTrait.php Class Class source
  Plain text file NoopLogger.php Class Class source
  Plain text file PdoMysqlLogger.php Class Class source
  Plain text file PhpMailLogger.php Class Class source

  Files folder image Files  /  src  /  Exception  
File Role Description
  Plain text file InvalidArgumentException.php Class Class source
  Plain text file RuntimeException.php Class Class source

  Files folder image Files  /  src  /  Stream  
File Role Description
Files folder imageStrategy (5 files)
  Plain text file FileLogger.php Class Class source
  Plain text file PdoMysqlLogger.php Class Class source
  Plain text file PhpMailLogger.php Class Class source
  Plain text file StreamLoggerTrait.php Class Class source

  Files folder image Files  /  src  /  Stream  /  Strategy  
File Role Description
  Plain text file AbstractDatabaseLoggerStrategy.php Class Class source
  Plain text file AbstractEmailLoggerStrategy.php Class Class source
  Plain text file FileLoggerStrategy.php Class Class source
  Plain text file PdoMysqlLoggerStrategy.php Class Class source
  Plain text file StreamLoggerStrategyInterface.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:152
This week:0
All time:9,014
This week:246Up